簡體   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