0

trend.py and test_trend.py are in the same folder. I have a class Trend with a function find_regres_values that is called from a instance method perform_analysis.

trend.py:

class Trend: def __init__(self, values, trend_type): self.all_values = values def permorn_analysis(self, first, trend_type): #blabla vals_reg = ["a", "b", "c", "d"] find_regres_values(vals_reg, first, trend_type) def find_regres_values(vals_reg, first, trend_type): #do somethin pass 

in test_trend.py

from trend import find_regres_values class ConsecRegions(unittest.TestCase): def test_find_regres_values_decreas_min_before_max(self): #initialize some values output = find_regres_values(vals_reg, first, trend_type) self.assertEqual(output, result) 

It shows me an error:

 File "test_trend.py", line 2, in <module> from trend import find_regres_values ImportError: cannot import name find_regres_values 

How do I import one function for testing?

3
  • Please add more details about your python version 2.x 3.x Commented Mar 9, 2017 at 9:51
  • Your function resides inside a class. You will need to import that class and then (instantiate if need) use the method. Commented Mar 9, 2017 at 9:52
  • @Tonja fix missing : in code and some lit to real list exemple to test without error. find_regres_values have indentation problem... Commented Mar 9, 2017 at 10:29

2 Answers 2

5

find_regres_values is a method of the class Trend, If you want find_regres_values to be its own function then remove the indentation

class Trend: def __init__(self, values, trend_type): self.all_values = values def permorn_analysis(self,first,trend_type) #blabla vals_reg = some list find_regres_values(vals_reg, first, trend_type) def find_regres_values(vals_reg, first, trend_type): #do something 
Sign up to request clarification or add additional context in comments.

1 Comment

in addition if find_regres_values is a class method, self param is missing.
0

What Python version you use?

If Python 3.x:

Create empty file __init__.py

For correct import use this code:

from trend import Trend 

and edit call of method:

from trend import Trend class ConsecRegions(unittest.TestCase): def test_find_regres_values_decreas_min_before_max(self): #initialize some values output = Trend.find_regres_values(vals_reg, first, trend_type) self.assertEqual(output, result) 

For information:

In file trend.py after permorn_analysis method insert a colon.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.