pytest
pytest认知
pytest是Python的一个第三方单元测试框架,提供了更加丰富的扩展,更加简单、灵活,弥补了unittest在做web自动化测试的一些不足。
pytest支持pip安装
pip install -U pytest
查看pytest版本
pytest --version
对于pytest学习,可以参考:
pytest第一个简单例子
- 首先通过pytest编写一个简单的测试例子,test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
- 切换到测试用例目录下,执行【pytest】命令
(py3_heima) D:\zhenghou\python_learning>cd test_pytest
(py3_heima) D:\zhenghou\python_learning\test_pytest>pytest
=============================================================================== test session starts ===============================================================================
platform win32 -- Python 3.6.4, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
rootdir: D:\zhenghou\python_learning\test_pytest
collected 1 item
test_sample.py F [100%]
==================================================================================== FAILURES =====================================================================================
___________________________________________________________________________________ test_answer ___________________________________________________________________________________
def test_answer():
> assert inc(3) == 5
E assert 4 == 5
E + where 4 = inc(3)
test_sample.py:13: AssertionError
============================================================================= short test summary info =============================================================================
FAILED test_sample.py::test_answer - assert 4 == 5
================================================================================ 1 failed in 0.46s ================================================================================
注
:
- pytest更加简单,不需要想unittest一样必须创建测试类
- 使用assert断言
- pytest的测试文件和测试函数必须以【test】开头
此外,pytest也可以使用像unittest一样,通过main()方法执行测试用例
import pytest
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
if __name__ == "__main__":
pytest.main()
在一个类中执行多组测试
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert hasattr(x, "check")
进入test_class所在目录,执行【test_class.py】
(py3_heima) D:\zhenghou\python_learning\test_pytest>pytest -q test_class.py
.F [100%]
==================================================================================== FAILURES =====================================================================================
_______________________________________________________________________________ TestClass.test_two ________________________________________________________________________________
self = <test_pytest.test_class.TestClass object at 0x000002D1ACA22780>
def test_two(self):
x = "hello"
> assert hasattr(x, "check")
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_class.py:16: AssertionError
============================================================================= short test summary info =============================================================================
FAILED test_class.py::TestClass::test_two - AssertionError: assert False
1 failed, 1 passed in 0.38s
pytest 使用方法
断言
pytest单元测试框架没有提供专门的断言方法,而是直接使用Python的asser进行断言。
# 计算a + b
def add(a, b):
return a + b
# 判断是否为素数
def is_prime(n):
if n <= 1:
return False
else:
for i in range(2, n):
if n % i == 0:
return False
return True
# 测试相等
def test_add_1():
assert add(3, 4) == 7
# 测试不相等
def test_add_2():
assert add(5, 8) != 12
# 测试小于等于
def test_add_3():
assert add(5, 8) <= 10
# 测试大于等于
def test_add_4():
assert add(4, 5) >= 3
# 测试包含于
def test_in():
assert "h" in "hello"
# 测试不包含
def test_not_in():
assert "he" not in "hello"
# 测试是否为True
def test_true_1():
assert is_prime(1)
# 测试是否为True
def test_true_2():
assert is_prime(1) is True
# 测试是否为True
def test_true_3():
assert is_prime(1) is not True
# 测试是否为False
def test_false():
assert is_prime(1) is False
Fixture
Fixture通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境
# 功能函数:计算两个数相加
def add(a, b):
return a + b
# ==============fixture=======================
def setup_module(module):
print("setup_module=========================>")
def teardown_module(module):
print("teardown_module==========================>")
def setup_function(function):
print("setup_function=====================>")
def teardown_function(function):