用 pytest設(shè)計(jì)簡(jiǎn)單接口測(cè)試框架--學(xué)習(xí)筆記

設(shè)計(jì)框架目錄

見名知意,根據(jù)自己的習(xí)慣喜好創(chuàng)建名稱
Api:根據(jù)接口文檔,編寫單接口API
Common:公用方法
Config:配置文件
Data:數(shù)據(jù)參數(shù)
Log:日志相關(guān)
Main:主方法執(zhí)行
Report:測(cè)試報(bào)告
TestCase:測(cè)試用例,斷言結(jié)果

在 Common 里封裝方法

封裝request方法

創(chuàng)建 requests.py 文件,將常用方法封裝,Api里的單接口可直接調(diào)用,我這里只封裝 get和 post 請(qǐng)求

import requests
class request:
    def get(self,**kwargs):
        '''封裝 get 方法'''
        url = kwargs.get('url')
        headers = kwargs.get('headers')
        params = kwargs.get('params')

        result=requests.get(url=url,headers=headers,params=params)
        return result.json()

    def post(self,**kwargs):
        '''封裝 post 方法'''
        url = kwargs.get('url')
        headers = kwargs.get('headers')
        json = kwargs.get('json')
        params = kwargs.get('params')
        data = kwargs.get('data')

        result = requests.post(url=url, headers=headers,json=json,data=data)
        return result.json()

Api 下寫單接口 api

以登錄接口為例,繼承 requests.py的 request,測(cè)試一下是否登錄成功

from apiAutomation.Common.requests import request
class login(request):
    def login(self):
        url='http://***/login.html'
        headers= {'X-Requested-With': 'XMLHttpRequest'}
        data={'user': 'aa', 'pwd': '123456'}

        result=request.post(self,url=url,headers=headers,data=data)


        return result

上述 url headers data 數(shù)據(jù)可以提取出來放到 yaml文件中管理

3.創(chuàng)建yaml文件

在 Data 中創(chuàng)建 data.yaml 文件,將需要的數(shù)據(jù)寫入文件中
如下:

login_url: 'http://***/login.html'
headers: {'X-Requested-With': 'XMLHttpRequest'}
data: {'user': 'aa', 'pwd': '123456'}

4.封裝讀取yaml文件的方法

在 Common 中創(chuàng)建 getData.py,獲取 yaml 中的數(shù)據(jù),存入 data 對(duì)象里

import yaml
class getData:

    def __init__(self):
        self.path='../Data/data.yaml'
        with open(self.path, 'r', encoding='utf-8') as f:
            self.data = yaml.safe_load(f)
    def get_data(self):
        return self.data

5. API請(qǐng)求通過 yaml參數(shù)化

將單接口 api 優(yōu)化一下,login()里面的參數(shù)通過 yaml 管理,login_v()封裝成傳參數(shù)的形式

from apiAutomation.Common.requests import request
from apiAutomation.Common.getData import getData
class login(request):
    def __init__(self):
        self.data=getData().get_data()

    def login(self):
        url=self.data['login_url']
        headers= self.data['headers']
        data=self.data['data']

        result=request.post(self,url=url,headers=headers,data=data)
        return result

    def login_v(self,**kwargs):
        url = kwargs.get('url')
        headers = kwargs.get('headers')
        json = kwargs.get('json')
        params = kwargs.get('params')
        data = kwargs.get('data')
        result=request.post(self,url=url,headers=headers,data=data)

        return result

在 testCase 下創(chuàng)建 case

創(chuàng)建 test_login.py 文件

from apiAutomation.Api.loginApi import class_login
import pytest
class Test_case:
    def test_login(self):
        obj=class_login()
        result=obj.login().json()
        msg=result['msg']
        assert msg=='登錄成功'

if __name__ == '__main__':
    pytest.main(['-s','test_login.py'])

執(zhí)行測(cè)試

plugins: allure-pytest-2.8.16, assume-2.4.2, html-3.1.1, metadata-1.11.0
collected 1 item

test_login.py .

============================== 1 passed in 0.17s ===============================

Process finished with exit code 0

使用allure生成報(bào)告

安裝 allure

pip3 install allure-pytest

命令行執(zhí)行測(cè)試,并收集結(jié)果 --alluredir 提供存儲(chǔ)結(jié)果的路徑

python3 -m pytest -s --alluredir ./apiAutomation/Reports/Raw/

在 apiAutomation/Reports下會(huì)產(chǎn)生一個(gè)目錄 Raw,里面有一些名字比較奇怪的文件 如下圖:


image.png

執(zhí)行命令,生成好看的報(bào)告

allure generate ./apiAutomation/Reports/Raw/ -o ./apiAutomation/Reports/html/ --clean

在apiAutomation/Reports/中會(huì)生成 html 文件夾,index.html 就是生成的界面報(bào)告,在 pycharm 中選擇該文件,右鍵選擇 open in Browser 選擇喜歡的瀏覽器打開就可以了


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容