pytest-命令行传入自定义的参数到测试文件中

pytest-命令行传入自定义的参数到测试文件中

conftest.py

import pytest

def pytest_addoption(parser):
    parser.addoption("--aa", action="store", default='', help="the input params")
    # parser.addoption("--outdir", action="store", default="~/benchmark/model/", type=str, 
    #                  help="the outdir of obj")

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

test.py

import pytest

def test_answer(aa):
    # if a == "type1":    #获取命令行传参的值后进行判断
    #     print("first")
    # elif a == "type2":
    #     print("second")
    # assert 0  # to see what was printed
    if aa == " " or aa == "":
        exit -1
    else:
        print('\n')
        print(12312412412)
        print(aa)
        print(type(aa))

# if __name__ == "__main__":
#     pytest.main(["-s", "test.py"])

https://www.it-swarm.cn/zh/python/如何通过命令行在pytest中传递参数/829127083/

posted @ 2021-12-10 15:21  michaelchengjl  阅读(455)  评论(0编辑  收藏  举报