獲取token
在做接口自動化的時候,經常會遇到多個用例需要用同一個參數token,並且這些測試用例跨.py腳本了。
一般token只需要獲取一次就行了,然后其它使用unittest框架的測試用例全部調用這個參數,那么如何實現呢?
雖然python里面有個全局變量global,但這個只是針對於在同一個.py里才有效,跨腳本就不起作用了。
解決思路
- 1.首先把公共數據單獨抽出來,用一個文件去管理,如yaml文件
- 2.寫一個讀yaml文件的get_token()函數放到a.py,去讀取需要的數據
- 3.其它腳本全部調用a.py里面的get_token()函數,然后傳參
- 4.token動態獲取可以寫個登錄函數放到run.py,獲取到之后把token值寫入到yaml文件,這樣保證每次token都是最新的
- 5.run.py里面在運行所有用例之前先登錄一次並寫入token到yaml,然后執行所有用例,出報告結果
設計結構如下
yaml配置
1.公共參數我們可以寫入到一個單獨的配置文件里,這里配置文件我用的yaml來管理數據
讀取token
1.寫個讀取yaml文件里面的token值的函數,放到re_token.py,代碼如下:
# coding:utf-8
import yaml
import os
# 作者:上海-悠悠 QQ交流群:588402570
cur = os.path.dirname(os.path.realpath(__file__))
def get_token(yamlName="token.yaml"):
'''
從token.yaml讀取token值
:param yamlName: 配置文件名稱
:return: token值
'''
p = os.path.join(cur, yamlName)
f = open(p)
a = f.read()
t = yaml.load(a)
f.close()
return t["token"]
if __name__ =="__main__":
print(get_token())
測試用例token參數關聯
1.經常用人問token如何參數關聯,問這種問題的,基本上沒認真學過python,估計平常寫的代碼都是東拼西湊到處復制過來的。
2.token的參數關聯用一個中間變量【token】去接收上一步【get_token()函數)】獲取的token值就行了,再傳入到需要的地方,so easy!
3.以下代碼是test_01和test_02里面寫入的演示案例
# coding:utf-8
import unittest
from common.re_token import get_token
# 作者:上海-悠悠 QQ交流群:588402570
class Test_01(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.token = get_token()
print("獲取到當前用例token值:%s" % cls.token)
def test_01(self):
'''測試用例1'''
body1 = {
"a": "111111",
"b": "111111",
"token": self.token # 參數關聯
}
print("用例1body:%s" % body1)
def test_02(self):
'''測試用例2'''
body1 = {
"a": "222222",
"b": "2222222",
"token": self.token # 參數關聯
}
print("用例2body:%s" % body1)
if __name__ == "__main__":
unittest.main()
4.執行結果:
..
獲取到當前用例token值:yoyoketang
用例1body:{'token': 'yoyoketang', 'a': '111111', 'b': '111111'}
用例2body:{'token': 'yoyoketang', 'a': '222222', 'b': '2222222'}
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
執行策略
1.單個腳本運行沒問題了,那么如果一次性執行多個用例腳本,都調用同一個token值呢?run.py腳本參考如下
# coding=utf-8
import unittest
import os
import yaml
from common import HTMLTestRunner_cn
from ruamel import yaml
curpath = os.path.dirname(os.path.realpath(__file__))
# 作者:上海-悠悠 QQ交流群:588402570
def login(user="yoyo", psw="123456"):
'''
先執行登錄,傳賬號和密碼兩個參數
:return: 返回token值
'''
print("登錄的賬號名稱:%s" % user)
p = psw
print("輸入的密碼:**********")
token = "xxxxxxxxx" # 登錄后獲取到的token值
return token
def write_yaml(value):
'''
把獲取到的token值寫入到yaml文件
:param value:
:return:
'''
ypath = os.path.join(curpath, "common", "token.yaml")
print(ypath)
# 需寫入的內容
t = {"token": value}
# 寫入到yaml文件
with open(ypath, "w", encoding="utf-8") as f:
yaml.dump(t, f, Dumper=yaml.RoundTripDumper)
def all_case(rule="test*.py"):
'''加載所有的測試用例'''
case_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "case")
# 定義discover方法的參數
discover = unittest.defaultTestLoader.discover(case_path,
pattern=rule,
top_level_dir=None)
return discover
def run_case(all_case, reportName="report"):
'''
執行所有的用例, 並把結果寫入HTML測試報告
'''
curpath = os.path.dirname(os.path.realpath(__file__))
report_path = os.path.join(curpath, reportName) # 用例文件夾
# 如果不存在這個report文件夾,就自動創建一個
if not os.path.exists(report_path):os.mkdir(report_path)
report_abspath = os.path.join(report_path, "result.html")
print("report path:%s"%report_abspath)
fp = open(report_abspath, "wb")
runner = HTMLTestRunner_cn.HTMLTestRunner( stream=fp,
verbosity=2,
title=u'自動化測試報告,測試結果如下:',
description=u'用例執行情況:')
# 調用add_case函數返回值
runner.run(all_case)
fp.close()
if __name__ == "__main__":
token = login("admin", "111111") # 1.登錄
write_yaml(token) # 2.寫入yaml
allcases = all_case() # 3.加載用例
run_case(allcases)
2.執行完之后再report文件下生產一個測試報告
代碼下載
完整代碼可以在QQ群文件下載,QQ群:588402570
---------------------------------python接口自動化完整版-------------------------
全書購買地址 https://yuedu.baidu.com/ebook/585ab168302b3169a45177232f60ddccda38e695
也可以關注下我的個人公眾號:yoyoketang