Python测试框架pytest:常用参数、查找子集、参数化、跳过

Pytest是一个基于python的测试框架,用于编写和执行测试代码。pytest主要用于API测试,可以编写代码来测试API、数据库、UI等。

pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个优点:

简单灵活,容易上手。pytest的语法简洁明了,易于理解和使用。

支持参数化。pytest可以通过装饰器或 fixture 方法对测试用例进行参数化,提高测试用例的覆盖率。

能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests)。

pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等。

测试用例的skip和xfail处理。pytest提供了灵活的跳过测试用例或预期失败的机制,可以根据需要在测试过程中跳过某个或某些测试用例。

可以很好的和jenkins集成。pytest可以和Jenkins等持续集成工具无缝集成,方便进行自动化测试和报告生成。

report框架----allure 也支持了pytest。pytest可以和Allure报告框架集成,生成详细的HTML测试报告,方便进行测试结果分析和展示。

pytest是一个功能强大、灵活易用的Python测试框架,适用于各种类型的测试需求,具有很高的实用价值。

安装

bash 复制代码
# 安装
pip install pytest

# 帮助
pytest -h

格式要求

文件名称:test_*.py 或 *_test.py

函数名:test开头

常用参数

-s 显示标准 输出,相当于--capture=no,pytest默认是不输出print logging等的输出的,除非assert失败。
-v 显示详细 报告。
-k 按照关键词 查找测试用例。
-q 显示简洁 报告。
-m 只运行被标记 的测试用例。
-x 用例失败时立即停止测试。
-c file从 file 加载配置文件。
-l (--showlocals) 用例失败信息回溯时显示局部变量及其值。
-rsxX 报告®测试用例被跳过(s)、预计失败(x)、预计失败但实际通过(X)的原因。
-strict禁止使用未在配置文件(pytest.ini)注册的 mark 标记。
--maxfail=n失败n后停止运行测试。
--reruns=num失败用例重跑num次。需要安装 pytest-rerunfailures 插件模块。
--lf (--last-failed)仅执行上次失败的用例。如果没有失败的用例或者没找到缓存文件,默认是运行所有的用例。
--lfnf =[all, none]--lf 同时使用,=all 代表找不到用例或缓存文件时执行所有用例,=none 代表找不到用 例或缓存文件时不执行测试用例。

python 复制代码
pytest.main(['--lf','--lfnf=none', "test.py"])

--ff (--failed-first)先执行失败的用例,再执行其他用例。
--nf (--new-first)首先从新文件或新修改的用例开始运行测试。
--sw (--stepwise)在测试失败时退出,且下一次在测试失败的用例开始测试。
--stepwise-skip忽略第一个失败的测试,在第二次测试失败时退出。
--keep-duplicates 不断重复的测试。
--durations=n显示执行最慢的n条用例。注意:除非添加参数 -vv,默认情况下,否则pytest不会显示<0.01s的测试时间。
--fixtures显示所有可用的 fixture。
--tb=style堆栈回溯信息打印模式 (auto/long/short/line/native/no])。
--setup-show显示fixture执行步骤。
--cache-show=[CACHESHOW]显示缓存内容,不执行收集或测试。
--cache-clear运行前清除pytest缓存。
--continue-on-collection-errors即使发生收集(收集用例阶段)错误,也强制执行测试。
--rootdir=ROOTDIR定义测试的根目录。
--color=color终端输出的颜色(yes/no/auto)。
--collect-only只收集用例,不执行。
--assert=MODE "plain"不执行任何断言调试,"rewrite"重写测试模块中的assert语句,以提供assert表达式信息

基础测试

文件名:test_one.py

python 复制代码
# 测试函数
def test_division():
    assert 1/1.0==1

# 测试类
class TestOne:
    def test_addition(self):
        """
        测试方法
        """
        assert 1 + 1 == 2

    def testsquare(self):
        """
        测试方法
        """
        assert 2*2 == 3
    
    def tesequality():
        """
        无效
        """
        assert 10 == 11

运行:

bash 复制代码
pytest -v

-v表示查看详情。

找到3个测试用例,1个失败,2个通过。

测试子集

按照函数名查找子集

test_sub.py

python 复制代码
# 测试子集
class TestSub:
    def test_compare_one(self):
        """
        测试方法
        """
        assert 1 + 1 == 2


    def test_compare_two(self):
        """
        测试方法
        """
        assert 1 + 2 == 3
bash 复制代码
pytest -v -k compare

使用pytest -k <substring>命令的-k参数值来过滤函数名。

分组标记

python 复制代码
# -*- coding: utf-8 -*-
import pytest

# 测试子集
class TestGroup:
    @pytest.mark.group
    def test_group_one(self):
        """
        测试方法
        """
        assert 1 + 1 == 2

    @pytest.mark.group
    def test_group_two(self):
        """
        测试方法
        """
        assert 1 + 2 == 3
bash 复制代码
pytest -v -m group

这里用装饰器 @pytest.mark.group来标记函数,然后用pytest -v -m group中的-m来寻找这个分组标记。

夹具函数

python 复制代码
import pytest

# 测试fixture
class TestFixture:
    @pytest.fixture
    def input_value(self):
        return 36


    def test_division(self, input_value):
        """
        测试方法
        """
        assert input_value / 6 == 6

这里用@pytest.fixture修饰的函数input_value提前准备了数据,以供test_division用。这种方法只能在一个文件里用,如果想全局使用可以配置Conftest.py

参数化

python 复制代码
import pytest

@pytest.mark.parametrize("num, output",[(1,11),(2,22),(3,35),(4,44)])
def test_multiplication_11(num, output):
   assert 11*num == output
bash 复制代码
pytest test_parame.py -v

跳过测试

python 复制代码
import pytest
@pytest.mark.xfail
@pytest.mark.great
def test_greater():
   num = 100
   assert num > 100

@pytest.mark.xfail
@pytest.mark.great
def test_greater_equal():
   num = 100
   assert num >= 100

@pytest.mark.skip
@pytest.mark.others
def test_less():
   num = 100
   assert num < 200
bash 复制代码
pytest test_xfail_skip.py -v

@pytest.mark.xfail标记为xfail状态。
@pytest.mark.skip直接跳过。

更多请见官网

相关推荐
Eiceblue2 小时前
Python读取PDF:文本、图片与文档属性
数据库·python·pdf
weixin_527550402 小时前
初级程序员入门指南
javascript·python·算法
程序员的世界你不懂2 小时前
Appium+python自动化(十)- 元素定位
python·appium·自动化
CryptoPP3 小时前
使用WebSocket实时获取印度股票数据源(无调用次数限制)实战
后端·python·websocket·网络协议·区块链
树叶@3 小时前
Python数据分析7
开发语言·python
老胖闲聊4 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
码界奇点4 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
浠寒AI4 小时前
智能体模式篇(上)- 深入 ReAct:LangGraph构建能自主思考与行动的 AI
人工智能·python
行云流水剑6 小时前
【学习记录】如何使用 Python 提取 PDF 文件中的内容
python·学习·pdf
心扬6 小时前
python生成器
开发语言·python