一、Pytest的安装
(1) 安装
pip install pytest
(2)查看版本,如下表示安装OK
$ pytest --version
pytest 6.2.5
(3)升级
pip install -U pytest
二、创建第一个测试脚本
(1)四行代码即可写一个测试脚本,如下,文件命名为 test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
(2)打开cmd窗口或者从pycharm进入终端,或者使用git shell,进入到test_sample.py文件所在的目录,然后执行pytest命令即开始执行脚本
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-1.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================
三、使用类组织测试函数
(1)使用类组织测试函数,注意不能写__init__()函数,创建test_class.py文件,代码如下:
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert hasattr(x, "check")
然后执行pytest test_class.py
$ pytest test_class.py
============================= test session starts =============================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\python39\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('G:\\src\\blog\\tests\\demo\\.hypothesis\\examples')
rootdir: G:\src\blog\tests, configfile: pytest.ini
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collecting ... collected 2 items
test_class.py::TestClass::test_one PASSED
test_class.py::TestClass::test_two FAILED
================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________
self = <test_class.TestClass object at 0x000001BC163991F0>
def test_two(self):
x = "hello"
> assert hasattr(x, "check")
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_class.py:8: AssertionError
=========================== short test summary info ===========================
FAILED test_class.py::TestClass::test_two - AssertionError: assert False
========================= 1 failed, 1 passed in 0.35s =========================
(2)使用类组织测试函数的好处
- 可以有效的组织用例
- 可以仅在类中共享fixture
- 可以在类上打标签从而对类中的所有用例打标签
(3)使用类组织测试函数时需要特别注意,勒种的每个用例都是测试类的一个独立的对象,类中的用例如果通过类属性共享变量将是非常糟糕的设计,如下:
# content of test_class_demo.py
class TestClassDemoInstance:
value = 0
def test_one(self):
self.value = 1
assert self.value == 1
def test_two(self):
assert self.value == 1
执行结果如下:
$ pytest -k TestClassDemoInstance -q
.F [100%]
================================= FAILURES =================================
______________________ TestClassDemoInstance.test_two ______________________
self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef0002>
def test_two(self):
> assert self.value == 1
E assert 0 == 1
E + where 0 = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef0002>.value
test_class_demo.py:9: AssertionError
========================= short test summary info ==========================
FAILED test_class_demo.py::TestClassDemoInstance::test_two - assert 0 == 1
1 failed, 1 passed in 0.12s