鉴于您可能要构建测试套件的原因可能有多种,我将提供几种选择。
假设有mytests
目录:
mytests/
├── test_something_else.py
└── test_thing.py
从该目录运行所有测试很容易
$> nosetests mytests/
例如,您可以将烟雾测试,单元测试和集成测试放入不同的目录中,但仍然可以运行“所有测试”:
$> nosetests functional/ unit/ other/
nose.有属性选择器插件。用这样的测试:
import unittest
from nose.plugins.attrib import attr
class Thing1Test(unittest.TestCase):
@attr(platform=("windows", "linux"))
def test_me(self):
self.assertNotEqual(1, 0 - 1)
@attr(platform=("linux", ))
def test_me_also(self):
self.assertFalse(2 == 1)
您将能够运行具有特定标签的测试:
$> nosetests -a platform=linux tests/
$> nosetests -a platform=windows tests/
最后,nose.main
支持suite
参数:如果传递了,则 发现不会完成。在这里,我为您提供了有关如何手动构造测试套件然后使用Nose运行它的基本示例:
#!/usr/bin/env python
import unittest
import nose
def get_cases():
from test_thing import Thing1Test
return [Thing1Test]
def get_suite(cases):
suite = unittest.TestSuite()
for case in cases:
tests = unittest.defaultTestLoader.loadTestsFromTestCase(case)
suite.addTests(tests)
return suite
if __name__ == "__main__":
nose.main(suite=get_suite(get_cases()))
如您所见,nose.main
获得unittest
由构造并返回的常规测试套件get_suite
。该get_cases
功能是您选择的测试用例被“加载”的地方(例如,上面的案例类仅被导入)。
如果您确实需要XML,get_cases
则可以在这里返回从通过解析的XML文件获得的模块(通过__import__
或 导入importlib.import_module
)获得的案例类。在nose.main
通话附近,您可以argparse
用来获取XML文件的路径。