pytest的测试函数标记

1.使用命令中指定 -m marker_name运行
ep:@pytest.mark.smoke
@pytest.mark.get
$pytest -m 'smoke'
$pytest -m 'smoke and get'
$pytest -m 'smoke or get'
$pytesr -m 'smoke and not get'
2.skip 和skipif允许跳过一个不希望运行的测试
要给跳过的测试添加理由和条件,应当使用skipif代替skip
ep:@pytest.mark.skipif(tasks.__version__<'0.2.0', reason = 'not supported until version 0.2.0')
使用skip和skipif标记,测试会直接跳过,而不会被执行
3.使用xfail标记,告诉我们使用pytest运行测试,但运行会预期失败
运行结果:x代表XFAIL,意味着excepted to fail 预期失败,实际也失败
X代表XPASS,意味着exceped to fail but passed 预期失败,但实际运行并没有失败
4.运行单个测试函数,只需要在文件名后添加::符号和函数名
运行单个测试类,只需要在文件名后添加::符号和类名
如果不希望运行测试类中的所有测试,只想指定运行其中一个,一样可以在文件名后添加::符号和方法名
-k选项运许用一个表达式指定需要运行的测试,该表达式可以匹配测试名或其子串,表达式中可以包含and、or、not
5.参数话测试允许传递多组数据,一旦发现测试失败,pytest会及时报告
ep1:@pytest.mark.parametrize('task',
[Task('sleep',done=True),
Task('wake','brain'),
Task('brathe','BRIAN',True),
Task('exercise','BrIan',False)])
parameterize()函数的第一个参数’task‘是用逗号分隔的字符串列表,第二个参数是一个值列表,上面是一个Task对象列表
ep2:@pytest.mark.parametrize('summary,owner,done',
[('sleep',None,False),
('wake','brian',False),
('breathe','BRIAN',False),
('eat eggs','BrIan',False)
])
ep3:tasks_to_try = (Task('sleep',done=True),
Task('wake','brain'),
Task('wake','brain'),
Task('brathe','BRIAN',True),
Task('exercise','BrIan',False))
@pytest.mark.parametrize('task',tasks_to_try)
如果标识中包含空格,需要添加引号
在给@pytest.mark.parameterize()装饰器传入列表参数时,还可以在参数值旁边定义一个id来做标记,语法是pytest.param(<value>,id='something'),例如:
@pytest.mark.parametrize('task',[
pytest.param(Task('create'),id='just summary'),
pytest.param(Task('inspire','Michelle'),id='summary/owner'),
pytest.param(Task('encourage','Michelle',True),id='summary/owner/done')])

猜你喜欢

转载自www.cnblogs.com/drug/p/10521648.html