pytest常用命令参数

pytest 参数

1.参数:-s

运行过程中执行print打印函数:pytest -s,以下两个输出 上边带参数,下边不带

2.参数: --collect-only 

收集将要执行的用例,但不会执行用例:pytest --collcet-onty

3.参数:-k args(关键字args:可以是py文件名,也可以是函数名,若py文件名和函数均包含 则需要严格指定 xx.py 以运行py文件)

运行包含关键词的用例:pytest -k "install",如下图:

4.参数:-x

用例运行失败则立即停止执行

5.参数:--maxfail=num

用例运行时 允许的最大失败次数,超过则立即停止,pytest --maxfail=3

6.参数:--tb=选项(选项:'auto', 'long', 'short', 'no', 'line', 'native')

用例运行失败时,展示错误的详细程度

7.参数:-l 或--showlocals

用例运行失败时,打印相关的局部变量,pytest -l

8.参数:-v 或 -q

打印用例执行的详细/简略过程,pytest -v ,pytest -q

9.参数:--lf / --last-failed

只执行上次执行失败的测试

10.参数:--ff / --failed-first

先执行完上次失败的测试后,再执行上次正常的测试

11.参数:--durations=num -vv(num为0时则倒序显示所有的用例,为具体值则显示耗时最长的对应该数量的用例,-vv 显示持续时间为0秒的用例)

会按用例执行耗时时长:从长到短 显示结果,用于调优测试代码

比如显示运行耗时最长的3个用例且包含持续时间为0秒的:pytest --durations=3 -vv

import allure
import pytest
import time
@allure.step('检查UI名:{0}打开了')
def ui_check(tips):
    return tips

@allure.feature('feature:功能名1')
@allure.story('story:1-子功能UI测试')
@allure.severity('normal')
@allure.issue('http://bug.report')#缺陷链接地址
@allure.testcase('http://testcase.com')#用例链接地址
#@allure.attach('sname',f,allure.attachment_type.PNG)
#这是用例标题-功能1-子功能UI测试
def test_call_check_ui():

    """
    用例描述:UI检查测试
    """
    time.sleep(1)
    a,b=2,3
    print('我是print打印函数:UI检查测试')
    #assert ui_check('Main')=='Main'
    assert 6==a+b


@allure.feature('feature:功能名1')
@allure.story('story:1-子功能逻辑测试')
@allure.severity('critical')
#@pytest.mark.smoke
def test_app_logic():
    '''
    用例描述:逻辑测试
    '''
    print('逻辑测试')
    time.sleep(2)
    assert 1==1
View Code

12.运行指定的函数(使用两对冒号 : 分隔)

pytest 模块名::类名::函数名,pytest test.py::check_ui

 13.参数:-r option

生成简略的指定需求的报告

option f E s x X P  p a A
Des failed error xkipped xfailed xpassed passed passed with output all except pP all
原文地址:https://www.cnblogs.com/sc912/p/11369237.html