If you want to learn Pytest from scratch, you can check out this series of articles!
https://www.cnblogs.com/poloyy/category/1690628.html
Foreword
In addition to supporting the features of pytest (fixture, parametrize, xfail, skip), allure itself also has powerful features that can be used in pytest
@allure.step
- The most important point of the allure report is that it allows very detailed steps for each test case
- The @ allure.step () decorator allows test cases to display a more detailed test process in the allure report
Sample code
# ! / usr / bin / env python # -*-coding: utf-8-*- "" " __title__ = __Time__ = 2020-04-08 21:24 __Author__ = small pineapple test notes __Blog__ = https: // www. cnblogs.com/poloyy/ "" " import allure @ allure.step ( " first step " ) def passing_step (): pass @ allure.step ( " second step " ) def step_with_nested_steps (): nested_step () @allure. step ( " third step " ) def nested_step (): nested_step_with_arguments ( 1,'abc') @allure.step("第四步{0},{arg2}") def nested_step_with_arguments(arg1, arg2): pass @allure.step("第五步") def test_with_nested_steps(): passing_step() step_with_nested_steps()
Display of test cases on allure
Knowledge point
- step () has only one parameter, which is title, what you pass, what is displayed on allure
- It can be like a python string, it supports positional parameters and keyword parameters {0}, {arg2}, you can see the fourth step, if the function parameters are not matched, you will get an error
- The use scenario of step () gives me the feeling that it will be more useful when nesting between methods, otherwise only one step will be displayed, similar to the figure below
allure.attach (very useful)
Function: The allure report also supports the display of many different types of attachments, which can supplement the test results; output whatever you want, very good
语法: allure.attach(body, name, attachment_type, extension)
parameter list
- body : the content to be displayed (attachment)
- name : attachment name
- attachment_type : attachment type, is allure.attachment_type one inside
- extension : the extension of the attachment (less used)
What attachment types are provided by allure.attachment_type?
Syntax 2: allure.attach.file (source, name, attachment_type, extension)
source: file path, equivalent to transferring a file
Other parameters are the same as above
Code for one of the test cases
# ! / usr / bin / env python # -*-coding: utf-8-*- "" " __title__ = __Time__ = 2020-04-08 21:24 __Author__ = small pineapple test notes __Blog__ = https: // www. cnblogs.com/poloyy/ "" " import allure import pytest @ pytest.fixture def attach_file_in_module_scope_fixture_with_finalizer (request): allure.attach ( ' Add an attachment in the fixture pre-operation txt ' , ' fixture front attachment ' , allure.attachment_type .TEXT) def finalizer_module_scope_fixture (): allure.attach ( 'Txt add an attachment operation in which the rear fixture ', 'fixture后置附件', allure.attachment_type.TEXT) request.addfinalizer(finalizer_module_scope_fixture) def test_with_attacments_in_fixture_and_finalizer(attach_file_in_module_scope_fixture_with_finalizer): pass def test_multiple_attachments(): allure.attach('<head></head><body> 一个HTML页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML) allure.attach.file('./reports.html', attachment_type=allure.attachment_type.HTML)
See the result after running
This is a txt attachment
This is an allure.attach () to insert a piece of HTML and allure.attach.file () to import an existing HTML file (pytest-html report)