pytest文檔68-pytest-lazy-fixture 插件解決 pytest.mark.parametrize 中使用 fixture 問題


前言

測試用例參數化的時候,使用 pytest.mark.parametrize 參數化傳測試數據,如果我們想引用前面 不同fixture 返回的數據當測試用例的入參,前面一篇用fixture 參數化 prams 來間接解決這個問題。
接下來用 pytest-lazy-fixture 插件可以直接在測試用例中參數化時 pytest.mark.parametrize 中使用 fixture

pytest-lazy-fixture 插件

pytest-lazy-fixture 插件是為了解決測試用例中用 @pytest.mark.parametrize 參數化調用fixture的問題,先pip安裝

pip install pytest-lazy-fixture

目前使用的版本是 0.6.3

>pip show pytest-lazy-fixture
Name: pytest-lazy-fixture
Version: 0.6.3
Summary: It helps to use fixtures in pytest.mark.parametrize
Home-page: https://github.com/tvorog/pytest-lazy-fixture
Author: Marsel Zaripov
Author-email: marszaripov@gmail.com
License: MIT
Location: e:\python36\lib\site-packages
Requires: pytest
Required-by:

parametrize 使用示例

參數化的時候,其中一些測試數據,來源於前面的 fixture

import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/


@pytest.fixture(params=[1, 2])
def one(request):
    return request.param


@pytest.mark.parametrize('arg1,arg2', [
    ('val1', pytest.lazy_fixture('one')),
])
def test_func(arg1, arg2):
    print(arg1, arg2)
    assert arg2 in [1, 2]

運行結果

..\test_y.py val1 1
.val1 2
.

============== 2 passed in 0.04 seconds ===========

fixture 參數化 params

在 fixture 參數化的 params 中也可以使用

import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

@pytest.fixture
def one():
    return 1


@pytest.fixture
def two():
    return 2


@pytest.fixture(params=[
    pytest.lazy_fixture('one'),
    pytest.lazy_fixture('two')
])
def some(request):
    return request.param


def test_func(some):
    assert some in [1, 2]

pytest-lazy-fixture 相關的使用可以查看github 地址https://github.com/TvoroG/pytest-lazy-fixture


免責聲明!

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



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