請考慮以下測試代碼,它將模擬運行結果與預期結果進行比較。運行結果的值取決於參數化夾具paramfixture的值,該參數提供了兩個值,因此有兩種可能的運行結果變量。由於它們都是會話裝置,因此我們應該期望run_result fixture只執行兩次。Pytest參數化會話夾具執行的次數太多
現在,請查看測試用例test_run_result,它接收要比較的run_result和expected_result燈具,並接收容差夾具,該夾具用兩個值進行參數化。測試用例檢查預期和結果之間的差異是否在容差範圍內。請注意,運行不依賴於容差。
由於某些原因,我不明白Pytest執行run_result()三次。你能解釋爲什麼嗎?
這是使用pytest進行測試。 2.9.1
順便說一句,如果測試用例沒有被參數化,或者使用decoractor而不是夾具進行參數化,那麼run_result夾具只會執行兩次,即:@ pytest.mark.parametrize('tolerance', [1e-8,1e-11])。
import pytest
runcounter = 0
@pytest.fixture(scope="session", params=[1e-8, 1e-11])
def tolerance(request):
"""Precision in floating point compare."""
return request.param
@pytest.fixture(scope='session', params=[1, 2])
def paramfixture(request):
return request.param
@pytest.fixture(scope="session")
def expected_result(paramfixture):
return 1 + paramfixture
@pytest.fixture(scope='session')
def run_result(paramfixture):
global runcounter
runcounter = runcounter + 1
print "Run #", runcounter, 'param:', paramfixture
return 1 + paramfixture
def test_run_result(run_result, expected_result, tolerance):
print "run_result: %d, expected_result: %d" % (run_result, expected_result)
assert abs(run_result - expected_result) < tolerance
Pytest截圖:
$ py.test -vs test/end2end/test_temp.py
===================================================== test session starts ======================================================
platform linux2 -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /home/f557010/.conda/envs/sfpdev/bin/python
cachedir: .cache
rootdir: /home/f557010/svndev/SFP, inifile: pytest.ini
collected 4 items
test/end2end/test_temp.py::test_run_result[1e-08-1] Run # 1 param: 1
run_result: 2, expected_result: 2
PASSED
test/end2end/test_temp.py::test_run_result[1e-08-2] Run # 2 param: 2
run_result: 3, expected_result: 3
PASSED
test/end2end/test_temp.py::test_run_result[1e-11-2]
run_result: 3, expected_result: 3
PASSED
test/end2end/test_temp.py::test_run_result[1e-11-1] Run # 3 param: 1
run_result: 2, expected_result: 2
PASSED
=================================================== 4 passed in 0.01 seconds ===================================================
您使用的是哪個版本的py.test?夾具只需要運行兩次,但會有四個測試排列:2個參數x 2個容差水平。 –