接口测试集成到unitTest的学习

1、集成UnitTest

将接口测试脚本集成到UnitTest单元测试框架中,利用UnitTest的功能来运行接口测试用例

2、使用UnitTest框架的目的

  1. 方便管理和维护多个测试用例
  2. 提供丰富的断言方法
  3. 能够生成测试报告

3、代码示例

以下是对登录接口和获取个人信息接口进行测试
实现思路:

# 导包
# 创建测试类
	# 创建测试方法
	# setup
		# 实例化session对象
		# 定义验证接口url地址
		# 定义正如接口url地址
	# teardown
		# 关闭session对象
	# 登录成功
		# 发送验证码请求并断言
		# 发登录请求并断言
	# 账号不存在
		# 发送验证码请求并断言
		# 发登录请求并断言
	# 密码错误
		# 发送验证码请求并断言
		# 发登录请求并断言
import unittest
import requests

class LoginTest(unittest.TestCase):
    def setUp(self):
        self.session=requests.Session()
        #登录url
        self.url_login="https://toutiao.itheima.net/v1_0/authorizations"
        #获取个人信息url,这个接口需要token
        self.url_user="https://toutiao.itheima.net/v1_0/user"

    def tearDown(self):
        self.session.close()
    #登录成功
    def test_001(self):
        login_data={
            "mobile":"13811111111",
            "code":"074986"
        }
        response=self.session.post(url=self.url_login,json=login_data)
        #断言
        self.assertEqual(201,response.status_code)
        self.assertIn("OK",response.json().get("message"))
        token=response.json().get("data").get("token")
        print(response.json())
        print(token)
        headers={
            "Authorization":'Bearer '+token
        }
        response=self.session.get(url=self.url_user,headers=headers)

        print(response.json())

    # 登录失败(手机号错误)
    def test_002(self):
        login_data = {
            "mobile": "138111111112",
            "code": "074986"
        }
        response = self.session.post(url=self.url_login, json=login_data)
        self.assertEqual(400, response.status_code)
        self.assertIn("手机号或验证码格式不正确", response.json().get("message"))
    #登录失败(验证码错误)
    def test_003(self):
        login_data = {
            "mobile": "13811111111",
            "code": "255254"
        }
        response = self.session.post(url=self.url_login, json=login_data)
        self.assertEqual(400, response.status_code)
        self.assertIn("验证码不正确", response.json().get("message"))

在这里插入图片描述

4、生成测试报告

#导包
import time
import unittest
from tools.HTMLTestRunner import HTMLTestRunner
from test01 import LoginTest

#封装测试套件
suite=unittest.TestSuite()
suite.addTest(unittest.makeSuite(LoginTest))

#指定报告路径
report='./report/report-{}.html'.format(time.strftime("%Y%m%d-%h%M%S"))

#打开文件流
with open(report,"wb") as f:
    #创建HTMLRunner运行器
    runner=HTMLTestRunner(f,title="接口测试用例报告")
    #执行测试套件
    runner.run(suite)

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

5、unitTest参数化

以下是对登录接口进行测试,将登录的一些数据写到一个json文件里,通过参数化的形式,减少代码量
json数据文件的准备:

[
    {
        "desc": "登录成功",#为了区分每一个用例。为了添加一个key来对用例进行描述
        "mobile": "13811111111",
        "code": "559720",
        "status_code": 201,
        "message": "OK"
    },
     {
        "desc": "手机号错误",
        "mobile": "138111111112",
        "code": "123456",
        "status_code": 400,
        "message": "手机号或验证码格式不正确"
    },
     {
        "desc": "验证码错误",
        "mobile": "13811111111",
        "code": "123456",
        "status_code": 400,
        "message": "验证码不正确"
    }
]
import json
import unittest
import requests
from parameterized import parameterized

def build_data():
    #先准备好一个空列表
    test_data=[]
    #打开json文件
    filePath='./data/login_data.json'
    with open(filePath,encoding='utf-8') as file:
        json_data=json.load(file)
        for item in json_data:
            mobile=item.get("mobile")
            code = item.get("code")
            status_code = item.get("status_code")
            message = item.get("message")
            #将这些参数作为元组追加到列表
            test_data.append((mobile,code,status_code,message))
            print("test_data的数据为:",test_data)
    return test_data

class LoginTest2(unittest.TestCase):
    def setUp(self):
        self.session=requests.Session()
        #登录url
        self.url_login="https://toutiao.itheima.net/v1_0/authorizations"
    def tearDown(self):
        self.session.close()

    #需要这个装饰器,装饰器里面的这个方法需要从json文件拿数据,并将数据return这边才能拿到数据,进行传参
    @parameterized.expand(build_data())
    def test_001(self,mobile,code,status_code,message):
        login_data={
            "mobile":mobile,
            "code":code
        }
        response=self.session.post(url=self.url_login,json=login_data)
        #断言
        self.assertEqual(status_code,response.status_code)
        self.assertIn(message,response.json().get("message"))
        print(response.json())

步骤:
1、准备json数据
2、定义一个方法用来打开一个json文件,从里面拿数据,将数据封装成元组后,放到列表,然后进行return
3、在测试类的测试方法前添加装饰器,装饰条调用第二步中写的方法,这样就实现了将json文件里的数据传参给测试方法的形参的一个桥梁

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值