我正在测试模块中编写一组测试用例,例如 Test1、Test2。
有没有办法使用命令nose.main()跳过Test1或有选择地仅执行该模块中的Test2?
我的模块包含,
test_module.py,
class Test1:
setUp(self):
print('setup')
tearDown(self):
print('teardown')
test(self):
print('test1')
class Test2:
setUp(self):
print('setup')
tearDown(self):
print('teardown')
test(self):
print('test2')
我使用不同的 python 文件运行它,
if __name__ == '__main__':
nose.main('test_module')
最佳答案
在nose上下文中,跳过测试和不运行测试的概念是不同的:跳过的测试将在测试结果末尾报告为已跳过。如果您想跳过测试,则必须使用装饰器对测试模块进行猴子修补或执行其他一些黑魔法。
但是,如果您不想运行测试,则可以按照与命令行相同的方式进行操作:使用 --exclude 选项。它采用您不想运行的测试的正则表达式。像这样的事情:
import sys
import nose
def test_number_one():
pass
def test_number_two():
pass
if __name__ == '__main__':
module_name = sys.modules[__name__].__file__
nose.main(argv=[sys.argv[0],
module_name,
'--exclude=two',
'-v'
])
运行测试将为您提供:
$ python stackoverflow.py
stackoverflow.test_number_one ... ok
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
关于Python 单元测试 : In Nose is there a way to skip a test case from nose. run()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29397973/