0
我试图通过nosetest自动创建测试用例并运行它们。当命令运行创建自动测试用例
测试运行正常: 蟒蛇-m单元测试test_auto1 蟒蛇-m单元测试test_auto1.TestAuto.test_two
不过,如果我尝试使用nosetest运行测试,它在一定条件下失败:
1)nosetests test_auto1 - 失败,错误
2)nosetests test_auto1:TestAuto - 工作正常
3)nosetests test_auto1:TestAuto.test_one - 失败,错误
下面是测试代码:
import unittest
def generator(test_class, a, b):
def test(self):
self.assertEqual(a, b)
return test
def add_test_methods(test_class):
#First element of list is variable "a", then variable "b", then name of test case that will be used as suffix.
test_list = [[2,3, 'one'], [5,5, 'two'], [0,0, 'three']]
for case in test_list:
test = generator(test_class, case[0], case[1])
setattr(test_class, "test_%s" % case[2], test)
class TestAuto(unittest.TestCase):
def setUp(self):
print 'Setup'
pass
def tearDown(self):
print 'TearDown'
pass
add_test_methods(TestAuto)
if __name__ == '__main__':
unittest.main(verbosity=1)
的错误,同时运行单一的测试是:
======================================================================
ERROR: Failure: ValueError (no such test method in <class 'test_auto2.TestAuto'>: test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose-1.3.1-py2.7.egg\nose\loader.py", line 516, in makeTest
return self._makeTest(obj, parent)
File "C:\Python27\lib\site-packages\nose-1.3.1-py2.7.egg\nose\loader.py", line 570, in _makeTest
return parent(obj.__name__)
File "C:\Python27\lib\unittest\case.py", line 189, in __init__
(self.__class__, methodName))
ValueError: no such test method in <class 'test_auto2.TestAuto'>: test
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
这不是我一直在寻找,但它肯定教的东西我认为是非常有帮助...谢谢 –