Pytest自动化测试框架实战

🍅 点击文末小卡片,免费获取软件测试全套资料,资料在手,涨薪更快

1、运行方式

命令行模式:

复制代码
pytest -s login.py

主函数模式:

复制代码
if __name__ == '__main__':
	pytest.main(["-s", "login.py"])

pytest.ini运行:

在 pytest.ini 文件中配置 pytest 的运行参数。

注意点:

位置:配置文件一般会放在项目的根目录下;

编码:必须是ANSI,可以使用文本编辑工具修改编码格式;

规则:无论是哪种运行方式,都会读取配置文件,是最高级的;

文件开头第一行除了注释,一定为 pytest ,这是固定格式,文件名 pytest.ini 也是固定的,不能自己重命名;

常用参数:

addopts 命令行的参数,用空格分隔;

testpaths 测试用例的路径;

markers 标记参数,赋值方式为 key:value;

python_files 模块的命名规则 xx.py;

python_classes 类名的命名规则 Xxx;

python_functions 方法的命名规则 **

required_plugins 插件的使用;

xfail_strict = true 禁用xpass;

2、断言

pytest 单元测试框架并没有提供专门的断言方法,而是直接使用Python 的 assert 进行断言。

复制代码
import pytest

# 功能:用于计算 a 与 b 相加的和
def add(a, b):
    return a + b

# 功能:用于判断素数
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
        return True

# 测试相等
def test_add_1():
    assert add(3, 4) == 7

# 测试不相等
def test_add_2():
    assert add(17, 22) != 50

# 测试大于或等于
def test_add_3():
    assert add(17, 22) <= 50

# 测试小于或等于
def test_add_4():
    assert add(17, 22) >= 38

# 测试包含
def test_in():
    a = "hello"
    b = "he"
    assert b in a

# 测试不包含
def test_not_in():
    a = "hello"
    b = "hi"
    assert b not in a

# 判断是否为 True
def test_true_1():
    assert is_prime(13)

# 判断是否为 True
def test_true_2():
    assert is_prime(7) is True

# 判断是否不为 True
def test_true_3():
    assert not is_prime(4)

# 判断是否不为 True
def test_true_4():
    assert is_prime(6) is not True

# 判断是否为 False
def test_false_1():
    assert is_prime(8) is False


if __name__ == '__main__':
    pytest.main(["-s", "0701.py"])

3、Fixture

Fixture 通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境。

setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行。

setup_function/teardown_function:在每个测试函数之前与之后执行。

setup/teardown:在每个测试函数之前与之后执行。这两个方法同样可以作用于类方法。

4、参数化

复制代码
# argnames:参数名 
# argvalues:参数对应值,类型必须为可迭代类型,一般使用list 
@pytest.mark.parametrize(argnames, argvalues, indirect=False,ids=None, scope=None)

示例:

复制代码
import pytest

class TestLogin:
    @pytest.mark.parametrize(("username", "password"), [("zhangsan", "zhangsan123"), (" xiaoming", "xiaoming123")])
    def test_a(self, username, password):
        print(username)
        print(password)
        assert 1

5、运行测试

pytest 提供了丰富的参数运行测试用例,通过"pytest --help"可以查看帮助。

运行名称中包含某字符串的测试用例:

例如:通过"-k"来指定在名称中包含"add"的测试用例

复制代码
pytest -k add test.py

if __name__ == '__main__':
	pytest.main(["-k", "add", "test.py"])

减少测试的运行冗长:

运行日志少了很多信息,"-q"用来减少测试运行的冗长;也可以使用"--quiet"代替。

复制代码
pytest -q test.py

if __name__ == '__main__':
	pytest.main(["-q", "test.py"])

如果出现一条测试用例失败,则退出测试:

这在测试用例的调试阶段是有用的,当出现一条失败的测试用例时,应该先通过调试让这条测试用例运行通过,而不是继续执行后面的测试用例。

复制代码
pytest -x test.py

if __name__ == '__main__':
	pytest.main(["-x", "test.py"])

运行测试目录:

测试目录既可以指定相对路径(如 ./test_dir ) , 也可以指定绝对路径(如D:\pytest_sample\test_dir)。

复制代码
pytest ./test_dir

指定特定类或方法执行:

这里指定运行 test_fixtures_02.py 文件中 TestMultiply 类下的 test_numbers_5_6()方法,文件名、类名和方法名之间用"::"符号分隔。

复制代码
pytest test_fixtures_02.py::TestMultiply::test_numbers_5_6

6、跳过测试

使用方式:在需要跳过的测试脚本之上加上装饰器 @pytest.mark.skipif(condition, reason="xxx")

复制代码
# condition:跳过的条件,必传参数 
# reason:标注原因,必传参数 
@pytest.mark.skipif(condition, reason=None)

7、生成测试报告

生成 JUnit XML 文件:

复制代码
pytest ./test_dir --junit-xml=./report/log.xml

生成在线测试报告:

复制代码
pytest ./test_dir --pastebin=all

上述代码可生成一个 session-log 链接,复制链接,通过浏览器打开,会得到一张 HTML格式的测试报告。

8、pytest 插件

pytest-html:

pytest-html 可以生成 HTML 格式的测试报告。

首先,通过 pip 命令安装 pytest-html 扩展。

复制代码
pip install pytest-html

其次,运行测试用例,并生成测试报告.

复制代码
pytest ./ --html=./report/result.html

pytest-rerunfailures:

pytest-rerunfailures 可以在测试用例失败时进行重试。

复制代码
pip install pytest-rerunfailures

通过"--reruns"参数设置测试用例运行失败后的重试次数。

复制代码
pytest -v test.py --reruns 3

pytest-parallel:

pytest-parallel 扩展可以实现测试用例的并行运行。

复制代码
pip install pytest-parallel

参数"--tests-per-worker"用来指定线程数,"auto"表示自动分配。

复制代码
pytest -q test.py --tests-per-worker auto

示例:

复制代码
from time import sleep

def test_01():
    sleep(3)

def test_02():
    sleep(5)

def test_03():
    sleep(6)

不使用线程运行测试用例花费14.05s,使用后被缩短到 6.02s。

pytest-ordering:

控制函数执行顺序。

复制代码
pip3 install pytest-ordering

使用:

标记于被测试函数,@pytest.mark.run(order=x);

根据order传入的参数来解决运行顺序;

order值全为正数或全为负数时,运行顺序:值越小,优先级越高;

正数和负数同时存在:正数优先级高;

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。

相关推荐
Angeldream1238 分钟前
第 10 章 医学影像伦理与安全
图像处理·人工智能·python·安全·机器学习·医学影像
勤奋的大熊猫23 分钟前
Python 双层 enumerate 循环选取最后一层循环的优雅写法
python
JYCJ_29 分钟前
星愿池·子项清单
python
Wang's Blog31 分钟前
Java 项目实战: 交友项目 - 动态查询开工:昨日回顾与今日任务
java·python·交友
2601_9679791234 分钟前
0 到 1 学爬虫:用 Python 爬取网页数据指南一(附实战案例)
爬虫·python·实战案例·html解析·网页数据
云和数据.ChenGuang44 分钟前
光哥说Python 异常深度解析:从异常模型到工程化实践
java·前端·人工智能·python·langchain
螺丝钉tina1 小时前
[Playwright][08] Upload 模块自动化:如何设计真实业务测试用例?
运维·自动化·测试用例
SelectDB1 小时前
云数仓月度账单总是超预算:Apache Doris 与 Snowflake 五类成本项拆解与对账脚本
python
迪菲赫尔曼1 小时前
【基础篇 12】把环境与源码接起来:检查 GPU,再安装服务器上的项目
运维·服务器·开发语言·人工智能·python·yolo·目标检测
2601_962885721 小时前
2026 Python 股票数据接口选型指南(横评 + 决策树)
开发语言·python·区块链