在nosetest中,我知道可以指定要通过nosetests文件运行哪些测试:
[nosetests]
tests=testIWT_AVW.py:testIWT_AVW.tst_bynd1,testIWT_AVW.py:testIWT_AVW.tst_bynd3
然而,当添加了大量的测试时,上面的内容看起来很混乱,而且很难维护,特别是不能使用换行器。我发现能够使用unittests特性指定要运行哪些测试要方便得多。例如:
def custom_suite():
suite = unittest.TestSuite()
suite.addTest(testIWT_AVW('tst_bynd1'))
suite.addTest(testIWT_AVW('tst_bynd3'))
return suite
if __name__=="__main__":
runner = unittest.TextTestRunner()
runner.run(custom_suite())
问题:我如何指定哪些测试应该由nosetest在我的.py文件中运行?
谢谢。
如果有一种方法可以通过nosetest配置文件指定测试,而不强制将所有测试写在一行上,那么我也会打开它,作为第二种选择
发布于 2013-07-23 14:37:30
我不完全确定您是希望以编程方式还是从命令行运行测试。无论是哪种方式,这两者都应包括在内:
import itertools
from nose.loader import TestLoader
from nose import run
from nose.suite import LazySuite
paths = ("/path/to/my/project/module_a",
"/path/to/my/project/module_b",
"/path/to/my/project/module_c")
def run_my_tests():
all_tests = ()
for path in paths:
all_tests = itertools.chain(all_tests, TestLoader().loadTestsFromDir(path))
suite = LazySuite(all_tests)
run(suite=suite)
if __name__ == '__main__':
run_my_tests()
注意,nose.suite.TestLoader对象有许多不同的方法可用于加载测试。
您可以从其他代码调用run_my_tests
方法,也可以使用python解释器从命令行运行这个方法,而不是通过鼻子运行。如果您有其他的鼻子配置,您可能也需要以编程的方式传递它。
https://stackoverflow.com/questions/16268688
复制相似问题