1. 程式人生 > 其它 >robotest介面自動化測試之引數傳遞之我準備好了開始吧

robotest介面自動化測試之引數傳遞之我準備好了開始吧

技術標籤:介面自動化測試Python軟體測試

直接上菜!

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全域性變數要方便呢,但是實際中當使用的時候需要看場景,舉一反三。

.

.

.

.

走過路過不要錯過,別忘了點贊+關注,如果還有其他問題可以聯絡我,一起討論!

另外別忘了掃碼支援一下