繁体   English   中英

使用 py.test 运行测试套件(任意测试集合)

[英]running a test suite (an arbitrary collection of tests) with py.test

我正在使用 py.test 构建功能测试框架,因此我需要能够指定要运行的确切测试。 我了解动态测试集合的美妙之处,但我希望能够先运行我的测试环境健康检查,然后再运行我的回归测试; 该分类并不排除将这些集合中的测试用于其他目的。

测试套件将绑定到 Jenkins 构建项目。 我正在使用 osx、python 2.7.3、py.test 2.3.4。

所以我有一个如下的测试用例:

# sample_unittest.py
import unittest, pytest

class TestClass(unittest.TestCase):

    def setUp(self):
        self.testdata = ['apple', 'pear', 'berry']

    def test_first(self):
        assert 'apple' in self.testdata

    def test_second(self):
        assert 'pear' in self.testdata

    def tearDown(self):
        self.testdata = []

def suite():
    suite = unittest.TestSuite()
    suite.addTest(TestClass('test_first'))
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())

我有一个这样的测试套件:

# suite_regression.py
import unittest, pytest
import functionaltests.sample_unittest as sample_unittest

# set up the imported tests
suite_sample_unittest = sample_unittest.suite()

# create this test suite
suite = unittest.TestSuite()
suite.addTest(suite_sample_unittest)

# run the suite
unittest.TextTestRunner(verbosity=2).run(suite)

如果我从命令行对套件运行以下命令,则 test_first 运行(但我没有得到 py.test 将提供的附加信息):

python功能测试/suite_regression.py -v

如果我对套件运行以下命令,则会收集 0 个测试:

py.test 功能测试/suite_regression.py

如果我针对测试用例运行以下命令,则 test_first 和 test_second 运行:

py.test 功能测试/sample_unittest.py -v

我不明白用关键字做 py.test 将如何帮助将测试组织成套件。 将测试用例放入文件夹结构并使用文件夹选项运行 py.test 不会让我按功能区域组织测试。

所以我的问题:

  1. 是否有 py.test 机制以可重用的格式指定任意测试分组?
  2. 有没有办法从 py.test 使用 unittest.TestSuite?

编辑:所以我尝试了 py.test 标记,它让我用任意标签标记测试函数和测试方法,然后在运行时过滤该标签。

# conftest.py
import pytest

# set up custom markers
regression = pytest.mark.NAME
health = pytest.mark.NAME

我更新的测试用例:

# sample_unittest.py
import unittest, pytest

class TestClass(unittest.TestCase):

    def setUp(self):
        self.testdata = ['apple', 'pear', 'berry']

    @pytest.mark.healthcheck
    @pytest.mark.regression
    def test_first(self):
        assert 'apple' in self.testdata

    @pytest.mark.regression
    def test_second(self):
        assert 'pear' in self.testdata

    def tearDown(self):
        self.testdata = []

def suite():
    suite = unittest.TestSuite()
    suite.addTest(TestClass('test_first'))
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite()) 

因此运行以下命令收集并运行 test_first:

py.test 功能测试/sample_unittest.py -v -m 健康检查

这将收集并运行 test_first 和 test_second:

py.test 功能测试/sample_unittest.py -v -m 回归

所以回到我的问题:标记是一个部分解决方案,但我仍然没有办法控制收集的标记测试的执行。

在这种情况下不需要使用标记:在 py.test 测试类上设置@pytest.mark.incremental将强制执行顺序为声明顺序:

# sequential.py
import pytest

@pytest.mark.incremental
class TestSequential:

    def test_first(self):
        print('first')

    def test_second(self):
        print('second')

    def test_third(self):
        print('third')

现在运行它

pytest -s -v sequential.py

产生以下输出:

=========== test session starts ===========
collected 3 items

sequential.py::TestSequential::test_first first
PASSED
sequential.py::TestSequential::test_second second
PASSED
sequential.py::TestSequential::test_third third
PASSED
=========== 3 passed in 0.01 seconds ===========

我想现在有点晚了,但我刚刚完成了一个带有文档的交互式选择插件:

https://github.com/tgoodlet/pytest-interactive

我实际上使用了上面提到的钩子 Holger。

它允许您在使用 IPython 的收集阶段之后选择一组测试。 使用切片、下标或制表符完成对测试进行排序非常容易,如果这是您所追求的。 请注意,它是一个交互式工具,用于在开发过程中使用,而不是用于自动回归运行。

对于使用标记的持久排序,我使用了pytest-ordering这实际上非常有用,特别是如果您在长回归套件中有基线先决条件测试。

目前没有直接的方法来控制测试执行的顺序。 FWIW,有一个插件钩子pytest_collection_modifyitems可以用来实现一些东西。 有关使用它来实现随机化的插件,请参阅https://github.com/klrmn/pytest-random/blob/master/random_plugin.py

我知道这是旧的,但这个库似乎允许操作员正在寻找的东西......将来可能会帮助某人。

https://pytest-ordering.readthedocs.io/en/develop/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM