[英]Calling a method from another file AttributeError: object has no attribute
I'm trying to create a unit test and I'm having some errors when I try to use a function from another file.我正在尝试创建一个单元测试,但当我尝试使用另一个文件中的 function 时遇到了一些错误。 You can see an example of the basic idea of what I am trying to do below.
您可以在下面看到我正在尝试做的基本想法的示例。 I am a beginner in Python so I'm not sure what's the problem.
我是 Python 的初学者,所以我不确定是什么问题。
In file 1: model.py在文件 1 中:model.py
Class Model(parameters):
def calc_maximum(self, data, thresholds):
df['Max']= ...
return df
def calc_model_output(self,data,param,param2):
S=self.calc_maximum(data,thresholds)
Si=S+param2
return Si
in file 2: test.py在文件 2 中:test.py
import model as ml
import unittest
...
...
class Tests(unittest.Testcase):
def test_calc_maximum(self):
Expected1=ml.Model.calc_maximum(self,input1, input2)
def test_calc_model_output(self):
Expected2=ml.Model.calc_model_output(self,input1,input2,input3)
when I try to run test.py file, the test for Expected1
seems to work ok, but I get the following error from the Expected2
row: AttributeError: 'Tests' object has no attribute 'calc_maximum
.当我尝试运行 test.py 文件时,对
Expected1
的测试似乎工作正常,但我从Expected2
行收到以下错误: AttributeError: 'Tests' object has no attribute 'calc_maximum
。
Any ideas?有任何想法吗?
Just add brackets after Model in Expected1, ie make Expected1 Expected1中的Model后面加上括号即可,即make Expected1
Expected1=ml.Model().calc_maximum(input1,input2)
Do the same for Expected2对 Expected2 做同样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.