加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何通过命令行在pytest中传递参数

发布时间:2020-12-20 10:34:02 所属栏目:Python 来源:网络整理
导读:我有一个代码,我需要传递像终端名称这样的参数. 这是我的代码以及如何传递参数.我收到一个“文件未找到”的错误,我不明白. 我在终端中尝试了命令:pytest filename .py -almonds 我应该把这个名字打印成“杏仁” @pytest.mark.parametrize("name")def print_
我有一个代码,我需要传递像终端名称这样的参数.
这是我的代码以及如何传递参数.我收到一个“文件未找到”的错误,我不明白.

我在终端中尝试了命令:pytest< filename> .py -almonds
我应该把这个名字打印成“杏仁”

@pytest.mark.parametrize("name")
def print_name(name):
    print ("Displaying name: %s" % name)

解决方法

在你的pytest测试中,不要使用@ pytest.mark.parametrize:

def test_print_name(name):
    print ("Displaying name: %s" % name)

在conftest.py中:

def pytest_addoption(parser):
    parser.addoption("--name",action="store",default="default name")


def pytest_generate_tests(metafunc):
    # This is called for every test. Only get/set command line arguments
    # if the argument is specified in the list of test "fixturenames".
    option_value = metafunc.config.option.name
    if 'name' in metafunc.fixturenames and option_value is not None:
        metafunc.parametrize("name",[option_value])

然后,您可以使用命令行参数从命令行运行:

pytest -s tests/my_test_module.py --name abc

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读