robotest接口自动化测试之参数传递之我准备好了开始吧

直接上菜!

setUpClass():所有的测试方法运行前运行,为单元测试做前期准备,但必须使用@classmethod装饰器进行修饰,整个测试过程中只执行一次。

import unittest


class Test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.token = None

    def test_01(self):
        # 赋值需要使用类名.变量
        Test.token = '嗷呜呜呜呜呜呜呜!'

    def test_02(self):
        print(self.token)


if __name__ == '__main__':
    unittest.main()

看看执行结果....

C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2020.3.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path G:/软件测试/接口测试/Robotest/cases/test_class_method.py
Testing started at 15:59 ...
Launching unittests with arguments python -m unittest G:/软件测试/接口测试/Robotest/cases/test_class_method.py in G:\软件测试\接口测试\Robotest\cases

嗷呜呜呜呜呜呜呜!


Ran 2 tests in 0.002s

OK

Process finished with exit code 0

setUpClass定义一个token变量,我们通过test_01去给这个变量赋值其实相当于我们的A接口获取到的返回值先把它保存到这个变量中,再在test_02中使用,相当与我们的B接口的入参是A接口获取到的值,我们通过变量取得,进而组成B接口请求的data。

robotest接口自动化测试之参数传递so easy:https://blog.csdn.net/u011640418/article/details/111999768

robotest接口自动化测试之参数传递之global全局变量:https://blog.csdn.net/u011640418/article/details/112078629

改造下我们接口代码

import unittest
import requests
import os
from ddt import ddt, file_data, unpack
from common.getpath import data_path
import json


post_data = os.path.join(data_path, 'post_data.yaml')
post_path2 = os.path.join(data_path, 'post_data2.yaml')


@ddt
class parameter_association(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.token = None

    @file_data(post_data)
    @unpack  # 二次分解元组
    def test_testcase1(self, **kwargs):
        url = kwargs['url']
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
            AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"}
        data = {"name": kwargs['name'],
                "pwd": kwargs['pwd']
                }
        # 发送post请求
        response = requests.post(url=url, headers=headers, data=data)
        # 因为返回值是一个json字典一样的字符串:{"code": 200, "message": "登录成功", "token": "@R_r18sQ8#"}
        # 使用json.loads()就可以自动转为最符合的数据类型,然后从转换后的字典中取token 的值
        parameter_association.token = json.loads(response.text)['token']
        # 把需要存的数据结构定义好 我需要保存成- token: '@R_r18sQ8#'
        print(response.text)

    @file_data(post_path2)
    @unpack  # 二次分解元组
    def test_testcase2(self, **kwargs):
        url = kwargs['url']
        # r 读取模式
        data = {"name": kwargs['name'],
                "token": self.token
                }
        response = requests.post(url=url, data=data)
        print(url)
        print(data)
        print(response.text)


if __name__ == '__main__':
    unittest.main()

查看下执行结果

C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2020.3.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path G:/软件测试/接口测试/Robotest/cases/test_class_method2.py
Testing started at 15:50 ...
Launching unittests with arguments python -m unittest G:/软件测试/接口测试/Robotest/cases/test_class_method2.py in G:\软件测试\接口测试\Robotest\cases

{"code": 200, "message": "登录成功", "token": "@R_r18sQ8#"}


Ran 2 tests in 0.042s

OK
http://127.0.0.1:8888/userinfo
{'name': 'xiaoming', 'token': '@R_r18sQ8#'}
{"code": 200, "name": "xiaoming", "info": "我长得可美了", "message": "获取成功"}

Process finished with exit code 0

是不是发现要比之前的写入到文件再读取还有使用global全局变量要方便呢,但是实际中当使用的时候需要看场景,举一反三。

.

.

.

.

走过路过不要错过,别忘了点赞+关注,如果还有其他问题可以联系我,一起讨论!

另外别忘了扫码支持一下

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值