1. 概述

pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:

  • 1、简单灵活,容易上手,文档丰富;
  • 2、支持参数化,可以细粒度地控制要测试的测试用例;
  • 3、能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);
  • 4、pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;
  • 5、测试用例的skip和xfail处理;
  • 6、可以很好的和CI工具结合,例如jenkins

2. 使用介绍

2.1. 安装

pip install pytest

2.2. 示例代码

编写规则

编写pytest测试样例非常简单,只需要按照下面的规则:

  • 测试文件以test_开头(以_test结尾也可以)
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试函数以test_开头
  • 断言使用基本的assert即可

pytest1.py

# -*- coding:utf-8 -*-
import pytest @pytest.fixture(scope='function')
def setup_function(request):
def teardown_function():
print("teardown_function called.")
request.addfinalizer(teardown_function) # 此内嵌函数做teardown工作
print('setup_function called.') @pytest.fixture(scope='module')
def setup_module(request):
def teardown_module():
print("teardown_module called.")
request.addfinalizer(teardown_module)
print('setup_module called.') @pytest.mark.website
def test_1(setup_function):
print('Test_1 called.') def test_2(setup_module):
print('Test_2 called.') def test_3(setup_module):
print('Test_3 called.')
assert 2==1+1 # 通过assert断言确认测试结果是否符合预期

fixture的scope参数

scope参数有四种,分别是'function','module','class','session',默认为function。

  • function:每个test都运行,默认是function的scope
  • class:每个class的所有test只运行一次
  • module:每个module的所有test只运行一次
  • session:每个session只运行一次

setup和teardown操作

  • setup,在测试函数或类之前执行,完成准备工作,例如数据库链接、测试数据、打开文件等
  • teardown,在测试函数或类之后执行,完成收尾工作,例如断开数据库链接、回收内存资源等
  • 备注:也可以通过在fixture函数中通过yield实现setup和teardown功能

2.3. 测试结果

如何执行

  • pytest # run all tests below current dir
  • pytest test_mod.py # run tests in module file test_mod.py
  • pytest somepath # run all tests below somepath like ./tests/
  • pytest -k stringexpr # only run tests with names that match the
    # the "string expression", e.g. "MyClass and not method"
    # will select TestMyClass.test_something
    # but not TestMyClass.test_method_simple
  • pytest test_mod.py::test_func # only run tests that match the "node ID",
    # e.g "test_mod.py::test_func" will be selected
    # only run test_func in test_mod.py

通过pytest.mark对test方法分类执行

通过@pytest.mark控制需要执行哪些feature的test,例如在执行test前增加修饰@pytest.mark.website

  • 通过 -m "website" 执行有website标记的test方法
$ pytest  -v -m "website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python
cachedir: .cache
Using --randomly-seed=1522925202
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items pytest1.py::test_1 PASSED ============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
=============================================================================== 2 tests deselected ===============================================================================
=========================================================== 1 passed, 2 deselected, 1 pytest-warnings in 0.00 seconds ============================================================
  • 通过 -m "not website" 执行没有website标记的test方法
$ pytest  -v -m "not website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python
cachedir: .cache
Using --randomly-seed=1522925192
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items pytest1.py::test_3 PASSED
pytest1.py::test_2 PASSED ============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
=============================================================================== 1 tests deselected ===============================================================================
=========================================================== 2 passed, 1 deselected, 1 pytest-warnings in 0.00 seconds ============================================================

Console参数介绍

  • -v 用于显示每个测试函数的执行结果
  • -q 只显示整体测试结果
  • -s 用于显示测试函数中print()函数输出
  • -x, --exitfirst, exit instantly on first error or failed test
  • -h 帮助

Case 1

$ pytest -v pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python
cachedir: .cache
Using --randomly-seed=1522920341
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items pytest1.py::test_1 PASSED
pytest1.py::test_3 PASSED
pytest1.py::test_2 PASSED ============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds ===================================================================

Case 2

$ pytest -s pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1
Using --randomly-seed=1522920508
rootdir: /home/kevin/learn/python-web/tox/case2, inifile:
plugins: randomly-1.0.0, mock-1.2, cov-2.0.0
collected 3 items pytest1.py setup_function called.
Test_1 called.
.teardown_function called.
setup_module called.
Test_2 called.
.Test_3 called.
.teardown_module called. ============================================================================= pytest-warning summary =============================================================================
WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead.
================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds ===================================================================

3. 扩展插件

3.1. 测试报告

安装与样例

pip install pytest-cov # 计算pytest覆盖率,支持输出多种格式的测试报告
pytest --cov-report=html --cov=./ test_code_target_dir

Console参数介绍

  • --cov=[path], measure coverage for filesystem path (multi-allowed), 指定被测试对象,用于计算测试覆盖率
  • --cov-report=type, type of report to generate: term, term-missing, annotate, html, xml (multi-allowed), 测试报告的类型
  • --cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件
  • --no-cov-on-fail, do not report coverage if test run fails, default: False,如果测试失败,不生成测试报告
  • --cov-fail-under=MIN, Fail if the total coverage is less than MIN. 如果测试覆盖率低于MIN,则认为失败

Console Result

---------------------------------------------------------------- coverage: platform linux2, python 2.7.14-final-0 ----------------------------------------------------------------
Name Stmts Miss Cover
--------------------------------
pytest1.py 18 0 100%

Html Result

 
image.png

3.2. 测试顺序随机

pip install pytest-randomly

3.3. 分布式测试

pip install pytest-xdist

3.4. 出错立即返回

pip install pytest-instafail

4. 参考

作者:红薯爱帅
链接:https://www.jianshu.com/p/a754e3d47671
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Pytest - 使用介绍的更多相关文章

  1. Pytest框架介绍

    Pytest框架介绍.安装 pytest是python测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,功能更强大 pytest特征 1:断言提示信 ...

  2. pytest一:pytest 框架介绍

    pytest 是 python 的一种单元测试框架,与python 自带的 unittest测试框架类似,但是比 unittest 框架使用起来更简洁,效率更高.根据pytest 的官方网站介绍,它具 ...

  3. pytest框架-介绍、Mark(打标签)、命令运行用例、用例执行顺序、

    1.pytest介绍:基于unittest 之上的单元测试框架 1.1.自动发现测试模块和测试用例: unitest 需要添加用例,(泰斯特楼贷)加载器加载测试用例 pytest 只需要一条代码就可以 ...

  4. try easy pytest 1tep介绍

    『 tep is a testing tool to help you write pytest more easily. Try Easy Pytest! 』 tep前身 tep的前身是接口自动化测 ...

  5. 一、pytest的介绍和安装

    需要针对一个项目系统开发一套UI自动化测试脚本,自己结合着学习,采用了pytest去实现,这里留下记录. 什么是pytest pytest 是一个非常成熟的全功能的Python测试框架 可以胜任uni ...

  6. Pytest安装介绍--使用(html报告)

    Pytes是 一个单元测试框架,可以生成html报告. #卸载# pip uninstall pytest#安装# pip install -U pytest# 查看# pytest --versio ...

  7. pytest单侧模块_入门汇总

    Pytest简单介绍 (pytest是python的一个测试框架,主要是用来进行一些小的测试) 安装:pip install -U pytest 查看是否安装成功:pytest --version 运 ...

  8. [Python]使用pytest进行单元测试

    安装pytest pipenv install pytest 验证安装的版本: pytest --version This , imported /site-packages/pytest.py 接下 ...

  9. 『德不孤』Pytest框架 — 3、Pytest的基础说明

    目录 1.Pytest参数介绍 2.Pytest框架用例命名规则 3.Pytest Exit Code说明 4.pytest.ini全局配置文件 5.Pytest执行测试用例的顺序 1.Pytest参 ...

随机推荐

  1. Leetcode96.Unique Binary Search Trees不同的二叉搜索树

    给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...

  2. Leetcode622.Design Circular Queue设计循环队列

    设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列的一个好处是 ...

  3. R语言可视化--颜色

    RColorBrewer包 三类调色板:sequential / diverging / qualitative 调色板的信息可以与colorRamp / colorRampPalette结合使用 从 ...

  4. bzoj 1024 [SCOI2009]生日快乐——模拟

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1024 可以枚举这边放多少块.那边放多少块. 注意精度.不要每次用x*y/base算有多少块, ...

  5. 2017年2月27日Unicorn, US (148) and China (69), followed by the U.K. (10), India (9), Israel (5) and Germany (5).

    Revisiting The Unicorn Club Get to know the newest crowd of billion dollar startups In 2013, when Ai ...

  6. 大数据心法来了!一站式玩转MaxCompute,还有开发者资源等你领!

    阿里云大数据计算平台开发者版2019年3月推出,MaxCompute正在成为开发者的免费大数据平台.今天,MaxCompute在企业构建自己的数据处理平台实践中起到了至关重要的作用,我们特别精选了企业 ...

  7. layer弹出图片的问题

    转载:https://blog.csdn.net/qq_41815146/article/details/81141088 layer下载地址:http://layer.layui.com/ jQue ...

  8. Directx11教程(46) alpha blend(3)

    原文:Directx11教程(46) alpha blend(3)       现在我们尝试改变box的贴图,使用一张带alpha的dds文件wirefence.dds, 用directx textu ...

  9. [React Native] 解析JSON文件

    在编写代码时,开发者有时需要存储一些比较多,在应用程序运行时不需要更改的数据.文件大不便于写在代码中,可以把这些数据存储到JSON文件中. 优点非常明显: 1. 数据存放在单独的文件中,代码精简有条理 ...

  10. SSH applicationContext.xml import异常

    近期在项目上,遇到了一个问题.在配置applicationContext.xml使用<import>标签引入其他的xml文件时,导致项目启动时过慢.有时还会引起启动异常.后来查到是xml文 ...