栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytest常用参数_pytest运行命令?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytest常用参数_pytest运行命令?

命令行参数使用

前言tb 参数

可选参数使用示例 durations参数

使用示例 setup-show参数

使用示例


前言

本篇来学习pytest命令行执行时,几个很有用的参数 tb 参数

作用:可以设置报错的时候回溯打印内容,可以设置参数(auto/long/short/line/native/no) 可选参数

–tb=auto 有多个用例失败的时候,只打印第一个和最后一个用例的回溯信息
–tb=long 输出最详细的回溯信息
–tb=short 输入assert的一行和系统判断内容
–tb=line 使用一行显示错误信息
–tb=native 只输出python标准库的回溯信息
–tb=no 不显示回溯信息

使用示例

不加参数,显示整个用例信息

# -*- coding: utf-8 -*-
# @Time    : 2022/3/19
# @Author  : 大海

import os


def test_01():
    sum_result = 1 + 2
    expected = 2
    assert sum_result == expected


if __name__ == '__main__':
    os.system('pytest -s test_49.py')

–tb=no 不显示回溯信息

# -*- coding: utf-8 -*-
# @Time    : 2022/3/19
# @Author  : 大海

import os


def test_01():
    sum_result = 1 + 2
    expected = 2
    assert sum_result == expected


if __name__ == '__main__':
    os.system('pytest -s test_49.py --tb=no')

–tb=line 使用一行输出所有的错误信息

# -*- coding: utf-8 -*-
# @Time    : 2022/3/19
# @Author  : 大海

import os


def test_01():
    sum_result = 1 + 2
    expected = 2
    assert sum_result == expected


if __name__ == '__main__':
    os.system('pytest -s test_49.py --tb=line')

durations参数

作用:统计出每个用例运行的时间 使用示例

–durations=0 显示全部用例的运行时间

# -*- coding: utf-8 -*-
# @Time    : 2022/3/19
# @Author  : 大海

import os
import pytest
import time


@pytest.fixture()
def set_up_fixture():
    time.sleep(0.1)
    yield
    time.sleep(0.2)


def test_01(set_up_fixture):
    print("用例1")
    time.sleep(1.0)


def test_02(set_up_fixture):
    print("用例2")
    time.sleep(0.6)


def test_03(set_up_fixture):
    print("用例3")
    time.sleep(1.2)


def test_04(set_up_fixture):
    print("用例4")
    time.sleep(0.3)


def test_05(set_up_fixture):
    print("用例5")
    time.sleep(2.3)


if __name__ == '__main__':
    # --durations=0 显示所有用例执行时间
    os.system('pytest -s test_50.py --durations=0')

用例运行的时候会经历3个阶段:setup,call,teardown。call就是测试用例,setup和teardown就是用例的fixture部分
–durations=N 显示最慢的N条用例

# -*- coding: utf-8 -*-
# @Time    : 2022/3/19
# @Author  : 大海

import os
import pytest
import time


@pytest.fixture()
def set_up_fixture():
    time.sleep(0.1)
    yield
    time.sleep(0.2)


def test_01(set_up_fixture):
    print("用例1")
    time.sleep(1.0)


def test_02(set_up_fixture):
    print("用例2")
    time.sleep(0.6)


def test_03(set_up_fixture):
    print("用例3")
    time.sleep(1.2)


def test_04(set_up_fixture):
    print("用例4")
    time.sleep(0.3)


def test_05(set_up_fixture):
    print("用例5")
    time.sleep(2.3)


if __name__ == '__main__':
    # --durations=2 显示执行最慢的两条
    os.system('pytest -s test_50.py --durations=2')

setup-show参数

作用:显示fixture 的执行过程. 使用示例

# -*- coding: utf-8 -*-
# @Time    : 2022/3/19
# @Author  : 大海

import os
import pytest


@pytest.fixture()
def login():
    print("前置操作:准备数据")
    yield
    print("后置操作:清理数据")


def test_01(login):
    a = 1
    b = 1
    assert a == b


def test_02(login):
    a = "hell"
    b = "hello 大海"
    assert a in b


if __name__ == '__main__':
    os.system('pytest -s test_51.py --setup-show')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/782873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号