If you want to learn from scratch Pytest, you can look at this series of articles Oh!
https://www.cnblogs.com/poloyy/category/1690628.html
Foreword
- Unlike unittest, pytest using python comes to assert assert keyword
- assert keyword followed by one expression can, as long as the final result of the expression is True, then the assertion by the successful implementation of the use case, or else fails use case
assert small chestnuts
I want to output some tips after throwing an exception after executing it easy to see what is the reason the
# Abnormality information DEF F (): return . 3
DEF test_function (): a = F () Assert a% 2 == 0, " determines a is an even number, a current value:% S " % a
Results of the
Common assertions
pytest assertion which is actually inside the python assert assertion methods commonly used are the following
- assert xx: xx determination is true
- assert not xx: xx judge is not true
- assert a in b: b comprising a Analyzing
- assert a == b: a = b Analyzing
- assert a = b:! a = b Analyzing
Abnormal assertion
Pytest.raises can be used as a context manager, it can be acquired when an exception is thrown to the corresponding abnormal category
# Assertion abnormal DEF test_zero_division (): with pytest.raises (ZeroDivisionError): 1/0
Assertion scene:
the assertion that it is not expected to throw exceptions you want
Code execution:
1/0
Expected result:
cast exception is ZeroDivisionError: division by zero
How asserted:
usually asserted type and value of the abnormal values
DETAILED DESCRIPTION:
Here exception type is 1/0 ZeroDivisionError, abnormal values divisionby zero value
# Assert detailed abnormal DEF test_zero_division_long (): with pytest.raises (ZeroDivisionError) AS excinfo: 1/ 0 # assertion exception type of the type the Assert excinfo.type == ZeroDivisionError # assertion abnormal value value the Assert " Division by ZERO " in str (excinfo. value)
excinfo
: it is an example of the abnormality information
The main properties:
.type
,
.Value
,
.traceback
Note: The
assertion type when abnormal type is unquoted, the assertion value when the value to be transferred str