pytest 基本用法
1、断言用assert,可以进行==,!=,+,-,*,/,<=,>=,is True、False,is not True、False ,in ,not in 等判断。
import pytest
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(12,3) != 16 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 def test_true_1():
'''判断是否为True'''
assert is_prime(13) is True def test_true_2():
'''判断是否为True'''
assert is_prime(7) is True def test_true_3():
'''判断是否不为True'''
assert is_prime(4) is not True def test_true_4():
'''判断是否不为True'''
assert is_prime(6) is not True def test_false_1():
'''判断是否为False'''
assert is_prime(8) is False if __name__ == '__main__':
pytest.main()
2、测试文件和测试函数必须以“test”开头,测试类必须以‘Test’开头。
3、可以通过main()方法执行测试用例。需要指定参数和路径,还可以指定某个测试类或测试方法用“::”隔开。如:
pytest.main(['-s','./test_fixtures_01.py::test_multiply_5_6'])
4、Pytest提供了丰富的参数运行测试用例,‘-s’:关闭捕捉,输出打印信息。‘-v’:用于增加测试用例的冗长。‘-k’ :运行包含某个字符串的测试用例。如:pytest -k add XX.py 表示运行XX.py中包含add的测试用例。‘q’:减少测试的运行冗长。‘-x’:出现一条测试用例失败就退出测试。在调试阶段非常有用,当测试用例失败时,应该先调试通过,而不是继续执行测试用例。pytest还可以运行测试目录下的所有测试用例:pytest 目录路径
5、Fixtrue
import pytest #功能函数
def multiply(a,b):
return a * b class TestMultiply:
#=======fixtures========
@classmethod
def setup_class(cls):
print('setup_class==============================>') @classmethod
def teardown_class(cls):
print('teardown_class==========================>') def setup_method(self,method):
print('setup_method============================>') def teardown_method(self,method):
print('teardown_method============================>') def setup(self):
print('setup======================================>') def teardown(self):
print('teardown===================================>')
6、参数化。pytest本身是支持参数化的,不需要安装插件。
import pytest
import math #pytest参数化
@pytest.mark.parametrize('base,exponent,expected',[(2,2,4),(2,3,8),(2,4,16),(0,9,0)],ids = ['case1','case2','case3','case4'])
def test_pow(base,exponent,expected):
assert math.pow(base,exponent) == expected if __name__ == '__main__':
pytest.main(['-s','./test_parameterized.py'])
7、conftest.py 是pytest的本地测试配置文件。可以设置项目级别的Fixtrue还可以导入外部插件,还可以指定钩子函数。conftest.py值作用于它所在的目录及子目录。
import pytest
#设置钩子函数
@pytest.fixtrue()
def test_url():
return 'http://www.baidu.com'
8、pytest-html 插件可以生成HTML格式的测试报告。支持测试用例失败 的截图。对于web自动化测试来说非常有用。
运行:pytest 用例路径 --html=./report/result.html 注意:--html= 没有空格。
还可以用main()方法来运行:pytest.main(['当前用例路径','--html=测试报告/XX.html '])
9、pytest-rerunfailures 插件可以在测试用例失败时进行重试。通过‘--reruns 重试次数’来设置测试用例运行失败后的重试次数。
pytest 用例路径 --reruns 3
相对于unittest框架,pytest更适合做UI自动化:1、pytest通过conftest.py文件配置全局浏览器的启动或关闭,整个自动化项目只需要启动或者关闭一次浏览器即可,将节省用例运行时间和开销。2、pytest支持用例运行失败截图。通过pytest-html可以实现,需要在conftest.py配置即可。3、测试用例失败后重跑。UI自动化测试的稳定性是个问题。有很多不可控的因素会导致测试用例的失败,pytest-rerunfailurea可以实现用例重跑。
pytest 基本用法的更多相关文章
- Pytest测试框架(三):pytest fixture 用法
xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...
- pytest 常见用法
前言 之前一篇文章简单介绍了 pytest 以及 fixture :https://www.cnblogs.com/shenh/p/11572657.html .实际在写自动化测试脚本中,还会有一些很 ...
- 【Python】【自动化测试】【pytest】【常用命令行选项】
https://www.cnblogs.com/cnkemi/p/9989019.html http://www.cnblogs.com/cnkemi/p/10002788.html pytest 常 ...
- pytest 常用命令行选项(二)
本文接上篇继续简介pytest常用的命令行选项. 8.-v(--verbose) 选项 使用-v/--verbose选项,输出的信息会更详细.最明显的区别就是每个文件中的每个测试用例都占一行,测试的名 ...
- pytest setup和teardown初始化
用法简介: setup_method:仅作用于class用例集中的用例,置于class内,每个用例都会调用一次 setup_function:作用于独立的def用例,不可作用于class内的用例 se ...
- 自动化冒烟测试 Unittest , Pytest 哪家强?
前言:之前有一段时间一直用 Python Uittest做自动化测试,觉得Uittest组织冒烟用例比较繁琐,后来康哥提示我使用pytest.mark来组织冒烟用例 本文讲述以下几个内容: 1.Uni ...
- [Python]使用pytest进行单元测试
安装pytest pipenv install pytest 验证安装的版本: pytest --version This , imported /site-packages/pytest.py 接下 ...
- pytest初始化与清除fixture(二)
@pytest.fixture用法 1.导入pytest模块:import pytest 2.调用装饰器函数:@pytest.fixture(callable_or_scope=None,*args, ...
- python测试框架-pytest
一.pytest 介绍.运行.参数化和数据驱动.Fixture pytest安装与介绍 官网 : pip install -U pytest 查看版本号:pytest --version 为何选择py ...
随机推荐
- Linux文件(夹)属性
ll 或者 ls -lh 查看文件属性:
- Devices Tree加载流程
DT.IMG布局 hdr zImage Ramdisk.img DT.img 其中DT.img由DTBTOOL打包所有编译生成的dtb生成:布局如下: DT header dt_entry_0 dt_ ...
- Dumpsys Alarm查看应用程序唤醒命令
Dumpsys alarm查看应用程序唤醒命令: 在安卓adb root进如命令行后(没有root或者root群组的权限执行不了该命令), 1. <span style="font-s ...
- diff算法实现
大致流程 var vnode = { tag: 'ul', attrs: { id: 'list' }, children: [{ tag: 'li', attrs: { className: 'it ...
- k8s的api
一.namespaced resources 所谓的namespaced resources,就是这个resource是从属于某个namespace的, 比如pod, deployment, serv ...
- P1168 中位数[堆 优先队列]
题目描述 给出一个长度为NNN的非负整数序列AiA_iAi,对于所有1≤k≤(N+1)/21 ≤ k ≤ (N + 1) / 21≤k≤(N+1)/2,输出A1,A3,…,A2k−1A_1, A_3 ...
- 持续集成实践---基于ant+jmeter+jenkins接口CI
背景: 大纲: jmeter入门教程 jenkins入门教程 ant介绍 jmeter+ant脚本自动执行实践 接口测试CI实践
- js+css 动效+1的效果
点击数值 +1 的动效 vue data:{ timer: null,plus:''// 次数 } method:{ animate(plus) { var _this = this; clearIn ...
- go语言-关于文件的操作和解释
一.文件存放的位置 bin文件:存放编译后的二进制文件 pkg文件:存放编译后的库文件 src文件:存放源代码文件 二.运行文件的常用命令 两种运行区别(直接运行和编译后运行) 1.编译生成可执行文件 ...
- js中的数据类型以及转换
Js中的数据类型 Js中的数据类型一共有六种,即number,string,boolean,underfine,null,object. 一,number Number数据类型指的是数字,可以为整型, ...