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测试框架,适用于各种类型的测试需求,具有很高的实用价值。

安装

# 安装
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 代表找不到用 例或缓存文件时不执行测试用例。

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

# 测试函数
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

运行:

pytest -v

-v表示查看详情。
3

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

测试子集

按照函数名查找子集

test_sub.py

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


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

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

分组标记

# -*- 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
pytest -v -m group

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

夹具函数

import pytest

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


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

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

参数化

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

pytest test_parame.py -v

1

跳过测试

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

pytest test_xfail_skip.py -v

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

1

更多请见官网

猜你喜欢

转载自blog.csdn.net/lilongsy/article/details/132150307