如何在夹具外部使用安装夹具中的变量
How to use a variable from setup fixture outside fixture
我想在 setupone
夹具外部使用产品变量。我想在 test_title.py in skipif
标记中使用它进行条件跳过(运行 测试用例,如果产品名称满足)。
conftest.py
def pytest_addoption(parser):
parser.addoption("--browser_name", action="store", default="chrome")
parser.addoption("--env", action="store", default="Production")
parser.addoption("--product_name", nargs = '*', type = str, action ="store", default="chitale")
products_list = getProductList()
@pytest.fixture(scope='class', params = products_list)
def setupone(request):
browser = request.config.getoption("browser_name")
env_name = request.config.getoption("env")
product = request.param
(getProductList() returns ["product1", "product2", "product3"] 存储在 product_list)
BaseOne.py
@pytest.mark.usefixtures("setupone")
class BaseOne:
pass
test_title.py
from conftest import prod
class TestStoreTitle(BaseOne):
@pytest.mark.skipif(prod not in ["product1", "product2", "product3"], reason="test case doesn't apply for desired product")
def test_Store_Title(self):
pass
我想在函数之外使用 setupone
的产品,例如:
def pytest_addoption(parser):
parser.addoption("--browser_name", action="store", default="chrome")
parser.addoption("--env", action="store", default="Production")
parser.addoption("--product_name", nargs = '*', type = str, action ="store", default="chitale")
products_list = getProductList()
@pytest.fixture(scope='class', params = products_list)
def setupone(request):
browser = request.config.getoption("browser_name")
env_name = request.config.getoption("env")
product = request.param
prod = product
另一种方法是仅从其固定装置访问产品 setupone
。
- Return 夹具的产品价值
setupone
- 定义一个 autouse-fixture,它将通过 get_closest_marker() 自动处理产品的跳过标记,下面是
skip_product_not_in_list
- 将求购商品列表传给大关
skip_product_not_in_list
- 如果
setupone
的当前产品不在想要的列表中,请通过 pytest.skip() 跳过它
import pytest
products_list = ["product1", "product2", "product3"]
@pytest.fixture(scope='class', params=products_list)
def setupone(request):
product = request.param
return product
@pytest.fixture(autouse=True)
def skip_non_applicable_products(request, setupone):
if product_marker := request.node.get_closest_marker("skip_product_not_in_list"):
if setupone not in product_marker.args[0]:
# Option 1
# pytest.skip(product_marker.kwargs.get('reason'))
# Option 2
pytest.skip(f"{product_marker.kwargs.get('reason') or 'Skipped'} {setupone}")
def test_Store_Title_all_products(setupone):
print("test_Store_Title_all_products:", setupone)
@pytest.mark.skip_product_not_in_list(["product1", "product3"], reason="test case doesn't apply for desired product")
def test_Store_Title_skip_products(setupone):
print("test_Store_Title_skip_products:", setupone)
输出
$ pytest -rPS test_src.py
============================================== PASSES ===============================================
______________________________ test_Store_Title_all_products[product1] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_all_products: product1
_____________________________ test_Store_Title_skip_products[product1] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_skip_products: product1
______________________________ test_Store_Title_all_products[product2] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_all_products: product2
______________________________ test_Store_Title_all_products[product3] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_all_products: product3
_____________________________ test_Store_Title_skip_products[product3] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_skip_products: product3
====================================== short test summary info ======================================
SKIPPED [1] test_src.py:19: test case doesn't apply for desired product product2
============================== 5 passed, 1 skipped, 1 warning in 0.08s ==============================
- 对于
test_Store_Title_all_products
,它测试了所有产品。对于我们定义自定义列表的 test_Store_Title_skip_products
,我们可以看到它跳过了 "product2"
,因为它不在列表中。
相关问题:
我想在 setupone
夹具外部使用产品变量。我想在 test_title.py in skipif
标记中使用它进行条件跳过(运行 测试用例,如果产品名称满足)。
conftest.py
def pytest_addoption(parser):
parser.addoption("--browser_name", action="store", default="chrome")
parser.addoption("--env", action="store", default="Production")
parser.addoption("--product_name", nargs = '*', type = str, action ="store", default="chitale")
products_list = getProductList()
@pytest.fixture(scope='class', params = products_list)
def setupone(request):
browser = request.config.getoption("browser_name")
env_name = request.config.getoption("env")
product = request.param
(getProductList() returns ["product1", "product2", "product3"] 存储在 product_list)
BaseOne.py
@pytest.mark.usefixtures("setupone")
class BaseOne:
pass
test_title.py
from conftest import prod
class TestStoreTitle(BaseOne):
@pytest.mark.skipif(prod not in ["product1", "product2", "product3"], reason="test case doesn't apply for desired product")
def test_Store_Title(self):
pass
我想在函数之外使用 setupone
的产品,例如:
def pytest_addoption(parser):
parser.addoption("--browser_name", action="store", default="chrome")
parser.addoption("--env", action="store", default="Production")
parser.addoption("--product_name", nargs = '*', type = str, action ="store", default="chitale")
products_list = getProductList()
@pytest.fixture(scope='class', params = products_list)
def setupone(request):
browser = request.config.getoption("browser_name")
env_name = request.config.getoption("env")
product = request.param
prod = product
另一种方法是仅从其固定装置访问产品 setupone
。
- Return 夹具的产品价值
setupone
- 定义一个 autouse-fixture,它将通过 get_closest_marker() 自动处理产品的跳过标记,下面是
skip_product_not_in_list
- 将求购商品列表传给大关
skip_product_not_in_list
- 如果
setupone
的当前产品不在想要的列表中,请通过 pytest.skip() 跳过它
import pytest
products_list = ["product1", "product2", "product3"]
@pytest.fixture(scope='class', params=products_list)
def setupone(request):
product = request.param
return product
@pytest.fixture(autouse=True)
def skip_non_applicable_products(request, setupone):
if product_marker := request.node.get_closest_marker("skip_product_not_in_list"):
if setupone not in product_marker.args[0]:
# Option 1
# pytest.skip(product_marker.kwargs.get('reason'))
# Option 2
pytest.skip(f"{product_marker.kwargs.get('reason') or 'Skipped'} {setupone}")
def test_Store_Title_all_products(setupone):
print("test_Store_Title_all_products:", setupone)
@pytest.mark.skip_product_not_in_list(["product1", "product3"], reason="test case doesn't apply for desired product")
def test_Store_Title_skip_products(setupone):
print("test_Store_Title_skip_products:", setupone)
输出
$ pytest -rPS test_src.py
============================================== PASSES ===============================================
______________________________ test_Store_Title_all_products[product1] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_all_products: product1
_____________________________ test_Store_Title_skip_products[product1] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_skip_products: product1
______________________________ test_Store_Title_all_products[product2] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_all_products: product2
______________________________ test_Store_Title_all_products[product3] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_all_products: product3
_____________________________ test_Store_Title_skip_products[product3] ______________________________
--------------------------------------- Captured stdout call ----------------------------------------
test_Store_Title_skip_products: product3
====================================== short test summary info ======================================
SKIPPED [1] test_src.py:19: test case doesn't apply for desired product product2
============================== 5 passed, 1 skipped, 1 warning in 0.08s ==============================
- 对于
test_Store_Title_all_products
,它测试了所有产品。对于我们定义自定义列表的test_Store_Title_skip_products
,我们可以看到它跳过了"product2"
,因为它不在列表中。
相关问题: