python自动化脚本 01

在这里插入图片描述

setUp()函数:就是在一个类中最先被调用的函数,每次执行一个函数都要先执行这个函数,有几个函数就被调用几次,与放的位置无关,随便放到哪里都会先执行这个函数
tearDown():就是在一个类中最后被调用的函数,每个函数执行之后都会执行一次,与放的位置无关,随便放到哪里都会最后执行这个函数,不管其他函数是否能执行成功,这个函数都会被执行,如果setUp()函数执行失败,则认为这个测试项目失败,所有的函数都不会被执行也不会执行tearDown()这个函数

执行测试用例

import requests
import unittest
# 创建测试类
class TPShopLogin(unittest.TestCase)
	def setUp(self):
	#实例化session对象
		self.session=requests.Session()
	#定义验证接口url地址
		self.url_verify="http://localhost/index.php?m=Home&c=User&a=verify"
	#定义登录接口url地址
		self.url_login="http://localhost/index.php?m=Home&c=User&a=do_login"
	def tearDown(self):
		#关闭session对象
		self.session.close()
	def test01_success(self):
		#发送验证码请求断言
		response=self.session.get(url=self.url_verify)
		self.assertEqual(200,response.status_code) # 200为预期结果,status_code为实际结果
		self.assertIn("image",response.headers.get("Content-Type"))
		# 发送登录请求并断言
		login_data={
			"username":"13488888888",
			"password":"123456",
			"verify_code":"8888"
			}
		response1=self.session.post(url=self.url_login,data=login_data)
		self.assertEqual(200,response1.status_code) # 200为预期结果,status_code为实际结果
		self.assertIn(1,response1.json().get("status"))
		self.assertEqual("登陆成功",response1.json().get("msg")) # 200为预期结果,status_code为实际结果
		
	def test02_user_is_not_exist(self):
		#发送验证码请求断言
		response=self.session.get(url=self.url_verify)
		self.assertEqual(200,response.status_code) # 200为预期结果,status_code为实际结果
		self.assertIn("image",response.headers.get("Content-Type"))
		# 发送登录请求并断言
		login_data={
			"username":"13488888889",
			"password":"123456",
			"verify_code":"8888"
			}
		response1=self.session.post(url=self.url_login,data=login_data)
		self.assertEqual(200,response1.status_code) # 200为预期结果,status_code为实际结果
		self.assertIn(1,response1.json().get("status"))
		self.assertEqual("登陆成功",response1.json().get("msg")) # 200为预期结果,status_code为实际结果
		
	

测试结果如下:(test03在代码中未写出来)

在这里插入图片描述

生成测试报告

import time
import unittest
from tools.HTMLRunner import HTMLRunner #HTMLRunner 放在了tools文件下
from test01 import TPShopLogin

# 封装测试套件
suite=unittest.TestSuite()
suite.addTest(unittest.makeSuite(TPShopLogin))
# 指定报告路径
# 若起名为report.html 则每执行一次测试用例,由于名字相同,则会覆盖上一次所执行的,所以加一个时间信息,进行格式化处理,则可以保存每一次的测试报告
report="./report/report-{}.html".format(time.strftime("%Y%m%d-%H%M%S")) #每个人设置不同,所以文件目录不同,./为当前路径下
# 打开文件流
with open(report,"wb") as f: # wb为以二进制b可以写入w的方式打开文件
	# 创建HTMLRunner运行器
	runner=HTMLTestRunner(f,title="tpshop接口测试报告")
	#执行测试套件
	runner.run(suite)

运行结果如下:
在这里插入图片描述

参数化

json文件
在这里插入图片描述

import json
import requests
import unittest
from parameterized import parameterized 

# 构建测试数据
def build_data():
	test_data=[]
	file="./data/login" # login为准备好的文件
	with open(file,encoding="utf-8") as f:
	json_data=json.load(f)
	for case_data in json_data:
		username=case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            content_type = case_data.get("content_type")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((username,password,verify_code,content_type,status_code,status,msg))
            print(test_data) #可以打印出来,也可以不打印
        return test_data

# 创建测试类
class TPShopLogin(unittest.TestCase):
	def setUp(self):
		#实例化session对象
		self.session=requests.Session()
		# 定义验证接口url地址
		self.url_verify="http://localhost/index.php?m=Home&c=User&a=verify"
	#定义登录接口url地址
		self.url_login="http://localhost/index.php?m=Home&c=User&a=do_login"
	def tearDown(self):
		#关闭session对象
		self.session.close()
	
	@parameterized.expend(build_data())	
	def test01_login(self,username,password,verify_code,status_code,status,msg)
		# 发送验证码请求并断言
		response=self.session.get(url=self.url_verify)
		self.assertEqual(200,response.status_code)
		self.assertIn("image",response.headers.get("Content-Type"))
		# 发送登录请求并断言
		login_data={
			"username"=username,
			"password"=password,
			"verify_code"=verify_code
		}
		response1=self.session.post(url=self.url_login,data=login_data)
		self.assertEqual(status_code,response1.status_code)
		self.assertEqual(status,response1.json().get("status"))
		self.assertIn(msg,response.json().get("msg"))
		

优化:

前置处理:

# 封装登录被测试系统的接口
class LoginAPI:
	# 初始化
	def __init__(self):
		self.url_verify="http://localhost/index.php?m=Home&c=User&a=verify"
        self.url_login="http://localhost/index.php?m=Home&c=User&a=do_login"
        # 获取验证码
    def	get_verify_code(self,session):
    	return session.get(self.url_verify)
    def login(self,session,username,password,verify_code):
    	login_data={
			"username"=username,
			"password"=password,
			"verify_code":verify_code
		}
		return session.post(url=self.url_login,data=login_data)

准备好的json文件
在这里插入图片描述

# 定义接口测试用例
import json
import requests
import unittest
from api.login import LoginAPI #上一次loginAPI代码放在api文件夹下,名为login
from parameterized import parameterized
# 构造数据方法
def build_data():
	file="../data/login.json" #每个人文件路径不同
	test_data=[]
	with open(file,encoding="utf-8") as f:
		json_data=json.load(f) #加载json文件数据
		for case_data in json_data:
			 username=case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            content_type = case_data.get("content_type")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((username,password,verify_code,content_type,status_code,status,msg))
            print(test_data)
        return test_data

# 创建测试类
class TestLogin(unittest.TestCase):

def setUp(self):
	self.login_api=LoginAPI()
	self.session=requests.Session() #创建session对象
def tearDown(self)if self.session:
		self.session.close()

# 创建测试用例
@parameterized.expend(build_data())
def test01_login(self,username,password,verify_code,content_type,status_code,status.,msg)
	# 调用验证码接口,获取验证码接口并且进行断言
	response1=self.login_api.get_verify_code(self.session)
	self.asserEqual(status_code,response1.status_code)
	self.assertIn(content_type,response1.headers.get(
"Content-Type"))
# 调用登录接口,获取登录信息并进行断言
	response2=self.login_api.login(self.session,username,password,verify_code)
	self.asserEqual(status_code,response2.status_code)
	self.asserEqual(status,response2.json().get("status"))
	self.assertIn(msg,response2.json().get("msg"))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值