Python单元测试框架之pytest -- 断言

时间:2023-12-30 15:51:38

  对于测试来讲,不管是功能测试,自动化测试,还是单元测试。一般都会预设一个正确的预期结果,而在测试执行的过程中会得到一个实际的结果。测试的成功与否就是拿实际的结果与预期的结果进行比较。这个比的过程实际就是断言(assert)。

  在unittest单元测试框架中提供了丰富的断言方法,例如assertEqual()、assertIn()、assertTrue()、assertIs()等,而pytest单元测试框架中并没提供特殊的断言方法,而是直接使用python的assert进行断言。

  下面我们就来介绍assert 的使用。

比较大小与是否相等                      

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")

 定义一个add()函数,用于计算两个入参相加,并将相加的结果返回。

  而assert可以使用直接使用“==”、“!=”、“<”、“>”、“>=”、"<=" 等符号来比较相等、不相等、小于、大于、大于等于和小于等于。

  运行结果:

============================= 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 ======================

  显然,17加22的结果并不大于50,所有最后一条用例失败。

测试包含或不包含                                                    

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")

  通过定义a和b 字符串变量来比较包含的关系。

  assert 可以直接使用 in 和not in 来比较包含与不包含。

  运行结果:

============================= 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 ======================

  显然“hello”并不包含“hi”,所以第一条测试用例运行失败。

测试true或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")

  通过is_prime()函数来判断n 是否为素数(只能被1和它本身整除的数)。返回值为ture或false。

  通过assert不需要任何辅助符号,直接判断对象是否为ture,而assert not 用于判断是否为false。

  运行结果:

============================= 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 ===========================

  显示,对于第二条测试用例来讲,7是素数,所以,is_prime()函数的返回结果是Ture,而assert not 需要的正确结果是False,因此,用例执行失败。