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

[Python learning record for interface testing day4 - generate test report using allure]

編輯:Python

Before generating the report , You need to record what you have learned recently pytest Configuration of , Add... At the root of the project pytest.ini file , Then fill in

[pytest]
addopts = -vs # Command line arguments , Space off
testpaths = ./ # Path of test case
python_files = test_*.py # Rules for file names
python_classes = Test* # Rules for class names
python_functions = test # Rules for method names
makers = # Set the grouping of use cases
smoke:' Use cases '
usermanage

Here you can modify the configuration according to your own needs

install Allure

Okay , Start installing our allure 了

Download address :Central Repository: io/qameta/allure/allure-commandline

Download the required version

After downloading and decompressing , take bin The directory where the folder is located is placed in environment variable in , It also needs to be installed JDK

After completion , Command line pip install allure-pytest install allure plug-in unit , After installation, you can verify it , See if the installation is successful , You need to restart after the first installation pycharm

Generate test reports

Code up

import pytest
import requests
import yaml
import json
import os
import allure
from load_data import yaml_load
token = None
@pytest.mark.parametrize('data1',yaml_load.load('../data/user.yaml'))
def test_01(data1):
url_login = "https://***/jlcloud/api/login"
response1 = requests.request("POST", url=url_login, json=data1['user'])
print(response1.text)
result=json.loads(response1.text)
try:
global token # Define global variables
token=result['data']
print(token)
except:
pass
assert data1['msg'] == result['message']
# Get user information
def test_02():
print(token)
url_userinfo = "https://***/jlcloud/api/login/getUserInfo"
params = {"token": token}
res2 = requests.request("GET", url=url_userinfo, params=params)
print(res2.text)
result2 = json.loads(res2.text)
assert result2['message'] == " success "
if __name__ == '__main__': # pytest Operation mode
pytest.main('-vs',--alluredir= ./temp") # Generate json data , After generation, it can be in temp The file to view
os.system('allure generate ./pytest_case/temp -o report/html --clean') # take json The data generated , Report saved to report file 

Generated json The data are as follows :

stay report Click on the index.html File open with browser , The test report is as follows :

Here I run the file directly , I don't know why the report is not generated , But using the terminal command line  allure generate ./pytest_case/temp -o report/html --clean But it can generate

Another way is to pytest.ini Profile's addopts Minor modifications , You don't need to write parameters every time you run

[pytest]
addopts = -vs --alluredir ./temp # Generate json data
testpaths= ./pytest_case
python_files=test_*.py
python_classes=Test*
python_functions= test
import pytest
import requests
import yaml
import json
import os
import allure
from load_data import yaml_load
token = None
@pytest.mark.parametrize('data1',yaml_load.load('../data/user.yaml'))
def test_01(data1):
url_login = "https://***/jlcloud/api/login"
response1 = requests.request("POST", url=url_login, json=data1['user'])
print(response1.text)
result=json.loads(response1.text)
try:
global token # Define global variables
token=result['data']
print(token)
except:
pass
assert data1['msg'] == result['message']
# Get user information
def test_02():
print(token)
url_userinfo = "https://***/jlcloud/api/login/getUserInfo"
params = {"token": token}
res2 = requests.request("GET", url=url_userinfo, params=params)
print(res2.text)
result2 = json.loads(res2.text)
assert result2['message'] == " success "
if __name__ == '__main__': # pytest Operation mode
pytest.main()
os.system('allure generate ./pytest_case/temp -o report/html --clean')# take json Data generation report 


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