在相對較大的測試套件中,您很可能需要用本地定義的??fixture
??覆蓋全局或根??fixture
??,以保持測試代碼的可讀性和可維護性。
假設測試文件結(jié)構(gòu)是:
tests/
__init__.py
conftest.py
# content of tests/conftest.py
import pytest
@pytest.fixture
def username():
return 'username'
test_something.py
# content of tests/test_something.py
def test_username(username):
assert username == 'username'
subfolder/
__init__.py
conftest.py
# content of tests/subfolder/conftest.py
import pytest
@pytest.fixture
def username(username):
return 'overridden-' + username
test_something.py
# content of tests/subfolder/test_something.py
def test_username(username):
assert username == 'overridden-username'
正如您所看到的,具有相同名稱的??fixture
??可以為特定的測試文件夾級別重寫。請注意,可以從上面示例中使用的覆蓋??fixture
??輕松訪問基?本?fixture
??或超級??fixture
??
假設測試文件結(jié)構(gòu)是:
tests/
__init__.py
conftest.py
# content of tests/conftest.py
import pytest
@pytest.fixture
def username():
return 'username'
test_something.py
# content of tests/test_something.py
import pytest
@pytest.fixture
def username(username):
return 'overridden-' + username
def test_username(username):
assert username == 'overridden-username'
test_something_else.py
# content of tests/test_something_else.py
import pytest
@pytest.fixture
def username(username):
return 'overridden-else-' + username
def test_username(username):
assert username == 'overridden-else-username'
在上面的示例中,可以為某些測試模塊重寫具有相同名稱的?fixture
?。
假設測試文件結(jié)構(gòu)是:
tests/
__init__.py
conftest.py
# content of tests/conftest.py
import pytest
@pytest.fixture
def username():
return 'username'
@pytest.fixture
def other_username(username):
return 'other-' + username
test_something.py
# content of tests/test_something.py
import pytest
@pytest.mark.parametrize('username', ['directly-overridden-username'])
def test_username(username):
assert username == 'directly-overridden-username'
@pytest.mark.parametrize('username', ['directly-overridden-username-other'])
def test_username_other(other_username):
assert other_username == 'other-directly-overridden-username-other'
在上面的示例中,??fixture
??值被測試參數(shù)值覆蓋。請注意,即使測試沒有直接使用??fixture
??的值(在函數(shù)原型中沒有提到它),也可以通過這種方式重寫它。
假設測試文件結(jié)構(gòu)是:
tests/
__init__.py
conftest.py
# content of tests/conftest.py
import pytest
@pytest.fixture(params=['one', 'two', 'three'])
def parametrized_username(request):
return request.param
@pytest.fixture
def non_parametrized_username(request):
return 'username'
test_something.py
# content of tests/test_something.py
import pytest
@pytest.fixture
def parametrized_username():
return 'overridden-username'
@pytest.fixture(params=['one', 'two', 'three'])
def non_parametrized_username(request):
return request.param
def test_username(parametrized_username):
assert parametrized_username == 'overridden-username'
def test_parametrized_username(non_parametrized_username):
assert non_parametrized_username in ['one', 'two', 'three']
test_something_else.py
# content of tests/test_something_else.py
def test_username(parametrized_username):
assert parametrized_username in ['one', 'two', 'three']
def test_username(non_parametrized_username):
assert non_parametrized_username == 'username'
在上面的例子中,參數(shù)化的??fixture
??被非參數(shù)化版本覆蓋,非參數(shù)化的??fixture
??被某個測試模塊的參數(shù)化版本覆蓋。這同樣適用于測試文件夾級別。
更多建議: