当前位置:网站首页>In depth interpretation of pytest official documents (26) customized pytest assertion error information
In depth interpretation of pytest official documents (26) customized pytest assertion error information
2022-07-02 03:14:00 【redrose2100】
Catalog
- Default error message
- rewrite pytest Assertion error message
- Rewrite common assertion error messages
Text
Default error message
For example, write the following test cases
def test_01():
assert 1==1
def test_02():
assert 1==2
def test_03():
assert "1"==1
The results are as follows :
$ pytest
============================== test session starts ===============================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\summer_ospp\summer_ospp_autotest\function_tests, configfile: pytest.ini
, testpaths: tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailure
s-10.1, xdist-2.3.0
collected 3 items
tests/test_demo.py::test_01 PASSED [ 33%]
tests/test_demo.py::test_02 FAILED [ 66%]
tests/test_demo.py::test_03 FAILED [100%]
==================================== FAILURES ====================================
____________________________________ test_02 _____________________________________
def test_02():
> assert 1==2
E assert 1 == 2
E +1
E -2
tests\test_demo.py:7: AssertionError
____________________________________ test_03 _____________________________________
def test_03():
> assert "1"==1
E AssertionError: assert '1' == 1
E +'1'
E -1
tests\test_demo.py:10: AssertionError
============================ short test summary info =============================
FAILED tests/test_demo.py::test_02 - assert 1 == 2
FAILED tests/test_demo.py::test_03 - AssertionError: assert '1' == 1
========================== 2 failed, 1 passed in 0.18s ===========================
For many testers , May be used to reading Chinese descriptions or more accurate Chinese descriptions , When you see the above error report , Although it is very clear from the description of error reporting , But from years of experience in automated testing , In fact, many testers or testers who write automated test scripts , When debugging or executing automated use cases, I still look blank when I see the above error reports , They prefer to see Chinese descriptions , Even more humanized Chinese description , Of course pytest The assertion of also provides a way to customize the error message in the script , For example, the following way , But it is found in the actual test development , Although this approach gives automated script developers the right to define themselves , But in the process of script development, if each assertion adds assertion error information in the following way , It takes up a lot of time , It's not convenient , There are even many assertions that the error message is actually similar , For example, judge whether two variables are equal , In fact, the assertion information is similar , And in the script, every assertion adds assertion error information, which will appear redundant , therefore , The other is in conftest.py The processing method of rewriting the assertion error information in is very easy to use
def test_03():
assert "1"==1,f" expect ’1‘ And 1 equal , Actual inequality "
rewrite pytest Assertion error message
In the root directory of the test case conftest.py Write the following code in : This is right == The error information of the operator is rewritten , For example, first judge whether it is a type , If it's not a type , Then it will directly prompt that the two data are not of the same type , If it is a type , In the numerical comparison , And the error information is described in Chinese
def pytest_assertrepr_compare(op, left, right):
if op == "==":
if not isinstance(right,type(left)):
return [
f" Assertion error , The data type of expected value and actual value is inconsistent , The expected value data type is :{
type(right)}, The actual value is :{
type(left)}",
f" The expected value is :{
right}, The actual value is :{
left}",
]
else:
return [
f" Assertion error , The expected value is inconsistent with the actual value , The expected value is :{
right}, The actual value is :{
left}",
]
At this time, write the following test cases , as follows , The assertion also adopts the following simple way , That is, don't add error information by yourself
def test_01():
assert 1==1
def test_02():
assert 1==2
def test_03():
assert "1"==1
The results are as follows : It can be seen that , Although there is no custom error message in the use case , The error message described in Chinese is still printed in the execution result , For such Chinese error information , I believe that any tester can know why it is wrong , I won't be confused by the full screen of English errors
$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\summer_ospp\summer_ospp_autotest\function_tests, configfile: pytest.ini, testpaths: tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items
tests/test_demo.py::test_01 PASSED [ 33%]
tests/test_demo.py::test_02 FAILED [ 66%]
tests/test_demo.py::test_03 FAILED [100%]
=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_02 ________________________________________________________________________________
def test_02():
> assert 1==2
E assert Assertion error , The expected value is inconsistent with the actual value , The expected value is :2, The actual value is :1
tests\test_demo.py:7: AssertionError
_______________________________________________________________________________ test_03 ________________________________________________________________________________
def test_03():
> assert "1"==1
E AssertionError: assert Assertion error , The data type of expected value and actual value is inconsistent , The expected value data type is :<class 'int'>, The actual value is :<class 'str'>
E The expected value is :1, The actual value is :1
tests\test_demo.py:10: AssertionError
======================================================================= short test summary info ========================================================================
FAILED tests/test_demo.py::test_02 - assert Assertion error , The expected value is inconsistent with the actual value , The expected value is :2, The actual value is :1
FAILED tests/test_demo.py::test_03 - AssertionError: assert Assertion error , The data type of expected value and actual value is inconsistent , The expected value data type is :<class 'int'>, The actual value is :<class 'str'>
===================================================================== 2 failed, 1 passed in 0.18s ======================================================================
Rewrite common assertion error messages
as follows , Rewrote ==,in,not in Assertion error message , Other things like >, <,>=,<=,!= The operator rewrite method is similar , Here is not a list ,conftest.py Write the following code in :
def pytest_assertrepr_compare(op, left, right):
if op == "==":
if not isinstance(right,type(left)):
return [
f" Assertion error , The data type of expected value and actual value is inconsistent , The expected value data type is :{
type(right)}, The actual value is :{
type(left)}",
f" The expected value is :{
right}, The actual value is :{
left}",
]
else:
return [
f" Assertion error , The expected value is inconsistent with the actual value , The expected value is :{
right}, The actual value is :{
left}",
]
if op == "in":
if isinstance(left,str) and isinstance(right,str):
return [
f" expect {
left} yes {
right} The string of , actual {
left} No {
right} The string of ,"
]
elif isinstance(right,list) or isinstance(right,set) or isinstance(right,tuple):
return [
f" expect {
left} Is a collection {
right} An element in , Actual set {
right} There is no {
left} Elements "
]
elif isinstance(right,dict):
return [
f" expect {
left} It's a dictionary {
right} One of them key, Actual Dictionary {
right} There is no value of {
left} Of key"
]
else:
return [
f" expect {
left} yes {
right} Part of , actually {
left} Not at all {
right} Part of "
]
if op == "not in":
if isinstance(left, str) and isinstance(right, str):
return [
f" expect {
left} No {
right} The string of , actual {
left} yes {
right} The string of ,"
]
elif isinstance(right, list) or isinstance(right, set) or isinstance(right, tuple):
return [
f" expect {
left} It's not a collection {
right} An element in , Actual set {
right} There is {
left} Elements "
]
elif isinstance(right, dict):
return [
f" expect {
left} Not a dictionary {
right} One of them key, Actual Dictionary {
right} There is a value of {
left} Of key"
]
else:
return [
f" expect {
left} No {
right} Part of , actually {
left} yes {
right} Part of "
]
The following is an example script
def test_01():
assert 1==1
def test_02():
assert 1==2
def test_03():
assert "1"==1
def test_04():
assert "aa" in "bbaa"
def test_05():
assert "aa" in "bba"
def test_06():
assert "aa" in ["aa","bb"]
def test_07():
assert "aa" in ("aa","bb")
def test_08():
assert "aa" in {
"aa","bb"}
def test_09():
assert "ab" in ["aa", "bb"]
def test_10():
assert "ab" in ("aa", "bb")
def test_11():
assert "ab" in {
"aa", "bb"}
def test_12():
assert "name" in {
"name":" Zhang Sanfeng ","age":100}
def test_13():
assert "gender" in {
"name":" Zhang Sanfeng ","age":100}
def test_14():
assert "aa" not in "bbaa"
def test_15():
assert "aa" not in "bba"
def test_16():
assert "aa" not in ["aa","bb"]
def test_17():
assert "aa" not in ("aa","bb")
def test_18():
assert "aa" not in {
"aa","bb"}
def test_19():
assert "ab" not in ["aa", "bb"]
def test_20():
assert "ab" not in ("aa", "bb")
def test_21():
assert "ab" not in {
"aa", "bb"}
def test_22():
assert "name" not in {
"name":" Zhang Sanfeng ","age":100}
def test_23():
assert "gender" not in {
"name":" Zhang Sanfeng ","age":100}
The results are as follows :
$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\summer_ospp\summer_ospp_autotest\function_tests, configfile: pytest.ini, testpaths: tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 23 items
tests/test_demo.py::test_01 PASSED [ 4%]
tests/test_demo.py::test_02 FAILED [ 8%]
tests/test_demo.py::test_03 FAILED [ 13%]
tests/test_demo.py::test_04 PASSED [ 17%]
tests/test_demo.py::test_05 FAILED [ 21%]
tests/test_demo.py::test_06 PASSED [ 26%]
tests/test_demo.py::test_07 PASSED [ 30%]
tests/test_demo.py::test_08 PASSED [ 34%]
tests/test_demo.py::test_09 FAILED [ 39%]
tests/test_demo.py::test_10 FAILED [ 43%]
tests/test_demo.py::test_11 FAILED [ 47%]
tests/test_demo.py::test_12 PASSED [ 52%]
tests/test_demo.py::test_13 FAILED [ 56%]
tests/test_demo.py::test_14 FAILED [ 60%]
tests/test_demo.py::test_15 PASSED [ 65%]
tests/test_demo.py::test_16 FAILED [ 69%]
tests/test_demo.py::test_17 FAILED [ 73%]
tests/test_demo.py::test_18 FAILED [ 78%]
tests/test_demo.py::test_19 PASSED [ 82%]
tests/test_demo.py::test_20 PASSED [ 86%]
tests/test_demo.py::test_21 PASSED [ 91%]
tests/test_demo.py::test_22 FAILED [ 95%]
tests/test_demo.py::test_23 PASSED [100%]
=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_02 ________________________________________________________________________________
def test_02():
> assert 1==2
E assert Assertion error , The expected value is inconsistent with the actual value , The expected value is :2, The actual value is :1
tests\test_demo.py:7: AssertionError
_______________________________________________________________________________ test_03 ________________________________________________________________________________
def test_03():
> assert "1"==1
E AssertionError: assert Assertion error , The data type of expected value and actual value is inconsistent , The expected value data type is :<class 'int'>, The actual value is :<class 'str'>
E The expected value is :1, The actual value is :1
tests\test_demo.py:10: AssertionError
_______________________________________________________________________________ test_05 ________________________________________________________________________________
def test_05():
> assert "aa" in "bba"
E assert expect aa yes bba The string of , actual aa No bba The string of ,
tests\test_demo.py:16: AssertionError
_______________________________________________________________________________ test_09 ________________________________________________________________________________
def test_09():
> assert "ab" in ["aa", "bb"]
E AssertionError: assert expect ab Is a collection ['aa', 'bb'] An element in , Actual set ['aa', 'bb'] There is no ab Elements
tests\test_demo.py:29: AssertionError
_______________________________________________________________________________ test_10 ________________________________________________________________________________
def test_10():
> assert "ab" in ("aa", "bb")
E AssertionError: assert expect ab Is a collection ('aa', 'bb') An element in , Actual set ('aa', 'bb') There is no ab Elements
tests\test_demo.py:33: AssertionError
_______________________________________________________________________________ test_11 ________________________________________________________________________________
def test_11():
> assert "ab" in {
"aa", "bb"}
E AssertionError: assert expect ab Is a collection {
'aa', 'bb'} An element in , Actual set {
'aa', 'bb'} There is no ab Elements
tests\test_demo.py:37: AssertionError
_______________________________________________________________________________ test_13 ________________________________________________________________________________
def test_13():
> assert "gender" in {
"name":" Zhang Sanfeng ","age":100}
E AssertionError: assert expect gender It's a dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} One of them key, Actual Dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} There is no value of gender Of key
tests\test_demo.py:43: AssertionError
_______________________________________________________________________________ test_14 ________________________________________________________________________________
def test_14():
> assert "aa" not in "bbaa"
E assert expect aa No bbaa The string of , actual aa yes bbaa The string of ,
tests\test_demo.py:46: AssertionError
_______________________________________________________________________________ test_16 ________________________________________________________________________________
def test_16():
> assert "aa" not in ["aa","bb"]
E AssertionError: assert expect aa It's not a collection ['aa', 'bb'] An element in , Actual set ['aa', 'bb'] There is aa Elements
tests\test_demo.py:52: AssertionError
_______________________________________________________________________________ test_17 ________________________________________________________________________________
def test_17():
> assert "aa" not in ("aa","bb")
E AssertionError: assert expect aa It's not a collection ('aa', 'bb') An element in , Actual set ('aa', 'bb') There is aa Elements
tests\test_demo.py:55: AssertionError
_______________________________________________________________________________ test_18 ________________________________________________________________________________
def test_18():
> assert "aa" not in {
"aa","bb"}
E AssertionError: assert expect aa It's not a collection {
'aa', 'bb'} An element in , Actual set {
'aa', 'bb'} There is aa Elements
tests\test_demo.py:58: AssertionError
_______________________________________________________________________________ test_22 ________________________________________________________________________________
def test_22():
> assert "name" not in {
"name":" Zhang Sanfeng ","age":100}
E AssertionError: assert expect name Not a dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} One of them key, Actual Dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} There is a value of name Of key
tests\test_demo.py:73: AssertionError
======================================================================= short test summary info ========================================================================
FAILED tests/test_demo.py::test_02 - assert Assertion error , The expected value is inconsistent with the actual value , The expected value is :2, The actual value is :1
FAILED tests/test_demo.py::test_03 - AssertionError: assert Assertion error , The data type of expected value and actual value is inconsistent , The expected value data type is :<class 'int'>, The actual value is :<class 'str'>
FAILED tests/test_demo.py::test_05 - assert expect aa yes bba The string of , actual aa No bba The string of ,
FAILED tests/test_demo.py::test_09 - AssertionError: assert expect ab Is a collection ['aa', 'bb'] An element in , Actual set ['aa', 'bb'] There is no ab Elements
FAILED tests/test_demo.py::test_10 - AssertionError: assert expect ab Is a collection ('aa', 'bb') An element in , Actual set ('aa', 'bb') There is no ab Elements
FAILED tests/test_demo.py::test_11 - AssertionError: assert expect ab Is a collection {
'aa', 'bb'} An element in , Actual set {
'aa', 'bb'} There is no ab Elements
FAILED tests/test_demo.py::test_13 - AssertionError: assert expect gender It's a dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} One of them key, Actual Dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} in ...
FAILED tests/test_demo.py::test_14 - assert expect aa No bbaa The string of , actual aa yes bbaa The string of ,
FAILED tests/test_demo.py::test_16 - AssertionError: assert expect aa It's not a collection ['aa', 'bb'] An element in , Actual set ['aa', 'bb'] There is aa Elements
FAILED tests/test_demo.py::test_17 - AssertionError: assert expect aa It's not a collection ('aa', 'bb') An element in , Actual set ('aa', 'bb') There is aa Elements
FAILED tests/test_demo.py::test_18 - AssertionError: assert expect aa It's not a collection {
'aa', 'bb'} An element in , Actual set {
'aa', 'bb'} There is aa Elements
FAILED tests/test_demo.py::test_22 - AssertionError: assert expect name Not a dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} One of them key, Actual Dictionary {
'name': ' Zhang Sanfeng ', 'age': 100} There is ...
==================================================================== 12 failed, 11 passed in 0.25s =====================================================================
Related to recommend :
Pytest In depth interpretation of official documents (1) Quick start
Pytest In depth interpretation of official documents (2) How to execute pytest Script
Pytest In depth interpretation of official documents (3) How to use assertions
Pytest In depth interpretation of official documents (4) How to use fixture
Pytest In depth interpretation of official documents (5) How to use mark
Pytest In depth interpretation of official documents (6) How to use parametrize A parameterized
Pytest In depth interpretation of official documents (7) How to use temporary directories and files
Pytest In depth interpretation of official documents (8) How to use monkey patch
Pytest In depth interpretation of official documents (9) How to perform a text test
Pytest In depth interpretation of official documents (10) How to re execute failed use cases
Pytest In depth interpretation of official documents (11) How to manage logs
Pytest In depth interpretation of official documents (12) How to capture standard output and standard error output
Pytest In depth interpretation of official documents (13) How to capture alarms
Pytest In depth interpretation of official documents (14) How to use skip and xfail
Pytest In depth interpretation of official documents (15) How to install and use plug-ins
Pytest In depth interpretation of official documents (16) How to write plugins
Pytest In depth interpretation of official documents (17) How to write hook Hook function
Pytest In depth interpretation of official documents (18) How to use pytest Test non installed local code
Pytest In depth interpretation of official documents (19) How to use pytest perform unittest Use cases
Pytest In depth interpretation of official documents (20) How to use pytest perform nose Use cases
Pytest In depth interpretation of official documents (21) How to use classic setup and teardown
Pytest In depth interpretation of official documents (22) How to set up bash Command line autocomplete
Pytest In depth interpretation of official documents (23) Loading principle of test script
Pytest In depth interpretation of official documents (24)Pytest Default use case discovery rules
Pytest In depth interpretation of official documents (25) modify Pytest Default use case discovery rules
Pytest In depth interpretation of official documents (26) Customize Pytest Assertion error message
边栏推荐
- 在QML中加载不同字体
- JDBC details
- Mongodb non relational database
- 跟着CTF-wiki学pwn——ret2shellcode
- Apple added the first iPad with lightning interface to the list of retro products
- Baohong industry | what misunderstandings should we pay attention to when diversifying investment
- A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
- 创业了...
- The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
- Qualcomm platform wifi-- WPA_ supplicant issue
猜你喜欢
MMSegmentation系列之训练与推理自己的数据集(三)
跟着CTF-wiki学pwn——ret2shellcode
2022-2028 global human internal visualization system industry research and trend analysis report
Continuous assignment of Verilog procedure
2022-2028 global military computer industry research and trend analysis report
[staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
About DNS
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
[JS reverse series] analysis of a customs publicity platform
随机推荐
Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
JS <2>
[staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
MongoDB非關系型數據庫
流线线使用阻塞还是非阻塞
32, 64, 128 bit system
2022-2028 global deep sea generator controller industry research and trend analysis report
tarjan2
Verilog 避免 Latch
MongoDB非关系型数据库
About DNS
Verilog timing control
Go execute shell command
2022-2028 global manual dental cleaning equipment industry research and trend analysis report
2022-2028 global aluminum beverage can coating industry research and trend analysis report
OSPF LSA message parsing (under update)
Qualcomm platform wifi-- WPA_ supplicant issue
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Form custom verification rules
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3