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 ...
随机推荐
- Computer Vision_33_SIFT:TILDE: A Temporally Invariant Learned DEtector——2014
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- 如何为Spring Boot应用程序配置端口
[转]https://www.javaroad.cn/questions/11162 1 个月前 1.1通过属性文件更新 . /src/main/resources/application.prope ...
- java相关网址汇总(myself)
jar包下载网址 https://www.mvnjar.com/ 或者 https://mvnrepository.com/ 或者 http://www.java2s.com/Open-Source/ ...
- Using Microsoft Visual C++ DLLs with C++Builder
Using Microsoft Visual C++ DLLs with C++Builder As powerful as C++Builder is, the majority of DLLs d ...
- RollingRegression(滚动回归分析)之Python实现
# -*- coding: utf-8 -*-"""Created on Sat Aug 18 11:08:38 2018 @author: acadsoc"& ...
- linux下环境管理anaconda3
我之前在centos之安装单独python3.6,大家都知道centos自带python2.7,通过输入python,和python3来控制想要使用python2,或者python3,如今想要要在li ...
- WebService在ssm框架中的简单应用
WebService的概念 Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配 ...
- 编写一个c程序来计算整数中的设置位数?
回答: unsigned int NumberSetBits(unsigned int n) { ; while (n) { ; ; } return CountSetBits; } 本质上就是计算n ...
- js文件上传下载组件
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- opencv 学习一安装环境vs2015+opencv3
参考博客 http://www.cnblogs.com/skyfsm/p/6840202.html 针对 模块计算机类型“X64”与目标计算机类型“X86”这个问题,我使用cmake 对环境的工程进行 ...