pytest的參數化


參數化有兩種方式:

1、

@pytest.mark.parametrize

2、利用conftest.py里的

pytest_generate_tests

 

1中的例子如下:

@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)]) def test_eval(test_input, expected): assert eval(test_input) == expected

 2中的例子(自己定義參數化,pytest_generate_tests 是在收集測試方法時會被調用)的:

conftest.py 

def pytest_addoption(parser): parser.addoption( "--stringinput", action="append", default=[], help="list of stringinputs to pass to test functions", ) def pytest_generate_tests(metafunc): if "stringinput" in metafunc.fixturenames: metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput"))

a_test.py
def test_valid_string(stringinput): assert stringinput.isalpha()

執行測試:
pytest -q --stringinput="hello" --stringinput="world" a_test.py


參數化參考地址:
https://docs.pytest.org/en/latest/parametrize.html#pytest-mark-parametrize-parametrizing-test-functions


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM