程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python requests+unittest+beautifulreport to realize a simple implementation of automated interface testing

編輯:Python

Everyone knows python requests The basic usage of , So let's apply the unit test framework unittest also BeautifulReport Simply implement the automatic interface test

One 、 Building framework

Let's also take a look at the figure below , The folders and files that need to be created are here , I use it on my side pycharm, Other development tools are similar , After the creation, we start to write code into it

We use wechat public platform to obtain token To test the interface , First, you need to get the interface related documents , We can enter the link below , You can look directly at api Document. , link https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html

The way to request is get, Here I list the required request parameters

Parameters

Whether must

explain

grant_type

yes

obtain access_token Fill in client_credential

appid

yes

Third party user unique credentials

secret

yes

Third party user unique credential key

Their own appid and secret How to get it , We need our own official account. , Here, huh , As shown in the figure , With the basic parameters , We can test it

We use a data-driven approach to testing , The data is in json In file , Let's write one first json The contents of the document are as follows , It says url, And the necessary parameters we requested

[
{
"path": {
"url": "https://api.weixin.qq.com/cgi-bin/token"
},
"params": {
"grant_type": "client_credential",
"appid": " Their own ",
"secret": " Their own "
}
}
]

The data is in data In the folder

Next is the writing of test cases , The assertion part is compared with the returned data , It can be seen that if the parameters are right , The returned data should be as follows , So with this 7200 The comparison , The returned data contains 7200, That means it's successful

{"access_token":"ACCESS_TOKEN","expires_in":7200}
from pyparsing import ParseExpression
import requests
import json
import unittest
from ddt import data, ddt, file_data
# from HTMLTestRunner import HTMLTestRunner
@ddt
class UnittestDemo(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
pass
@classmethod
def tearDownClass(cls) -> None:
pass
# use ddt Mode data driven , The address is written json The path of
@file_data("D:/interfacetest/apiDemo/data/testwechat.json")
def test_01(self, **kwargs):
print(kwargs["path"]["url"])
"""
Positive test case
:param kwargs:
:return:
"""
response = requests.get(url=kwargs["path"]["url"], params=kwargs["params"])
expectValue = '7200'
acutalValue = str(response.json()["expires_in"])
self.assertIn(expectValue, acutalValue)
print(" Request data :", kwargs)

Next is the main function , The main function we use BeautifulReport Realization , The specific code is as follows

from BeautifulReport import BeautifulReport
import unittest
if __name__ == '__main__':
discover = unittest.defaultTestLoader.discover("./test_case", pattern="testwechat.py")
BeautifulReport(discover).report(" Execute use cases ", filename="res.html", report_dir="./reports")

Take a look at the effect of implementation

Test report

The above is our simple automated interface test. We will update new content regularly in the future


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved