Pytest 测试框架
一 . Pytest 简介
Pytest是python的一种单元测试框架。
1. pytest 特点
- 入门简单,文档丰富
- 支持单元测试,功能测试
- 支持参数化,重复执行,部分执行,测试跳过
- 兼容其他测试框架(nose,unittest 等)
- 支持生成html报告
- 可集成CI环境(Jenkins 等)
- 第三方插件丰富,良好的自定义扩展性
2. pytest 与 unittest
(1)unittest
- 测试文件必须先 import unittest
- 测试类必须继承unittest.TestCase
- 测试方法必须以“test_”开头
- 测试类必须要有unittest.main()方法
- unittest只有setup/teardown装载测试用例
- ...
(2)pytest
- 测试文件名必须以“test_”开头
- 测试类以Test开头,并且不能带有 init 方法
- 测试方法必须以“test_”开头
- 除了有setup/teardown,还能更自由的定义fixture装载测试用例
- ...
(3)Comparison Table

二 . Pytest 安装
# 安装
pip install -U pytest
# 查看安装版本
pip show pytest
# 或者
pytest --version
三 . Pytest 运行
一个简单的例子 test_class.py:
# test_class.py
class TestClass:
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
1. cmd 中运行测试用例
# cmd中 cd到 test_class.py所在文件夹
# 运行 该文件夹中所有测试用例
pytest
# 或者
py.test
# 运行指定测试用例,加上-q参数用来指定执行的文件
pytest -q test_class.py
pytest运行规则:
查找当前目录及其子目录下以test_.py或_test.py文件,
找到文件后,在文件中找到以test开头函数并执行。
2. pycharm 中运行测试用例
一个简单的例子 test_sample.py:
# content of test_sample.py
import pytest
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
if __name__ == "__main__":
pytest.main('-q test_sample.py')
pycharm中运行pytest:
- file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test
- 右键选择pytest运行或者直接运行.py文件
四 . Pytest 装饰器
1. setup 与 teardown
装载运行级别:
- 模块级(setup_module/teardown_module)开始于模块始末
- 类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
- 方法级(setup_method/teardown_method)开始于方法始末(在类中)
- 类里面的(setup/teardown)运行在调用方法的前后
- 函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
一个简单的例子 test_module.py:
# -*- coding: utf-8 -*-
# Version : V1.0
# Author : Seven
# Date : 2018/8/2 19:41
import pytest
# 模块中的方法
def setup_module():
print("setup_module:整个.py模块只执行一次")
def teardown_module():
print("teardown_module:整个test_module.py模块只执行一次")
def setup_function():
print("setup_function:每个用例开始前都会执行")
def teardown_function():
print("teardown_function:每个用例结束后都会执行")
# 测试模块中的用例1
def test_one():
print("正在执行测试模块----test_one")
x = "this"
assert 'h' in x
# 测试模块中的用例2
def test_two():
print("正在执行测试模块----test_two")
x = "hello"
assert hasattr(x, 'check')
# 测试类
class TestCase():
def setup_class(self):
print("setup_class:所有用例执行之前")
def teardown_class(self):
print("teardown_class:所有用例执行之后")
def setup(self):
print("setup:每个用例开始前都会执行")
def teardown(self):
print("teardown:每个用例结束后都会执行")
def test_three(self):
print("正在执行测试类----test_three")
x = "this"
assert 'h' in x
def test_four(self):
print("正在执行测试类----test_four")
x = "hello"
assert hasattr(x, 'check')
if __name__ == "__main__":
pytest.main(["-s", "test_module.py"])
运行的效果:
collected 4 items
test_module.py setup_module:整个.py模块只执行一次
setup_function:每个用例开始前都会执行
正在执行测试模块----test_one
.teardown_function:每个用例结束后都会执行
setup_function:每个用例开始前都会执行
正在执行测试模块----test_two
Fteardown_function:每个用例结束后都会执行
setup_class:所有用例执行之前
setup:每个用例开始前都会执行
正在执行测试类----test_three
.teardown:每个用例结束后都会执行
setup:每个用例开始前都会执行
正在执行测试类----test_four
Fteardown:每个用例结束后都会执行
teardown_class:所有用例执行之后
teardown_module:整个test_module.py模块只执行一次
从结果看出:
- setup_module/teardown_module的优先级是最大的
- 然后函数里面的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉
2. fixture
一个简单的例子 test_fixture.py:
# conftest.py
# -*- coding: utf-8 -*-
import pytest
@pytest.fixture(scope="function")
def login():
print("请先输入账号和密码,然后登陆")
yield
print("退出登陆")
# test_1.py
# -*- coding: utf-8 -*-
import pytest
def test_fix1(login):
print("test_fix1 in test_1.py:需要登陆再执行操作")
def test_fix2():
print("test_fix2 in test_1.py:不需要登陆再执行操作")
def test_fix3(login):
print("test_fix3 in test_1.py:需要登陆再执行操作")
if __name__ == "__main__":
pytest.main(['-s', 'test_1.py'])
# test_2.py
# -*- coding: utf-8 -*-
import pytest
def test_fix3():
print("test_fix3 in test_2.py:不需要登陆再执行操作")
def test_fix4(login):
print("test_fix4 in test_2.py:需要登陆再执行操作")
if __name__ == "__main__":
pytest.main(['-s', 'test_2.py'])
文件结构:

运行效果:
pytest -s test_1.py
collected 3 items
test_1.py 请先输入账号和密码,然后登陆
test_fix1 in test_1.py:需要登陆再执行操作
.退出登陆
test_fix2 in test_1.py:不需要登陆再执行操作
.请先输入账号和密码,然后登陆
test_fix3 in test_1.py:需要登陆再执行操作
.退出登陆
pytest -s test_2.py
collected 2 items
test_2.py test_fix3 in test_2.py:不需要登陆再执行操作
.请先输入账号和密码,然后登陆
test_fix4 in test_2.py:需要登陆再执行操作
.退出登陆
从结果看出:
(1) scope 参数 实现控制 setup 级别
(2) conftest.py 配置 实现跨文件调用装饰器
(3) yield 实现 teardown
五 . Pytest 断言
- pytest允许使用python的标准assert语句进行断言处理
六 . Pytest 自动生成报告
# 生成xml格式的报告
pytest -v test_1.py --junitxml=Path
# 生成txt格式的报告
pytest -v test_1.py --resultlog=Path
# 生成html格式的报告
# 需预先装上pytest-html
pip install pytest_html
pytest -v test_1.py --html=Path
生成报告效果图:

转自:https://www.jianshu.com/p/75c27fe23b4e
Pytest 测试框架的更多相关文章
- pytest测试框架 -- 简介
一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...
- Pytest测试框架(一):pytest安装及用例执行
PyTest是基于Python的开源测试框架,语法简单易用,有大量的插件,功能非常多.自动检测测试用例,支持参数化,跳过特定用例,失败重试等功能. 安装 pip install -U pytest ...
- Pytest测试框架(二):pytest 的setup/teardown方法
PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...
- Pytest测试框架(三):pytest fixture 用法
xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...
- Pytest测试框架(五):pytest + allure生成测试报告
Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- python pytest测试框架介绍二
在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...
- 技术面试没过,居然是没有用pytest测试框架
1.引言 我有一个朋友是做Python自动化测试的.前几天他告诉我去参加一个大厂面试被刷了. 我问他是有没有总结被刷下来的原因.他说面试官问了一些 pytest 单元测试框架相关的知识,包括什么插件系 ...
- 【pytest系列】- pytest测试框架介绍与运行
如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言 目前有两种纯测试的测 ...
随机推荐
- [b0027] python 归纳 (十二)_并发队列Queue的使用
# -*- coding: UTF-8 -*- """ 学习队列 Queue 总结: 1. 队列可以设置大小,也可以无限大小 2. 空了,满了,读写时可以阻塞,也可以报错 ...
- 01 python的安装
下载3.7.0版本 然后点击 close 关闭 1-==>搜索输入“cmd”输入 “python -V”并回车. 出现版本说明安装成功.2==>>>> 是提示符3=== ...
- 2019徐州网络赛 H.function
题意: 先有\(n=p_1^{k_1}p_2^{k_2}\cdots p_m^{k_m}\),定义\(f(n)=k_1+k_2+\cdots+k_m\). 现在计算 \[ \sum_{i=1}^nf( ...
- 【bzoj1997】[Hnoi2010]Planar(平面图+2-sat)
传送门 几乎和这个题一样,就不说题意了,比较特殊的点就是,这里有个结论: 平面图的边数\(m<3n-6\),\(n\)为点数. 所以我们可以通过这个减枝,\(m\)较大时直接输出\(no\).小 ...
- JS阻止冒泡和取消默认事件(默认行为)
本文链接:http://caibaojian.com/javascript-stoppropagation-preventdefault.html 阻止事件冒泡 function(e){ if( e ...
- (day56)八、删除框、批量创建、分页器组件
目录 一.ajax结合sweetalert实现删除按钮的动态效果 二.bulk_create批量插入数据 三.自定义分页器 (一)手动推导 (二)自定义分页器 (1)模板 (2)用法 一.ajax结合 ...
- zz自动驾驶中轨迹规划的探索和挑战
大家好,今天我们主要介绍一下轨迹规划的探索和挑战,我主要从四个方面介绍: 轨迹规划的概念 决策 横向规划 纵向规划 轨迹规划的概念: 轨迹规划的核心就是要解决车辆该怎么走的问题.比如我们知道了附近有行 ...
- LG4341/BZOJ2251 「BJWC2010」外星联络 Trie
问题描述 LG4341 BZOJ2251 BZOJ需要权限号 题解 字符串的性质:一个字符串\(s\)所有的字串,等于\(s\)所有后缀的前缀. 枚举这个字符串的每一个后缀,将其插入一个\(\math ...
- ant design pro解决初始加载,有顺序的请求/请求顺序报错问题/登录后再加载其他数据/异步的顺序问题/偷跑
方法是:如在Authorized.jsx中解决,当未登录成功(包括登录失败和登录验证中),就显示loading,否则继续 加载渲染children 一个三目运算或者if分支就可以解决,但是要写到最先加 ...
- pindel及breandancer安装
1.安装pindel需要依赖htslib https://github.com/samtools/htslib.git mv htslib htslib1 autoheader # If using ...