【ZJ】Pytest框架搭建

Pytest框架搭建

1.测试框架

java:junit,testing

python:unittest,pytest

2.快速上手

安装
pip install pytest

升级到最新版
pip install pytest -U

pytest启动的三种方式

  1. 命令:pytest
  2. 代码
    建立了test_login文件后
python 复制代码
def test_login
	print('执行login测试用例')

在启动文件main.py

python 复制代码
import pytest
pytest.main()

3.鼠标(不推荐),因为是python提供的,不是pytest

3.看懂测试结果

执行环境:版本,根目录,用例数量

执行过程:文件名称,用例结果,执行进度

失败详情:用例内容,断言提示

整体摘要:结果情况,结果数量,花费时间

其中用例结果有6种

4.用例规则

用例发现规则

测试框架在识别,加载用例的过程,称之为:用例发现

pytest的用例发现规则步骤

  • 遍历所有的文件,除了:.开头的文件,.开头的目录
  • 打开python文件,test_开头或者_test结尾
  • 遍历所有Test开头类
  • 收集所有的test_开头的函数或者方法
    用例的内容规则
    pytest8.4增加一个强制的要求
  • 可调用的(函数,方法,类,对象)
  • 名字test_开头
  • 没有参数(参数有另外的含义)
  • 没有返回值(默认为None)

案例

python 复制代码
#测试函数"""
def add(a,b)
	return a+b
	
class TestAdd:
#测试方法"""
def test_int(self):
	res=add(1,3)
	assert res == 4

def test_str(self):
	res=add("1","3")
	assert res == "13"
	
def test_list(self):
	res=add([1],[2,3,4])
	assert res == [1,2,3,4]

5.配置框架

配置可以改变pytest默认的规则

  • 命令参数
  • ini配置文件
    所有的配置方式,可以一键获取
    pytest -h

有哪些配置

分别是什么方式

  • -开头:参数
  • 小写字母开头:ini配置
  • 大写字母开头:环境变量
    配置文件:pytest.ini

常用参数:

6.标记mark

标记,可以让用例与众不同,进而可以让用例被区别对待

1.用户自定义标记

用户自定义标记只能实现用例筛选

步骤:

1.先注册

如:在pytest.ini文件中

ini 复制代码
[pytest]
marker =
	api:接口测试
	web:web测试
	ut:单元测试
	login:登录相关
	pay:支付失败
	ddt:数据驱动测试

2.再标记

如:

python 复制代码
#测试函数"""

def add(a,b)
	return a+b

class TestAdd:
#测试方法"""
@pytest.mark.web	
def test_int(self):
	res=add(1,3)
	assert res == 4
@pytest.mark.api
def test_str(self):
	res=add("1","3")
	assert res == "13"
@pytest.mark.pay	
def test_list(self):
	res=add([1],[2,3,4])
	assert res == [1,2,3,4]

3.后筛选

pytest -m web

结果:只执行了标记了web的测试用例

2.框架内置标记

框架内置标记为用例增加特殊执行效果

和用户自定义标记区别

  • 不需注册,可以直接使用
  • 不仅可以筛选,还可以增加特殊效果
  • 不同标记,增加不同的特殊效果

    案例
python 复制代码
@pytest.mark.skip	
def test_list(self):
	res=add([1],[2,3,4])
	assert res == [1,2,3,4]

结果:跳过了这条测试用例

7.数据驱动测试参数

数据驱动测试=参数化测试+数据文件

根据数据文件的内容,动态决定用例的数量,内容

1.创建一个data.csv

csv 复制代码
a,b,c
1,2,3
2,3,4
4,5,6
1,2,4

2.封装函数,数据文件,驱动用例数量和内容

python 复制代码
import pytest	
#封装函数
import csv
	def read_csv(path):
		reader=csv.reader(f)
		return list(reader)[1:]

#测试函数"""		
def add(a,b)
	return a+b
#测试用例"""	
class TestAdd:
	@pytest.mark.ddt
	@pytest.mark.parametrize(
	"a,b,c"
	read_csv("data.csv")
	)	
	def test_list(self,a,b,c):
		res=add(int(a),int(b))
		assert res == c

8.夹具fixture

1.夹具:在执行之前,执行之后运行代码

场景:

之前:加密参数、之后:解密结果

之前:启动浏览器、之后:关闭浏览器

之前:注册,登录账号、之后:删除账号

1.创建函数

2.添加装饰器

3.添加yield关键字

python 复制代码
@pytest.fixture
def f():
	print(datatime.now(),'用例开始执行')
	#前置操作
	yield
	#后置操作
	print(datatime.now(),'用例结束执行')

2.使用fixture

1.在用例参数列表,加入fixtures名字就可以了

2.给用例加上usefixtures标记

python 复制代码
@pytest.fixture
def f():
	print(datatime.now(),'用例开始执行')
	#前置操作
	yield
	#后置操作
	print(datatime.now(),'用例结束执行')
#使用方式1
@pytest.mark.usefixtures("f")
def test_1():
	pass
#使用方式2
def test_2(f):
	pass

3.高级用法

1,现实使用

python 复制代码
@pytest.fixture(autouse=True)
def f():
	print(datatime.now(),'用例开始执行')
	#前置操作
	yield
	#后置操作
	print(datatime.now(),'用例结束执行')

def test_1():
	pass
#使用方式2
def test_2():
	pass

2.依赖使用

  1. linux:使用linux进行编辑
  2. git:使用git进行版本控制
  3. fixture:使用fixture来进行前后置自动操作
python 复制代码
import pytest
@pytest.fixture
def ff():
	print('我也是夹具,但是被fixture使用')

@pytest.fixture(autouse=True)
def f(ff):
	print(datatime.now(),'用例开始执行')
	#前置操作
	yield
	#后置操作
	print(datatime.now(),'用例结束执行')

def test_1():
	pass
#使用方式2
def test_2():
	pass

运行结果

3.返回内容:接口自动化封装:接口关联

如:用yield传递参数

python 复制代码
@pytest.fixture(autouse=True)
def f():
	print(datatime.now(),'用例开始执行')
	#前置操作 yield传递参数123
	yield 123
	#后置操作
	print(datatime.now(),'用例结束执行')

def test_1(f):
	print('收到传递的参数',f)
#使用方式2
def test_2():
	pass

4.范围共享:

默认范围:function

全局范围:session

使用conftest.py

相关推荐
趙卋傑6 小时前
接口自动化测试
python·pycharm·pytest
测试界萧萧2 天前
Jenkins+Allure+Pytest的持续集成
自动化测试·软件测试·功能测试·程序人生·ci/cd·jenkins·pytest
nvd113 天前
Pytest 中使用 SQLAlchemy 进行异步数据库测试
数据库·oracle·pytest
文人sec4 天前
pytest1-接口自动化测试场景
软件测试·python·单元测试·pytest
我的xiaodoujiao4 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 25--数据驱动--参数化处理 Excel 文件 2
前端·python·学习·测试工具·ui·pytest
我的xiaodoujiao12 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 24--数据驱动--参数化处理 Excel 文件 1
python·学习·测试工具·pytest
西游音月15 天前
(2)pytest+Selenium自动化测试-环境准备
selenium·测试工具·pytest
我的xiaodoujiao15 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 23--数据驱动--参数化处理 Yaml 文件
python·学习·测试工具·pytest