运行自定义命令行参数
1.首先需要在conftest.py添加命令行选项,命令行传入参数”—cmd“, 用例如果需要用到从命令行传入的参数,就调用cmd函数:
# conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption('--cmd', action='store', default='type2',
help='type1 or type2')
@pytest.fixture()
def cmdopt(request):
return request.config.getoption('--cmd')
# test_para.py
import pytest
def test_05(cmdopt):
if cmdopt == 'type1':
print('第一')
elif cmdopt == 'type2':
print('第二')
cmd打开,输入指令启动,也可以在pycharm里面右键执行上面代码
> pytest test_para.py --cmd=type1 或 > pytest test_para.py --cmd type1
上边的两种写法都可以,实例中的 ‘--cmd’ 是自定义的,自定义后可以通过 pytest -h 来查看, help = 后边跟的是参数的解释
配置文件pytest.ini
pytest 配置文件可以改变 pytest 的运行方式,它是一个固定的文件 pytest.ini 文件,读取配置信息,按指定的方式去运行
用 pytest -h 指令可以查看 pytest.ini 的设置选项
mark 标记
我们之前通过mark标记了一些函数,但是有的时候我们标记的过多,就会忘记了一些标记下都是什么case,为了之后我们能精准的运行mark标记,我们可以写入到pytest.ini文件中:
# pytest.ini
[pytest]
markers =
webtest: seek python
w: Open the space
我们写好后就可以通过命令来查看了
> pytest --markers
@pytest.mark.webtest: seek python
@pytest.mark.w: Open the space
@pytest.mark.skip(reason=None): skip the given test function with an optional reason. Example: skip(reason="no way of currently testing this") skips the test.
@pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see http://pytest.org/latest/skipping.html
@pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False): mark the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expected, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See http://pytest.org/latest/skipping.html
@pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see http://pytest.org/latest/parametrize.html for more info and examples.
@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures
@pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible.
@pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.
通过命令 pytest --markers 返回来的结果,我们可以发现前两行就是我们刚刚写入到pytest.ini的配置了
addopts
addopts 参数可以更改默认命令行选顷,这个当我们在 cmd 输入指令去执行用例的时候,会用到
例如我们要运行标记为 w 的函数,正常我们直接命令运行 > pytest -m w
我们可以直接在pytest.ini中配置
[pytest]
markers =
webtest: seek python
w: Open the space
addopts= -m w
这样,我们下次命令运行时,直接 > pytest 运行就可以运行标记为 w 的函数了