如何在python中一次性输入多个参数_python-如何使用命令行在pytest中传递多个参数?...

我想将输入作为命令行选项传递给pytest文件. https://stackoverflow.com/a/42145604/8031479这个问题很有帮助,但我不知道添加多个解析器采用.

我尝试将其添加到我的conftest.py文件中,但没有帮助:

def pytest_addoption(parser):

"""

This function is used to extract input1 and input2 values from the command line

"""

parser.addoption(

"--input1",action="store",default="input1"

)

parser.addoption(

"--input2",default="input2"

)

我的test.py文件的内容:

import pytest

@pytest.fixture()

def get_input1(input1):

print 'input1:',input1

return input1

# @pytest.mark.unit

@pytest.fixture()

def get_input2(input2):

print 'input2:',input2

return input2

def test_hello(get_input1,get_input2):

print 'testing pytest fixtures with command line options'

print get_input1,get_input2

这是我运行test.py文件的命令:

py.test test.py --input1="hello" --input2="world"

我收到此错误消息:

@pytest.fixture()

def get_input1(input1):

E fixture 'input1' not found

> available fixtures: cache,capfd,capfdbinary,caplog,capsys,capsysbinary,doctest_namespace,get_input1,get_input2,metadata,monkeypatch,pytestconfig,record_property,record_xml_attribute,recwarn,tmp_path,tmp_path_factory,tmpdir,tmpdir_factory

> use 'pytest --fixtures [testpath]' for help on them.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`pytest.main()` 是 `pytest` 框架用来从命令行启动测试会话的一个函数。通常情况下,`pytest` 会自动收集和执行测试用例,但如果你需要在命令行向 `pytest` 传递参数,可以通过 `pytest.main()` 函数的 `args` 参数来实现。 对于 `pytest.fixture`,这是 `pytest` 框架用于设置测试环境的一个装饰器,它定义了可以被测试函数使用的资源。如果你想在调用 `pytest.main()` 时向 fixture 传递参数,你可以使用 `pytest马克`(Marks)功能来实现。Marks 允许你定义参数化的 fixture 或者为 fixture 添加条件标志。 以下是如何在 `pytest.main()` 使用 `args` 参数向 fixture 传递参数的一个例子: ```python import pytest @pytest.fixture(scope="session") def my_fixture(request): # 这里可以根据请求参数来改变 fixture 的行为 param_value = getattr(request, 'param', None) # 根据 param_value 进行一些操作... yield param_value if __name__ == "__main__": # 通过命令行参数向 fixture 传递参数 # 例如,你想向 fixture 传递一个特定的参数pytest.main(['-m', 'my_fixture=desired_value']) ``` 在这个例子,我们定义了一个名为 `my_fixture` 的 fixture,并通过命令行参数 `my_fixture=desired_value` 来指定要传递给它的参数值。`-m` 选项用于指定一个标记,这里的标记就是 fixture 的名称 `my_fixture`,其后跟随的等号和参数值表示传递给该 fixture 的参数。 请注意,这种通过命令行参数传递值给 fixture 的方式,要求你能够在测试代码相应地处理这些参数,例如通过访问 `request.param` 属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值