簡體   English   中英

跳過模塊級pytest_generate_tests生成的參數化測試

[英]Skip parametrized tests generated by pytest_generate_tests at module level

我希望能夠從配置文件中參數化幾個測試,但同時能夠跳過這些測試,除非發出特定的命令選項。

我可以通過在測試模塊的頂部添加以下代碼來跳過測試:

from json import loads
import pytest
@pytest.mark.skipif(pytest.config.getvalue("-k") != "smoke",
                    reason="Smoke tests must be explicitly launched through -k smoke option")

發出py.testpython -m pytest時不執行測試,除非添加了-k smoke選項。

我還可以通過以下方式從配置文件創建參數化測試:

def pytest_generate_tests(metafunc):
    with open('tests/test_smoke.json','r') as fp:
        confs = loads(fp.read().decode("utf-8-sig"))

        for arg in metafunc.funcargnames:
            if arg == "conf":
                metafunc.parametrize("conf",confs)

參數化的測試示例如下:

def test_that_require_conf(conf):
    assert not conf

問題是兩件事情都不能很好地協同工作。 使用pytest_generate_tests時不會跳過測試。 如果我在pytest_generate_tests中添加一個選項以避免參數化,那么調用yo pytest失敗,因為test_that_require_conf所需的conf fixture。

有關如何實現這一點的任何想法?

我看到兩個選項:(我認為你的選項存儲為smoke

1)在第一個選項中,您需要更改pytest_generate_tests 測試將作為一個跳過

def pytest_generate_tests(metafunc):
    for arg in metafunc.funcargnames:
         if arg == "conf":
            if metafunc.config.option.keyword != 'smoke':
                confs = pytest.skip("Smoke tests must....")
            else:
                with open('tests/test_smoke.json', 'r') as fp:
                    confs = loads(fp.read().decode("utf-8-sig"))

            metafunc.parametrize("conf", confs)

輸出將是:

collected 0 items / 1 skipped

==================== 1 skipped in 0.01 seconds ========================

2)第二個選項將單獨跳過任何測試

def test_that_require_conf(request, conf):
    if request.config.option.smoke != 'smoke':
        pytest.skip('Smoke tests must....")
    assert conf

輸出會

collected 3 items

tests/test_2.py::test_that_require_conf[1] SKIPPED
tests/test_2.py::test_that_require_conf[2] SKIPPED
tests/test_2.py::test_that_require_conf[3] SKIPPED

====================== 3 skipped in 0.02 seconds ======================

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM