您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

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

假设有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文件的路径。

python 2022/1/1 18:14:54 有437人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶