如何在Python Selenium中实现类似TestNG的功能或在一个测试套件中添加多个单元测试?

如何在Python Selenium中实现类似TestNG的功能或在一个测试套件中添加多个单元测试?,第1张

如何在Python Selenium中实现类似TestNG的功能或在一个测试套件中添加多个单元测试?

鉴于您可能要构建测试套件的原因可能有多种,我将提供几种选择。

仅从目录运行测试

假设有

mytests
目录:

mytests/├── test_something_else.py└── test_thing.py

从该目录运行所有测试很容易

$> nosetests mytests/

例如,您可以将烟雾测试,单元测试和集成测试放入不同的目录中,但仍然可以运行“所有测试”:

$> nosetests functional/ unit/ other/
通过标签运行测试

nose.有属性选择器插件。用这样的测试:

import unittestfrom nose.plugins.attrib import attrclass 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 pythonimport unittestimport nosedef 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 suiteif __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文件的路径。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/5641979.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存