Python AttributeError:对象没有属性';运行测试&x27;
我是新来的Python AttributeError:对象没有属性';运行测试&x27;,python,python-unittest,Python,Python Unittest,我是新来的unittest,我不知道为什么会出现这个错误: runTest (__main__.TestTimeInterval) No test ... Traceback (most recent call last): File "/Users/bli1/Development/Trinity/qa-trinity/python_lib/qe/tests/test_timestamp_interval.py", line 122, in <module> sys.e
unittest
,我不知道为什么会出现这个错误:
runTest (__main__.TestTimeInterval)
No test ... Traceback (most recent call last):
File "/Users/bli1/Development/Trinity/qa-trinity/python_lib/qe/tests/test_timestamp_interval.py", line 122, in <module>
sys.exit(main(sys.argv))
File "/Users/bli1/Development/Trinity/qa-trinity/python_lib/qe/tests/test_timestamp_interval.py", line 110, in main
result_set = runner.run(suite)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/runner.py", line 168, in run
test(result)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/suite.py", line 87, in __call__
return self.run(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/suite.py", line 125, in run
test(result)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 625, in __call__
return self.run(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 555, in run
testMethod = getattr(self, self._testMethodName)
AttributeError: 'TestTimeInterval' object has no attribute 'runTest'
您几乎不需要自己创建TestSuite和TestRunner。通常情况下,您会这样做:
# my_test.py
import unittest
class Something(object):
def __init__(self):
self.foo = 1
class TestSomething(unittest.TestCase):
def setUp(self):
super(TestSomething, self).setUp()
self.something = Something()
def test_record(self):
self.assertTrue(1 > 0)
def test_something_foo_equals_1(self):
self.assertEqual(self.something.foo, 1)
if __name__ == '__main__':
unittest.main()
现在,要运行测试,只需执行脚本
python my_test.py
我不确定这里的方向是否正确,但为什么要重写
\uuu init\uuuu()
,而不是实现setUp()
?@favoretti我需要传入参数,这样我就可以用参数初始化对象。如何将对象传入要测试的对象中?@Liondancer——通常,TestSomething
应该创建要测试的对象。在每次测试前后都会运行一些特殊的方法(设置
和拆卸
),这样就可以自然地创建需要测试的对象。我将更新我的示例代码…我仍然有点困惑,因为在我的代码中,我用我的位置参数创建了一个对象(Something
)。我是否需要将某些内容传递到TestSomething
中才能使用我刚刚创建的对象?@liondacer——我不确定我是否理解。在测试中,您可以使用希望传递给它的任何位置参数来创建某物。。。如果你看第70行,我创建了一个对象。但我不确定如何将该实例传递到设置中使用。
python my_test.py