1,Environment
Environment is an environment variable. The report has no variable parameters by default and needs to be configured by itself.
1.1. Add Environment
Create the environment.properties or environment.xml file and store the file in the folder of test results (the directory specified after the - allouredir option when executing the script).
For example:
pytest -n auto --alluredir=allure
Stored in the allure folder.
1. Add profile
Method 1: environment.properties
Document content:
Browser=Chrome Browser.Version=89.0.4389.128 Stand=Test ApiUrl=127.0.0.1/login python.Version=3.7.9
Method 2: environment.xml
Document content:
<environment> <parameter> <key>Browser</key> <value>Chrome</value> </parameter> <parameter> <key>Browser.Version</key> <value>89.0.4389.128</value> </parameter> <parameter> <key>Stand</key> <value>Test</value> </parameter> <parameter> <key>ApiUrl</key> <value>127.0.0.1/login</value> </parameter> <parameter> <key>python.Version</key> <value>3.7.9</value> </parameter> </environment>
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
3. Report display effect:
Note: in environment.xml mode, when the file content contains Chinese, the report displays normally.
Note: in environment.properties mode, when the file content contains Chinese, the report displays garbled code.
1.2. Solve the problem that the configuration file is deleted
When running pytest to generate an allure report, you sometimes need to add the parameter -- clean alluredir (clear the previous report records), and the configuration file (environment.properties or environment.xml) will also be deleted.
resolvent:
Store the configuration file (environment.properties or environment.xml) in the root directory of the project and copy it to the report directory before running the report.
Project directory structure:
Run the command (configuration file environment.properties)
pytest test_case.py --alluredir=allure --clean-alluredir cp environment.properties ./allure/environment.properties allure serve allure
Run the command (configuration file environment.xml)
pytest test_case.py --alluredir=allure --clean-alluredir cp environment.xml ./allure/environment.xml allure serve allure
2,Categories
Categories are categories (categories of test case results)
By default, there are two types of defects:
-
Product defects (test result: failed)
-
Test defects (test result: error/broken)
You can create a custom defect classification and add the categories.json file to the directory of test results (in the same directory as environment.properties).
categories.json parameter:
-
Name: classification name.
-
matchedStatuses: the running status of the test case. The default is ["failed", "broken", "passed", "skipped", "unknown"].
-
messageRegex: the error message of the test case running. The default is. *. It is matched through regular.
-
traceRegex: error stack information of test case running. The default is .* , It is matched by regularization.
Contents of categories.json file:
[
{
"name": "Ignored tests",
"matchedStatuses": ["skipped"]
},
{
"name": "Infrastructure problems",
"matchedStatuses": ["broken", "failed"],
"messageRegex": ".*signOut.*"
},
{
"name": "Outdated tests",
"matchedStatuses": ["broken"],
"traceRegex": ".*FileNotFoundException.*"
},
{
"name": "Product defects",
"matchedStatuses": ["failed"]
},
{
"name": "Test defects",
"matchedStatuses": ["broken"]
}
]
As shown in the figure: when the test case reports an error, the display effect
3,allure.step()
There are two ways to add test case steps in the allure report:
1. @ allure.step() this method will bring the function parameters and corresponding values.
2. with allure.step() makes the code more readable, but does not bring the passed parameters and corresponding values in the function.
3.1 @ allure.step() method
The allure report allows a very detailed step description of each test case. Through the @ allure.step() decorator, the test case can display a more detailed test process in the allure report.
@allure.step() has only one parameter, title. Enter the title and the allure report will be displayed.
1. Create test_allure_step.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure @allure.step("First step") def step_one(): pass @allure.step("Step 2") def step_two_with_nested(): step_three() @allure.step("Step 3") def step_three(): step_four_with_arguments(123456, 'AllTests') @allure.step("Step 4{0},{arg2}") def step_four_with_arguments(arg1, arg2): pass @allure.step("Step 5") def test_step_five(): step_one() step_two_with_nested()
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
Like Python strings, positional and keyword parameters are supported
For example, step 4 {0}, {arg2}
3.2. with allure.step() method
1. Create test_ allure_ Step 2.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure def one(): pass def two_with_nested(): three() def three(): pass def four(arg1, arg2): pass def five(): one() two_with_nested() def test_case(): with allure.step("First step"): one() with allure.step("Step 2"): two_with_nested() with allure.step("Step 3"): three() with allure.step("Step 4"): four(arg1=123456, arg2='AllTests') with allure.step("Step 5"): five()
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
When the fourth step is executed, the report does not bring the parameters and corresponding values in the function.
4,allure.attach
The allure report can display many different types of attachments that can supplement test, step, or device results.
You can call:
Mode 1:
allure.attach(body, name, attachment_type, extension)
Parameters:
-
body: the original content of the file to be written.
-
Name: attachment name.
-
attachment_ Type: attachment type (one of allure.attachment_type).
-
Extension: the extension of the created file.
allure.attachment_ Attachment type provided by type:
Mode 2:
allure.attach.file(source, name, attachment_type, extension)
Parameters:
source: a string containing the file path.
(other parameters are the same)
1. Create test_allure_attach.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import pytest import allure @pytest.fixture def attach_fixture_one(request): allure.attach('fixture Pre operation, add an attachment txt', 'fixture Front attachment', allure.attachment_type.TEXT) def attach_fixture_two(): allure.attach('fixture Post operation, add an attachment txt', 'fixture Rear attachment', allure.attachment_type.TEXT) request.addfinalizer(attach_fixture_two) def test_attach_fixture(attach_fixture_one): print("allure") def test_attach_fixture_file(): allure.attach('<head></head><body> a page </body>', 'page demo', allure.attachment_type.HTML) allure.attach.file('./demo.html', 'AllTests demo', attachment_type=allure.attachment_type.HTML)
Create a demo.html file (stored in the root directory of the project)
Document content:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>software test </title> </head> <body> <h1>AllTests</h1> </body> </html>
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
Use case test_attach_fixture
Use case test_attach_fixture_file, the first attachment is handwritten HTML, and the second attachment is an imported HTML file.
5,@allure.description()
Add a test case description in sufficient detail to make it easier to view the test steps.
There are three ways:
Mode 1:
@allure.description_html(str): Pass one HTML Code string
Mode 2:
@allure.description(str)
Mode 3:
Add under test case function """ """
1. Create test_allure_description.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure # Mode 1 @allure.description_html(""" <h1>Test with some complicated html description</h1> <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr align="center"> <td>William</td> <td>Smith</td> <td>50</td> </tr> <tr align="center"> <td>Vasya</td> <td>Jackson</td> <td>94</td> </tr> </table> """) def test_html_description(): assert True # Mode II @allure.description(""" Multiline test description This is a@allure.description Decorator """) def test_description_from_decorator(): assert 42 == int(6 * 7) # Mode III def test_unicode_in_docstring_description(): """ Multiline test description assert Assert """ assert 42 == int(6 * 7)
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
Mode 1:
Mode 2:
Mode 3:
6,@allure.title()
The test case title can be made more readable in a special way. The title supports parameter placeholders and dynamic replacement.
Example 1:
1. Create test_allure_title.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import pytest import allure @allure.title("Pre operation: Login") @pytest.fixture def test_login(request): params = request.param name = params["username"] pwd = params["pwd"] allure.attach(f"Parameters passed by test case{params}") print(name, pwd, params) yield name, pwd @allure.title("Login succeeded, test data:{test_login}") @pytest.mark.parametrize("test_login", [{"username": "admin", "pwd": "123456"}, {"username": "root", "pwd": "root"}], indirect=True) def test_login_success(test_login): name, pwd = test_login allure.attach(f"account number:{name},password:{pwd}")
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
Example 2:
1. Create test_allure_title2.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import pytest import allure @allure.title("Test Title Parameterization: {param1} + {param2}") @pytest.mark.parametrize('param1,param2,expected', [ (2, 2, 4), (1, 2, 5) ]) def test_with_parameterized_title(param1, param2, expected): assert param1 + param2 == expected
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
7,@allure.link()
Link (access link)
Decorator source code:
Parameters:
-
url: link to jump.
-
Name: the name displayed in the allure report. If it is not transmitted, the complete link will be displayed.
Similar decorators:
Bug link: @ allure.issue()
Test case link: @allure.testcase()
1. Create test_allure_link.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure @allure.link('https://www.baidu.com/') def test_with_link(): pass @allure.link('https://www.baidu.com/', name='use Baidu Search') def test_with_named_link(): pass
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
No name parameter
There is a name parameter
8,@allure.issue()
Link (Bug link)
Decorator source code:
Call link(), but link_type=LinkType.ISSUE.
Parameters:
-
url: link to jump.
-
Name: the name displayed in the allure report. If it is not transmitted, the complete link will be displayed.
Similar decorators:
Access link: @ allure.link()
Test case link: @allure.testcase()
1. Create test_allure_issue.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure @allure.issue('https://www.baidu.com/') def test_with_issue(): pass @allure.issue('https://www.baidu.com/', 'BUG link') def test_with_named_issue(): pass
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
No name parameter
There is a name parameter
9,@allure.testcase()
Link (test case link)
Decorator source code:
Call link(), but link_type=LinkType.TEST_CASE.
Parameters:
-
url: link to jump.
-
Name: the name displayed in the allure report. If it is not transmitted, the complete link will be displayed.
Similar decorators:
Access link: @ allure.link()
Bug link: @ allure.issue()
1. Create test_allure_testcase.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure @allure.testcase('https://www.baidu.com/') def test_with_testcase(): pass @allure.testcase('https://www.baidu.com/', 'Test case address') def test_with_named_testcase(): pass
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
No name parameter
There is a name parameter
10,@allure.epic()/feature()/story()
allure tag decorator (can be displayed on the test report):
@allure.epic: the concept of agile, defining epic, and next is feature.
@allure.feature: description of function points, understood as modules, followed by story.
@allure.story: story, followed by title.
Example 1:
1. Create test_allure_epic_feature_story.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure def test_without_any_annotations_that_wont_be_executed(): pass @allure.story('story1') def test_with_story1(): pass @allure.story('story2') def test_with_story2(): pass @allure.feature('feature3') @allure.story('story3') def test_with_feature3_story3(): pass @allure.epic('epic4') @allure.feature('feature4') @allure.story('story4') def test_with_epic4_feature4_story4(): pass @allure.epic('epic5') @allure.feature('feature5') class TestCase: @allure.story('story5_1') def test_with_story5_1(self): print("implement test_with_story5_1") @allure.story('story5_2') def test_with_story5_2(self): print("implement test_with_story5_2")
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
The home page features by stores area displays the set marks
Under the Behaviors column, test cases with tags display the tag hierarchy order
Example 2: run from the command line, and specify to run a tag (epic, feature, story)
Command line parameters:
-
--allure-epics
-
--allure-features
-
--allure-stories
1. Create test_allure_epic_feature_story2.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import pytest import allure import os @pytest.fixture(scope="session") def login_fixture(): print("===Pre operation-land===") @allure.step("Step 1") def step1(): print("===Operation step 1===") @allure.step("Step 2") def step2(): print("===Operation step 2===") @allure.step("Step 3") def step3(): print("===Operation step 3===") @allure.epic("General description 1") @allure.feature("Test module 1") class TestCaseAll1: @allure.testcase("https://www.baidu.com/", "test case") @allure.issue("https://www.baidu.com/", "Bug link") @allure.title("Use case title") @allure.story("story1") @allure.severity("critical") def test_case1(self, login_fixture): print("===Test case 1===") step1() step2() @allure.story("story2") def test_case2(self, login_fixture): print("===Test case 2===") step1() step3() @allure.epic("General description 2") @allure.feature("Test module 2") class TestCaseAll2: @allure.story("story3") def test_case3(self, login_fixture): print("===Test case 3===") step1() @allure.story("story4") def test_case4(self, login_fixture): print("===Test case 4===") step3() if __name__ == '__main__': pytest.main(['-s', '-q', '--alluredir', './allure']) os.system('allure -c ./allure') os.system('allure serve ./allure')
2. Operation results:
(1) Run all:
After execution, automatically open the browser to load the test report
(2) Command line specified run:
1) . only run the test case named "overall description 1" in epic
pytest --alluredir ./allure --allure-epics=General description 1
allure serve allure
Operation results:
2) . only run the test cases with feature name "test module 2"
pytest --alluredir ./allure --allure-features=Test module 2
allure serve allure
Operation results:
3) . only run the test cases of story1 and story3 (you can also run without a space of =)
pytest --alluredir ./allure test_allure_epic_feature_story2.py --allure-stories story1,story3
allure serve allure
Operation results:
4) Run the test case of the specified feature+story (you can also run without the = sign or space)
pytest --alluredir ./allure test_allure_epic_feature_story2.py --allure-features Test module 2 --allure-stories story2
allure serve allure
Operation results:
11,@allure.severity()
Used to mark the use case level.
Use case levels provided by Allure:
-
BLOCKER = 'blocker' blocking defect (function not implemented, unable to next step)
-
CRITICAL = 'critical' serious defect (missing function point)
-
NORMAL = 'normal' general defect (boundary condition, format error)
-
MINOR = 'minor' minor defect (interface error is inconsistent with UI requirements)
-
Trival = 'trival minor defect (no prompt for mandatory items, or non-standard prompt, etc.)
1, Example:
1. Create test_allure_severity.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import allure def test_with_no_severity(): pass @allure.severity(allure.severity_level.BLOCKER) def test_with_blocker_severity(): pass @allure.severity(allure.severity_level.CRITICAL) def test_with_critical_severity(): pass @allure.severity(allure.severity_level.MINOR) def test_with_minor_severity(): pass @allure.severity(allure.severity_level.TRIVIAL) def test_with_trivial_severity(): pass @allure.severity(allure.severity_level.NORMAL) def test_with_normal_severity(): pass @allure.severity(allure.severity_level.NORMAL) class TestClassWithNormalSeverity(object): def test_inside_the_normal_severity_test_class(self): """ Test class priority normal """ print("Test class priority normal") @allure.severity(allure.severity_level.CRITICAL) def test_inside_the_normal_severity_test_class_with_overriding_critical_severity(self): """ Test class priority normal Test case priority critical """ print("Test class priority normal;Test case priority critical") @allure.severity("normal") def test_case_1(): """ normal Level test case """ print("normal Level test case") @allure.severity("critical") def test_case_2(): """ critical Level test case """ print("critical Level test case") @allure.severity("blocker") def test_case_3(): """ blocker Level test case """ print("blocker Level test case") @allure.severity("minor") def test_case_4(): """ minor Level test case """ print("minor Level test case") @allure.severity("trivial") def test_case_5(): """ trivial Level test case """ print("trivial Level test case") def test_case_6(): """ unmark severity The default use case is normal """ print("unmark severity The default use case is normal") @allure.severity("normal") def test_case_7(): """ normal Level test case """ assert (1 == 2) @allure.severity("critical") def test_case_8(): """ critical Level test case """ assert (1 == 2) @allure.severity("blocker") def test_case_9(): """ blocker Level test case """ assert (1 == 2)
2. Enter the command to run:
pytest -n auto --alluredir=allure
allure serve allure
Operation results:
The Severity field marks the use case level
Statistics of use case priority in the chart
2, Command line parameter run:
Select the test cases to be run according to the priority
Parameters:
--allure-severities
For example: only run the test cases with severity=blocker and critical, and enter the command line
Writing method 1:
pytest test_allure_severity.py -sq --alluredir=allure --allure-severities=blocker,critical
Method 2:
pytest test_allure_severity.py -sq --alluredir=allure --allure-severities blocker,critical
Operation results:
Seven test cases were run according to the level of use cases to be run.