假设我有两个最鼻子 ExampleTest1.py 和 ExampleTest2.py
ExampleTest1.py
class ExampleTest1(TestBase):
"""
"""
def testExampleTest1(self):
-----
-----
if __name__ == "__main__":
import nose
nose.run()
---------------
ExampleTest2.py
class ExampleTest2(TestBase):
"""
"""
def testExampleTest2(self):
-----
-----
if __name__ == "__main__":
import nose
nose.run()
现在我想从单个套件运行数百个测试文件。
我正在寻找类似testng的功能,例如testng.xml在下面我可以添加所有应该一一运行的测试文件
<suite name="Suite1">
<test name="ExampleTest1">
<classes>
<class name="ExampleTest1" />
</classes>
</test>
<test name="ExampleTest2">
<classes>
<class name="ExampleTest2" />
</classes>
</test>
</suite>
如果 python 中没有 testng.xml like 功能,那么还有什么其他选择来创建测试套件并在那里包含我所有的 python 测试?谢谢
鉴于您可能想要的原因可能有多种不同 为了构建测试套件,我将为您提供几个选项。
只是从目录运行测试
假设有mytests
目录:
mytests/
├── test_something_else.py
└── test_thing.py
从该目录运行所有测试很容易
$> nosetests mytests/
例如,可以将烟雾、单元和集成测试放入 不同的目录,仍然能够运行"所有测试":
$> nosetests functional/ unit/ other/
按标记运行测试
鼻子有 属性选择器插件。 通过这样的测试:
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
可能是您返回大小写的地方 从模块中获取的类(通过__import__
或导入(导入importlib.import_module
(,这是您从解析的 XML 文件中获取的。 在调用附近nose.main
您可以使用argparse
来获取 XML 文件的路径。