foreword
- Unlike unittest, pytest uses the assert keyword that comes with python to assert
- The assert keyword can be followed by an expression, as long as the final result of the expression is True, then the assertion passes and the use case executes successfully, otherwise the use case execution fails
assert little chestnut
I want to output some prompt information after an exception is thrown, and it is convenient to check the reason after execution
# 异常信息
def f():
return 3
def test_function():
a = f()
assert a % 2 == 0, "判断 a 为偶数,当前 a 的值为:%s" % a
Results of the
common assertion
The assertion in pytest is actually the assert assertion method in python. The commonly used methods are as follows
- assert xx: judge that xx is true
- assert not xx: judge that xx is not true
- assert a in b : judge that b contains a
- assert a == b : judge that a is equal to b
- assert a != b : judge that a is not equal to b
exception assertion
You can use pytest.raises as a context manager, and you can get the corresponding exception instance when an exception is thrown
# 断言异常
def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0
Assertion scene: assert whether the exception it throws is expected
Code execution: 1/0
Expected result: The exception thrown is ZeroDivisionError: division by zero
How to assert: usually the type and value of the assertion exception
Specific method: Here, the exception type of 1/0 is ZeroDivisionError, and the value of the exception is divisionby zero
# 详细断言异常
def test_zero_division_long():
with pytest.raises(ZeroDivisionError) as excinfo:
1 / 0
# 断言异常类型 type
assert excinfo.type == ZeroDivisionError
# 断言异常 value 值
assert "division by zero" in str(excinfo.value)
excinfo : is an instance of exception information
Main attributes: .type , .value , .traceback
Note: When asserting the type, the exception type does not need to be quoted, and when asserting the value value, it needs to be converted to str
Expansion 1: match
The match keyword argument can be passed to the context manager to test whether a regular expression matches the string representation of the exception
Note: This method can only assert value, not type
# 自定义消息
def test_zero_division_long():
with pytest.raises(ZeroDivisionError, match=".*zero.*") as excinfo:
1 / 0
The regexp parameter of the match method matches the re.search function, so match='zero' in the example above would also work
Extension 2: Check the assertion decorator
# 断言装饰器
@pytest.mark.xfail(raises=ZeroDivisionError)
def test_f():
1 / 0
Results of the
knowledge points
- The code throws an exception, but it matches the exception class specified by raises, so there is no assertion failure
- It is equivalent to a check exception decorator , function: check whether there is an exception, not sure whether there is an exception
- With pytest.raise(ZeroDivisionError) it may be better to use with pytest.raise(ZeroDivisionError) for cases where exception code is intentionally tested
而@pytest.mark.xfail(raises=ZeroDivisionError)
For checking for unfixed bugs (i.e., exceptions may occur), it may be better to use a checked assertion