当前位置:网站首页>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 !!!!
边栏推荐
- Simple use of MySQL database: add, delete, modify and query
- librosa音频处理教程
- Entity Developer数据库应用程序的开发
- Day 246/300 ssh连接提示“REMOTE HOST IDENTIFICATION HAS CHANGED! ”
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
- Pymongo gets a list of data
- 简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- 18. Multi level page table and fast table
猜你喜欢
[ 英語 ] 語法重塑 之 動詞分類 —— 英語兔學習筆記(2)
kubernetes集群搭建Zabbix监控平台
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
WPF之MVVM
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
LeetCode 78:子集
Leetcode daily question (971. flip binary tree to match preorder traversal)
Setting and using richview trvstyle template style
ROS学习_基础
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
随机推荐
【刷题】怎么样才能正确的迎接面试?
ROS2安装及基础知识介绍
Proteus -- Serial Communication parity flag mode
How to find a medical software testing institution? First flight software evaluation is an expert
19. Actual memory management of segment page combination
PCL realizes frame selection and clipping point cloud
Depth residual network
SAP SD发货流程中托盘的管理
Apache dolphin scheduler source code analysis (super detailed)
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
[unity] how to export FBX in untiy
SSO process analysis
[brush questions] how can we correctly meet the interview?
(practice C language every day) reverse linked list II
医疗软件检测机构怎么找,一航软件测评是专家
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
Leetcode daily question (1870. minimum speed to arrive on time)
Simple query cost estimation