Autotest

pytest 命令执行添加自定义参数

2020-12-08  本文已影响0人  绛珠仙靖

1.创建conftest.py

import pytest
from funpinpin_api.util.para import action

def pytest_addoption(parser):
    test_api = "test"
    test_product_url = action['test_product']
    parser.addoption ( "--test_api", default=test_api )
    parser.addoption ( "--test_product_url", default=test_product_url )


@pytest.fixture
def test_api(request):
    return request.config.getoption ( "--test_api" )

@pytest.fixture
def test_product_url(request):
    return request.config.getoption ( "--test_product_url" )

Example:

import pytest
class Test_aaa():
    def test_train(self,test_api,test_product_url):
        print(test_apii)
        print(test_product_url)
        assert 1 == 1

pytest.main ( "-v -s  buy.py::Test_aaa::test_train  --test_api=xyz  --test_product_url=ttt  --alluredir  ./report "  )

#Result
collected 1 item
buy.py::Test_aaa::test_train xyz
ttt
PASSED

批量执行所有test文件,不传值,也会执行设置的默认值
pytest.main ( "-v -s buy.py::Test_aaa --alluredir ./report " )

上一篇下一篇

猜你喜欢

热点阅读