我想将输入作为命令行选项传递给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 [email protected]()def get_input1(input1): print 'input1:',input1 return input1# @[email protected]()def get_input2(input2): print 'input2:',input2 return input2def 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.
最佳答案您可以通过以下方式使其工作:conftest.py:
import pytestdef pytest_addoption(parser): parser.addoption("--input1",default="default input1") parser.addoption("--input2",default="default input2")@pytest.fixturedef input1(request): return request.config.getoption("--input1")@pytest.fixturedef input2(request): return request.config.getoption("--input2")
test.py:
import [email protected] test_print_name(input1,input2): print ("displaying input1: %s" % input1) print("displaying input2: %s" % input2)
Cli:
>py.test -s test.py --input1 tt --input2 12================================================= test session starts =================================================platform win32 -- Python 3.7.0,pytest-4.1.1,py-1.7.0,pluggy-0.8.1rootdir: pytest,inifile:collected 1 itemtest.py displaying input1: ttdisplaying input2: 12.============================================== 1 passed in 0.04 seconds ===============================================
总结 以上是内存溢出为你收集整理的python-如何使用命令行在pytest中传递多个参数? 全部内容,希望文章能够帮你解决python-如何使用命令行在pytest中传递多个参数? 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)