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

标签 python unit-testing python-2.7 pytest

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

测试套件将绑定(bind)到 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 functionaltests/suite_regression.py -v

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

py.test functionaltests/suite_regression.py

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

py.test functionaltests/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 functionaltests/sample_unittest.py -v -m healthcheck

这会收集并运行 test_first 和 test_second:

py.test functionaltests/sample_unittest.py -v -m regression

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

最佳答案

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

# 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 ===========

关于python - 使用 py.test 运行测试套件(任意测试集合),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691587/

相关文章:

在 Appengine 上使用 Django 上传 JQuery 文件

python - 向 python 子进程发送复杂的 shell 命令

python-2.7 - 在python中使用Abs查找函数的导数

python - Flask流无法在我的计算机上运行

python - 将列表中的嵌套字典复制到新字典

java - getRequests() 必须返回一个可迭代的数组

java - Hibernate,对用户类型的简单依赖注入(inject)

python - 在 Python 单元测试中分别模拟多个 API 请求

python-2.7 - 在 Python : LinAlgError 中建模时检测 mulicollinear 或具有线性组合的列

python - 是否可以跨多行打破长函数名?