In testing, whether it's functional, automated, or unit testing, there is usually a predefined expected result. During test execution, an actual result is obtained. The success of the test depends on comparing the actual result with the expected result. This comparison process is known as assertion (assert).
The unittest testing framework provides various assertion methods, such as assertEqual(), assertIn(), assertTrue(), and assertIs(). However, the pytest testing framework does not provide specific assertion methods; instead, it directly uses Python's assert statement for assertions.
Below, we will explore the usage of assert.
Comparing Values and Equality
test_assert.py
#coding=utf-8
import pytest
def add(a, b):
return a + b
def test_add():
assert add(3, 4) == 7
def test_add2():
assert add(17, 22) != 50
def test_add3():
assert add(17, 22) <= 50
def test_add4():
assert add(17, 22) >= 50
if __name__ == '__main__':
pytest.main("test_assert.py")
A function called add() is defined to calculate the sum of two parameters and return the result.
The assert statement can use operators like ==, !=, <, >, <=, and >= to compare equality, inequality, less than, greater than, greater than or equal to, and less than or equal to.
Execution results:
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile:
plugins: html
collected 4 items
test_assert.py ...F
================================== FAILURES ===================================
__________________________________ test_add4 __________________________________
def test_add4():
> assert add(17, 22) >= 50
E assert 39 >= 50
E + where 39 = add(17, 22)
test_assert.py:22: AssertionError
===================== 1 failed, 3 passed in 0.02 seconds ======================
Clearly, the sum of 17 and 22 is not greater than 50, so the last test case fails.
Testing for Inclusion or Exclusion
test_assert2.py
#coding=utf-8
import pytest
def test_in():
a = "hello"
b = "he"
assert b in a
def test_not_in():
a = "hello"
b = "hi"
assert b not in a
if __name__ == '__main__':
pytest.main("test_assert2.py")
By defining string variables a and b, we check for inclusion relationships.
The assert statement can directly use in and not in to check for inclusion and exclusion.
Execution results:
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile:
plugins: html
collected 2 items
test_assert2.py F.
================================== FAILURES ===================================
___________________________________ test_in ___________________________________
def test_in():
a = "hello"
b = "hi"
> assert b in a
E assert 'hi' in 'hello'
test_assert2.py:9: AssertionError
===================== 1 failed, 1 passed in 0.01 seconds ======================
Clearly, 'hello' does not contain 'hi', so the first test case fails.
Testing True or False
test_assert3.py
#coding=utf-8
import pytest
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def test_true():
assert is_prime(13)
def test_true():
assert not is_prime(7)
if __name__ == '__main__':
pytest.main("test_assert3.py")
The is_prime() function determines whether a number is prime (a number divisible only by 1 and itself). It returns True or False.
The assert statement can directly check if an object is True, while assert not checks if its False.
Execution results:
============================= test session starts =============================
platform win32 -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2
rootdir: D:\pyse\pytest\test_case, inifile:
plugins: html
collected 1 items
test_assert3.py F
================================== FAILURES ===================================
__________________________________ test_true __________________________________
def test_true():
> assert not is_prime(7)
E assert not True
E + where True = is_prime(7)
test_assert3.py:22: AssertionError
========================== 1 failed in 0.01 seconds ===========================
It shows that for the second test case, 7 is a prime number, so the is_prime() function returns True, but assert not requires a False result, leading to the failure of the test case.