前言
首先说下为什么要学pytest,此处给出两条理由,第一条已经足够:
学会了可以装大神
避免被面试官鄙视
pytest简介
pytest是python的一种单元测试框架,与Python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。它有如下特点:
非常容易上手,入门简单,文档丰富
支持简单的单元测试和复杂的功能测试
支持参数化
执行测试过程中可以跳过某些测试
支持重复运行失败的测试用例
支持运行由nose和unittest编写的测试用例
可以生成HTML报告
方便地和持续集成工具jenkins集成
支持执行部分用例
具有很多第三方插件,并且可以自定义扩展
安装pytest
安装方法
pip install -U pytest
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>pip install -U pytest
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pytest
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/d6/36/9e022b76a3ac440e1d750c64fa6152469f988efe0c568b945e396e2693b5/pytest-6.1.1-py3-none-any.whl (272kB)
Requirement already satisfied, skipping upgrade: pluggy<1.0,>=0.12 in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest) (0.13.1)
Requirement already satisfied, skipping upgrade: colorama; sys_platform == "win32" in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest) (0.4.3)
Requirement already satisfied, skipping upgrade: attrs>=17.4.0 in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest)(19.3.0)
Requirement already satisfied, skipping upgrade: iniconfig in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest) (1.0.1)
Requirement already satisfied, skipping upgrade: py>=1.8.2 in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest) (1.9.0)
Requirement already satisfied, skipping upgrade: packaging in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest) (20.4)
Requirement already satisfied, skipping upgrade: toml in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest) (0.10.0)
Requirement already satisfied, skipping upgrade: atomicwrites>=1.0; sys_platform == "win32" in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from pytest) (1.4.0)
Requirement already satisfied, skipping upgrade: six in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from packaging->pytest)(1.14.0)
Requirement already satisfied, skipping upgrade: pyparsing>=2.0.2 in c:\users\administrator\appdata\local\programs\python\python38\lib\site-packages (from packaging->pytest) (2.4.7)
Installing collected packages: pytest
Successfully installed pytest-6.1.1
查看安装版本
pip show pytest
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>pip show pytest
Name: pytest
Version: 6.1.1
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Bianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT
Location: c:\users\administrator\appdata\local\programs\python\python38\lib\sit-packages
Requires: packaging, colorama, toml, iniconfig, pluggy, attrs, atomicwrites, py
Required-by:
C:\Users\Administrator>pytest --version
pytest 6.1.1
C:\Users\Administrator>pytest -V
pytest 6.1.1
也可以通过pytest --verson或者pytest -V查看安装的版本
快速开始
1.新建一个test_sample.py文件,写一下代码
# content of test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
2.打开test_sample.py所在文件夹,cmd窗口输入:pytest
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\Administrator>G:
G:\>cd pytest
G:\pytest>pytest
============================= test session starts =============================
platform win32 -- Python 3.8.6, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: G:\pytest
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 1.98s ==============================
3.pytest运行规则:
查找当前目录及其子目录下以test_*.py或*_test.py文件,找到文件后,在文件中找到以test开头函数并执行。
测试类
前面是写的一个test开头的测试用例,当用例有多个的时候,写函数就不太合适了。这时可以把多个测试用例写到一个测试类里。
# content of 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')
2.打开test_class.py所在文件夹,cmd窗口输入:pytest -q test_class.py
G:\pytest>pytest -q test_class.py
.F [100%]
================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________
self =
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 2.50s
第一条测试用例通过,第二条测试用例失败,您可以在断言中轻松查看失败的原因。
3.pytest会找到符合规则(test_*.py和*_test.py)的所有测试,因此它发现两个test_前缀功能。如果只想运行其中一个,可以指定传递文件名test_class.py来运行该模块。
PS:-q,--quiet decrease verbosity(显示简单结果)
pytest -q test_class.py
pytest用例规则
测试文件以test_*.py或*_test.py命名
测试类以Test开头,并且不能带有__init__方法
测试函数以test_开头
断言使用assert