基于python接口自动化测试——pytest


一、安装与入门

安装
pip install pytest
查看pytest版本
pytest --version
入门:创建第一个测试用例

#1、创建简单的测试方法
	#1、创建普通方法
def func(x):
	return x+1
	#2、使用pytest断言的方法
def test_a():#以test开头的方法
	print("test_a")
	assert func(3) == 5 #断言失败
def test_b():
	print("test_b")
	assert 1 #断言
if __name__="__main__":
	pytest.amin(["pytest_demo.py"])
#2、pytest运行
	#1.直接在paycahrm中运行
	#2.命令行执行

二、基础使用

2.1函数级别方法

·运行于测试方法的始末
·运行一次测试函数会运行一次setup和teardown

此文件名为:pytest_func.py

import pytest
#1、定义一个类
class TestFunc:
	
#2、创建测试方法test开头
	def test_a(self):
		print("test_a")
	def test_b(self):
		print("test_b")
#3、创建setup、teardoown
	def setup(self):
		print("setup")
	def teardown(self):
		print("teardown")
#4、运行查看结果
if __name__="__main__":
	pytest.main("-s","pytest_func.py")
	

2.2类级别方法

此文件名为:pytest_class.py

·运行于测试类的始末
—个测试内只运行一次setup_class和teardown_class,不关心测试类内有多少个测试函数

import pytest
#1、定义一个类
class TestClass:
#2、创建测试方法test开头
	def test_a(self):
		print("test_a")
	def test_b(self):
		print("test_b")
#3、创建setup、teardoown
	def setup_class(self):
		print("setup_class")
	def teardown_class(self):
		print("teardown_class")
		#4、运行查看结果
if __name__="__main__":
	pytest.main("-s","pytest_class.py")

三、常用插件

3.1测试报告

应用场景
自动化测试脚本最终执行是通过还是不通过,需要通过测试报告进行体现。

安装
pip install pytest-html
使用
在配置文件中的命令行参数中增加–html=用户路径/report.html

此文件名为:pytest.ini

addopts = --html=./report/report	.html

在这里插入图片描述
在这里插入图片描述

3.2失败重试

应用场景
当失败后尝试再次运行

安装
$ pip3 install pytest-rerunfailures
使用
在配置文件中的命令行参数中增加 --reruns n
如果你期望加上出错重试的等待时间 --reruns-delay
在这里插入图片描述

addopts = --html=./ report/report.html --reruns 3 --reruns-delay-2

四、数据参数化

4.1 传入单个参数

pytest.mark.parametrize(argnames, argvalues)

argnames:参数名
argvalues:参数对应值,类型必须为可迭代类型,一般使用list

import pytest
#1、创建类和测试方法
class TestDemo:
#2、创建测试数据
	data_list = ["xiaoming" , "xiaohong"]
	#3、参数化
	@pytest.mark.parametrize("name","data_list")
	def test_a(self,name):
		print( "test_a")
		print(name)
		assert 1
if __name__="__main__":
	pytest.main(["pytest_one.py"])

4.2 传多个参数

@pytest.mark.parametrize((‘参数名1,参数名2’),[(参数1_data[0],参数1_data[1])(参数2_data[0],参数2_data[1])J)

list的每个元素都是一个元组,元组里的每个元素和按参数顺序——对应

import pytest
#1、创建类和测试方法
class TestDemo:
#2、创建测试数据
	data_list = ["xiaohong" , "123456")("xiaohong","456789"]
	#3、参数化
	@pytest.mark.parametrize(("name","password),""data_list"))
	def test_a(self,name,password):
		print( "test_a")
		print(name)
		assert 1
if __name__="__main__":
	pytest.main(["pytest_two.py"])

五、应用接口用例

运行原则
在不指定运行目录,运行文件,运行函数等参数的默认情况下,pytest会执行当前目录下的所有以test
为前缀(test*.py)或以_test为后缀(_test.py)的文件中以test为前缀的函数
在这里插入图片描述

在这里插入图片描述

#执行目录
testpaths = testcasel#执行目录下的文件
python_files = test_*.py#执行测试类
#python_classes = Test_*#执行测试方法
python_functions = test_*

5.1登录\个人信息\商品列表\购物车\订单

1、根据默认运行原则,调整py文件命名,函数命名
2、pytest.main()运行,或者命令行直接pytest运行

此文件名为:test_Mail_post.py(只有post请求)

from RequestsUtil import requests_post
import requests
import pytest
def test_login():
   
#3、定义测试数据
   url="http://211.103.136.242:8064/authorizations/"
   data={"username" : "python","password" : "12345678"}
#4、发送post请求
   #r=requests.post(url,json=data)
   r=requests_post(url,json=data)
#5、输出结果
   #是json还是text,取决于返回的接口,如果不是json格式的话,使用json会报错
   #print(r.json)
   print(r)
def test_info():
   #1、参数
   url="http://211.103.136.242:8064/user/"
   token="上一步2.1登录输出的返回值中含有token,复制过来即可"
   headers={'Authorization':'JWT ' + this.token}
   #2、get请求
   #r=requests.get(url,headers=headers)
   requests_get(url,headers=headers)
   #3、输出
   #print(r.json())
   print(r)
def test_cart():
   #1、参数
   url="http://211.103.136.242:8064/cart/"
   data={"skiid":"3","count" :"1", "selected" :"true"}
   #需要登录,所以用到headers
   headers={'Authorization':'JWT ' + this.token}
   #2、请求
   #r=requests.post(url,json=data,headers=headers)
   r=requests_post(url,json=data,headers=headers)
   #3、输出
   print(r,json())
   print(r)
if __name__="__main__":
   #info()
   #login()
   #cart()
   pytest.main(["-s"])

此文件名为:test_Mail_get.py(只有get请求)

 from RequestsUtil import requests_post
 import requests
import pytest

def test_login():
	
#3、定义测试数据
	url="http://211.103.136.242:8064/authorizations/"
	data={"username" : "python","password" : "12345678"}
#4、发送post请求
	#r=requests.post(url,json=data)
	r=requests_post(url,json=data)
#5、输出结果
	#是json还是text,取决于返回的接口,如果不是json格式的话,使用json会报错
	#print(r.json)
	print(r)
def test_info():
	#1、参数
	url="http://211.103.136.242:8064/user/"
	token="上一步2.1登录输出的返回值中含有token,复制过来即可"
	headers={'Authorization':'JWT ' + this.token}
	#2、get请求
	#r=requests.get(url,headers=headers)
	#requests_get(url,headers=headers)
	from RequestsUtil import Request
	requests=Requests()
	r=requests.get(url,headers=headers)
	#3、输出
	#print(r.json())
	print(r)
def test_cart():
	#1、参数
	url="http://211.103.136.242:8064/cart/"
	data={"skiid":"3","count" :"1", "selected" :"true"}
	#需要登录,所以用到headers
	headers={'Authorization':'JWT ' + this.token}
	#2、请求
	#r=requests.post(url,json=data,headers=headers)
	r=requests_post(url,json=data,headers=headers)
	#3、输出
	print(r,json())
	print(r)
if __name__="__main__":
	#info()
	pytest.main(["-s"])

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值