如何在Python Selenium中实现类似TestNG的功能或在一个测试套件中添加多个单元测试? [英] How to achieve TestNG like feature in Python Selenium or add multiple unit test in one test suite?

查看:306
本文介绍了如何在Python Selenium中实现类似TestNG的功能或在一个测试套件中添加多个单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这两个鼻子测试ExampleTest1.py和ExampleTest2.py

Suppose I have this two nosetest ExampleTest1.py and 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()

现在,我想从一个套件中运行数百个测试文件.

Now I want to run such hundreds of test files from a single suite.

我正在寻找类似TestNG之类的功能,例如testng.xml,在这里我可以添加所有应该逐个运行的测试文件

I am looking something like TestNG feature like testng.xml below where I can add all my test files which should run one by one

 <suite name="Suite1">
      <test name="ExampleTest1">
        <classes>
           <class name="ExampleTest1" />          
        </classes>
      </test>  
      <test name="ExampleTest2">
        <classes>
           <class name="ExampleTest2" />          
        </classes>
      </test>  
    </suite> 

如果类似testng.xml的功能在python中不可用,那么创建测试套件并在其中包含我所有的python测试的其他选择是什么?谢谢

In case testng.xml like feature is not available in python, then what is other alternative to create test suites and include all my python test there? Thanks

推荐答案

鉴于您可能有多种不同的原因, 构建测试套件,我会给您几个选择.

Given that there may be multiple different reasons why you may want to construct test suites, I’ll give you several options.

假设有mytests目录:

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

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

Running all tests from that dir is a easy as

$> nosetests mytests/

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

For example, you could put smoke, unit, and integration tests into different dirs and still be able to run "all tests":

$> nosetests functional/ unit/ other/

通过代码运行测试

鼻子有 属性选择器插件. 通过这样的测试:

Running tests by tag

Nose has attribute selector plugin. With test like this:

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)

您将能够运行具有特定标记的测试:

You’ll be able to run tests that have particular tag:

$> nosetests -a platform=linux tests/

$> nosetests -a platform=windows tests/

运行手动构建的测试套件

最后,nose.main支持suite参数:如果传递了, 发现未完成. 在这里,我为您提供了有关如何手动构建的基本示例 测试套件,然后使用鼻子"运行它:

Running manually constructed test suite

Finally, nose.main supports suite argument: if it is passed, discovery is not done. Here I provide you basic example of how to manually construct test suite and then run it with 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功能是测试用例的地方 您选择的是已加载"(在上述示例中,仅导入了案例类).

As you can see, nose.main gets regular unittest test suite, constructed and returned by get_suite. The get_cases function is where test cases of your choice are "loaded" (in example above case class is just imported).

如果您确实需要XML,则get_cases可能是您返回大小写的地方 从模块获取的类(通过__import__importlib.import_module),您可以从解析的XML文件中获取. 在nose.main调用附近,您可以使用argparse获取XML文件的路径.

If you really need XML, get_cases may be the place where you return case classes that you get from modules (imported via __import__ or importlib.import_module) which you get from XML file you parsed. And near nose.main call you could use argparse to get path to XML file.

这篇关于如何在Python Selenium中实现类似TestNG的功能或在一个测试套件中添加多个单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆