我寫了這個簡單的引數化夾具
@pytest.fixture(params=[
pytest.lazy_fixture("client_a"),
pytest.lazy_fixture("client_b"),
pytest.lazy_fixture("client_c"),
])
def client_all(request):
return request.param
當放入測驗檔案時,它可以完美運行。當我將它移動到 conftest.py 時,結果是:
加載 conftest '/project/tests/conftest.py' 時匯入錯誤。conftest.py:181: 在???E AttributeError: 模塊 'pytest' 沒有屬性 'lazy_fixture'
顯然除錯 pytest 的插件管理器是不可行的,但我注意到當我收到這個錯誤時,pytest_configure里面的函式pytest-lazy-fixture永遠不會被呼叫,但是當夾具作業時它會被呼叫,所以問題一定是在加載插件串列時。我希望它與其他一些 pytest 選項有一些奇怪的沖突,但我不知道如何從這里向前推進。
請注意,該插件在整個專案的其他檔案中使用,但僅用于對單個測驗進行引數化,而從不作為夾具引數。
uj5u.com熱心網友回復:
看來是個老問題了。pytest_namespace鉤子在 pytest 4.0 中被洗掉,這意味著因為pytest.lazyfixture它是一個插件,它只在conftest.py. lazy_fixture直接匯入即可解決
from pytest_lazyfixture import lazy_fixture
@pytest.fixture(params=[
lazy_fixture("client_a"),
lazy_fixture("client_b"),
lazy_fixture("client_c"),
])
def client_all(request):
return request.param
uj5u.com熱心網友回復:
插件是在 conftest 檔案和夾具加載后配置的。
因此,在注冊全域裝置時,pytest.lazy_fixture命名空間尚不存在,因為對 的呼叫pytest_configure尚未發生。
正確的使用方法是從它自己的模塊本身加載它:
from pytest_lazyfixture import lazy_fixture
@pytest.fixture(params=[
lazy_fixture("client_a"),
lazy_fixture("client_b"),
lazy_fixture("client_c"),
])
def client_all(request):
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352678.html
