pytest学习总结

1、概述

pytest是一个功能齐全的单元测试框架,主要具有以下特点:

  • 1、丰富的资料文档,通过阅读示例文档可以快速上手
  • 2、用例可读性更强,框架结构清晰,可轻松实现数据、用例分离
  • 3、能够支持简单功能逻辑单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);
  • 4、插件丰富,并支持自定义扩展,如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;
  • 5、测试套件支持多种模式(‘function’,‘module’,‘class’,‘session’),比unittest更强;可以直接进行参数化操作;
  • 6、可以生成junit格式报告,可以很好与CI工具结合,如jenkins等。

2、使用介绍

2.1、安装

直接pip安装即可

pip install -U pytest

检查安装版本:

pytest --version

2.2、简单示例

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

  • 测试文件以test_开头或以_test结尾
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试函数以test_开头
  • 断言使用基本的assert即可
    test_scope.py
#!/usr/bin/env python
# encoding: utf-8
"""
@author: wanwei
@license: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited.
@contact: 
@software: pycharm
@file: test_scope.py
@time: 2019/9/29 15:29
@desc:
"""
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.test
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...")

fixture的scope参数

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

  • function:函数的初始化,每个test中的函数都运行,scope默认为function
  • class:类的初始化,每个class的所有test只运行一次
  • module:模块的初始化,即每个python文件模块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.test

通过 -m “recommend” 执行有recommend标记的test方法
pytest学习总结_第1张图片
通过 -m “not recommend” 执行没有recommend标记的test方法
pytest学习总结_第2张图片

命令行参数介绍

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

1、pytest -v test_scope.pypytest学习总结_第3张图片

2、pytest -s test_scope.py

pytest学习总结_第4张图片

3. 扩展插件

3.1 测试顺序随机

pip install pytest-randomly

3.2 分布式测试

pip install pytest-xdist

3.3 出错立即返回

pip install pytest-instafail

四、参考文档:

1、官方文档
http://doc.pytest.org/en/latest/getting-started.html
2、python的测试工具大全
https://wiki.python.org/moin/PythonTestingToolsTaxonomy
3、python主流的测试工具横向比较
http://docs.python-guide.org/en/latest/writing/tests/
http://pythontesting.net/test-podcast/
4、python单元测试框架pytest简介
https://blog.csdn.net/liuchunming033/article/details/46501653

你可能感兴趣的:(pytest)