当前位置:网站首页>Interface automation test framework: pytest+allure+excel
Interface automation test framework: pytest+allure+excel
2022-07-06 06:56:00 【Ali test Jun】
1. Allure brief introduction
brief introduction
Allure The framework is a flexible one 、 Lightweight 、 Support multi language test report tool , It's not just about Web The way to show the test results of the profile , And it allows everyone involved in the development process to learn from the tests they perform on a daily basis , Extract useful information to the greatest extent .
【 At the end of the article, a lot of benefits are prepared for you 】
Allure By Java Language development , Support Pytest,JaveScript、PHP、Ruby etc. .
- from DEV/QA From the perspective of ,Allure Detailed test reports are provided , For example, it simplifies the statistics of common defects ; Failed tests can be divided into bug And interrupted tests ; You can also configure logs 、 step 、fixture、 The attachment 、 timing 、 Executive history ; As well as TMS、BUG Management system 、Jenkins Integration, etc. . therefore , Through the above configuration , All responsible developers and testers can master the test information as much as possible .
- From a manager's point of view ,Allure Provides a clear “ Big picture ”, namely High Level The statistical report of , These include features that have been covered 、 The location of the defect aggregation 、 Perform the appearance of the timeline and many other convenient things .Allure Modularity and extensibility ensure that you can always fine tune something , bring Allure Better for you .
Now let's talk about how to make the report show the information we need in more detail , as well as Allure And Jenkins Integration of .
install
(Windows/Mac General installation method )
Download address :https://github.com/allure-framework/allure2/releases, Download the required version of zip package .
install :
- decompression —> Get into bin Catalog —> function allure.bat
- hold bin Directory add Path environment variable
- coordination pytest, Use allure2 Generate more elegant test reports :pip install allure-pytest
Operation method
Collect results during test execution
pytest [ The test file ] -s -q --alluredir=./result/ --clean-alluredir
- -s: Indicates that the case log of successful execution will be printed
- -q: If it follows the file execution path, it represents only the file to be executed
- --alluredir: Specify the path to store the test results ( If the directory does not exist, a new )
- --clean-alluredir: Clear historical result data
View test report
Mode one : Used to display the results after local rendering
allure serve ./result/
Mode two : For rendering and viewing results locally
# Generate a report allure generate ./result/ -o ./report/ --clean # Be careful : Overlay path plus --clean # Open the report allure open -h 127.0.0.1 -p 8883 ./report/
Be careful :/report/ In the directory index.html Is the final result page , But you can't see the actual report content when you open this file directly through the browser , This is because the actual report content needs allure After rendering, you can see .
2. Allure Common features
scene :
- Hope to see the test function in the report , Subfunction or scene , testing procedure , Include test additional information .
solve :
- import allure
- @allure.feature(' The name of the function ')
- @allure.story(' Subfunction name ')
- @allure.title(' Test case name ')
- @allure.step(' Step details ')
- @allure.description(' Test case description ')
- @allure.attach(' Specific text information '): Additional information is required , It can be data , Text , picture , video , Webpage
- If you only test the login function, you can add restrictions and filters when running , Such as :pytest file name --allure_features ' Shopping cart function ' --allure_stories ' Add to cart '

@alllure.feature() And @allure.store() The relationship between
feature Equivalent to a large function or module . take case Sort into a feature in , And in the report behaviors It shows that , amount to testsuite.
story Equivalent to branch function / modular , Belong to feature The structure under , And in the report features It shows that , amount to testcase.
feature And story Similar to a parent-child relationship .
@allure.step() And with allure.step() The difference between
- Every step of the test process , Generally put in specific logical methods .
- Can be placed in key steps , Show in report .
- stay App、Web In automated testing , It is recommended to switch to a new page as a step.
- usage :
- @allure.step(): Can only be placed on a class or method in the form of a decorator .
- with allure.step(): It can be put in the test case method , But the code for the test step needs to be included in the statement .
Prioritize test cases
scene :
Usually there is a smoke test 、 regression testing 、 Online verification test, etc , Then it needs to be executed according to the importance level , For example, when you go online, you should run through the main process and important modules .
solve :
- By attaching pytest.mark Tag description
- adopt allure.feature、allure.story Tag description
- adopt allure.severity Directly mark the use case level
Test cases are graded according to their importance , If you don't specify a level , The default is NORMAL Level :
- BLOCKER: Blocking defects ( Function not implemented , There's no next step )
- CRITICAL: Serious defects ( Missing function point )
- NORMAL: General defects ( Boundary situation , Format error )
- MINOR: Minor defects ( Interface error and ui Demand discrepancy )
- TRIVIAL: Minor defects ( Must be silent , Or the prompt is not standard )
step :
- In the method 、 Add... To functions and classes :@allure.severity(allure.severity_level.TRIVIAL)
- Specify the use case at the corresponding level of execution :pytest -s -v file name --allure-severities normal, critical
to Allure Add content to the test report ( picture 、 The attachment 、 Text 、 Screenshot 、HTML etc. )
scene :
- Front end automated testing often requires additional pictures or html, Like in the right place 、 Screenshots at the right time, etc .
solve :
- @allure.attach() Show many different types of provided attachments , You can add tests 、 Steps or test results .
step :
- Attach a web page to the test report :
- Format :allure.attach(body( Content ), name, attachment_typeextension)
- Example :allure.attach('<head>/head><body> home page </body>', ' This is the result message of the error page ', allure.attachment_type.HTML)
- Attach pictures to the test report :
- Format :allure.attach.file(source, name, attachment_type, extension)
- Example :allure.attach.file("./result/b.png", attachment_type=allure.attachment_type.PNG)
Integrated test management system
@allure.link()、@allure.issue()、@allure.testcase() The main purpose is to Allure Report and test management system integration , You can jump to the company's internal address more quickly .
Let's take a look at the source code of the three decorators :

def link(url, link_type=LinkType.LINK, name=None):
return safely(plugin_manager.hook.decorate_as_link(url=url, link_type=link_type, name=name))
def issue(url, name=None):
return link(url, link_type=LinkType.ISSUE, name=name)
def testcase(url, name=None):
return link(url, link_type=LinkType.TEST_CASE, name=name)
Summary
- issue() and testcase() In fact, the call is also link(), It's just link_type Dissimilarity .
- Must pass parameters url: Jump links .
- Optional parameters name: Displayed in the Allure Name of the report , If it doesn't, it shows the complete link ( Suggestion biography , Otherwise, the readability is not high ).
- Can be interpreted as : The three methods are the same , We all offer jump links and names , It's just linked type Dissimilarity , Finally displayed Different styles nothing more (type Dissimilarity , Different styles ).
- If you like , Only @allure.link() It's fine too .
- The reason for the emergence of three ornaments is to better integrate Categorize links ( Access link 、Bug link 、 Test case link ).
Code example

import allure
TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'
@allure.link('https://www.youtube.com/watch?v=4YYzUTYZRMU')
def test_with_link():
pass
@allure.link('https://www.youtube.com/watch?v=Su5p2TqZxKU', name=' Click on me to have a look youtube Well ')
def test_with_named_link():
pass
@allure.issue('140', 'bug issue link ')
def test_with_issue_link():
pass
@allure.testcase(TEST_CASE_LINK, ' Test case address ')
def test_with_testcase_link():
pass
Running results , see Allure The report
1)@allure.link() Don't pass on name The style of the parameter
As shown in the figure below , Don't pass on name when , When the link is long , The readability is poor .

2)@allure.link() Yes name The style of the parameter

3)@allure.testcase() The style of
As shown in the figure below , and link() Yes name Parameters are the same :

4)@allure.issue() The style of
As shown in the figure below , More bug style :

3. Example of interface automation test framework
Complete the project :https://github.com/juno3550/InterfaceAutoTestWithPytest
Example of test method

1 import pytest
2 import allure
3 import logging
4 from util.assert_util import assert_keyword
5 from util.request_util import api_request
6 from util.global_var import *
7 from util.excel_util import excel_util
8
9
10 register_test_data = excel_util.get_sheet_data(" register ")
11 login_test_data = excel_util.get_sheet_data(" Sign in ")
12
13
14 @allure.feature(" Login module ")
15 @pytest.mark.dependency(name="TestLoginModule")
16 class TestLoginModule:
17
18 @allure.story(" Registration function ")
19 @allure.title(' User registration ') # Specify the test case Title , The default is the function name
20 @allure.description(' User registration through the interface ') # Add test case description
21 @allure.severity(allure.severity_level.BLOCKER) # Blocking level
22 @pytest.mark.run(order=1)
23 @pytest.mark.parametrize('case_data', register_test_data)
24 def test_register(self, case_data):
25 with allure.step(" Read request data , Call interface "):
26 logging.info(" Interface use case data :%s" % case_data)
27 response = api_request(case_data[API_IP], case_data[API_URI], case_data[REQUEST_METHOD],
28 case_data[API_REQUEST_DATA], case_data[RESPONSE_EXTRACT_VAR],
29 case_data[REQUEST_HEADER], case_data[REQUEST_COOKIE])
30 with allure.step(" Get response data , To assert that "):
31 assert_keyword(response, case_data[RESPONSE_ASSERT_KEYWORD])
32
33 @allure.story(" Login function ")
34 @allure.title(' The user login ') # Specify the test case Title , The default is the function name
35 @allure.description(' User login through the interface ') # Add test case description
36 @allure.severity(allure.severity_level.BLOCKER) # Blocking level
37 @pytest.mark.run(order=2)
38 @pytest.mark.parametrize('case_data', login_test_data)
39 def test_login(self, case_data):
40 with allure.step(" Read request data , Call interface "):
41 logging.info(" Interface use case data :%s" % case_data)
42 response = api_request(case_data[API_IP], case_data[API_URI], case_data[REQUEST_METHOD],
43 case_data[API_REQUEST_DATA], case_data[RESPONSE_EXTRACT_VAR],
44 case_data[REQUEST_HEADER], case_data[REQUEST_COOKIE])
45 with allure.step(" Get response data , To assert that "):
46 assert_keyword(response, case_data[RESPONSE_ASSERT_KEYWORD])
47
48
49 if __name__ == "__main__":
50 test_dir = os.path.dirname(__file__)
51 pytest.main(['-s', '-q', test_dir, '--alluredir', '../test_result/', "--clean-alluredir"])
52 os.system('allure generate ../test_result/ -o ../test_report/ --clean')
53 os.system('allure open -h 127.0.0.1 -p 8881 ../test_report/')
Test data examples

Allure Examples of reporting results


a key : Learning materials of course, learning is inseparable from materials , Of course, here is also prepared for you 600G Learning materials
Private keywords needed 【000】 Get it for free Note that the keywords are :000
Project practice :

Large e-commerce platform :

Full set of software test automation test teaching video

300G Download tutorial materials 【 Video tutorial +PPT+ Project source code 】

A complete set of software testing automation testing factory has been

python automated testing ++ A complete set of templates + Performance testing


It's said that the iron juice who has paid attention to me for three consecutive years has been promoted, raised and made a fortune !!!!
边栏推荐
- Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
- librosa音频处理教程
- [Yu Yue education] flower cultivation reference materials of Weifang Vocational College
- Reflex WMS中阶系列3:显示已发货可换组
- Hydra common commands
- The registration password of day 239/300 is 8~14 alphanumeric and punctuation, and at least 2 checks are included
- ROS learning_ Basics
- Leetcode daily question (1997. first day where you have been in all the rooms)
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
猜你喜欢

18. Multi level page table and fast table

A brief introduction of reverseme in misc in the world of attack and defense

WPF之MVVM

Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation

机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-

Basic commands of MySQL

Database basics exercise part 2

19.段页结合的实际内存管理

Machine learning plant leaf recognition

Chapter 7 - thread pool of shared model
随机推荐
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Leetcode daily question (1870. minimum speed to arrive on time)
雲上有AI,讓地球科學研究更省力
Chapter 7 - thread pool of shared model
Bitcoinwin (BCW): the lending platform Celsius conceals losses of 35000 eth or insolvency
Latex文字加颜色的三种办法
L'Ia dans les nuages rend la recherche géoscientifique plus facile
万丈高楼平地起,每个API皆根基
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
【每日一题】729. 我的日程安排表 I
从autojs到冰狐智能辅助的心里历程
How to reconstruct the class explosion caused by m*n strategies?
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
UDP攻击是什么意思?UDP攻击防范措施
RichView TRVStyle 模板样式的设置与使用
[hot100] 739. Température quotidienne
(practice C language every day) reverse linked list II
19. Actual memory management of segment page combination
编译,连接 -- 笔记 -2
Introduction to ros2 installation and basic knowledge