unittest、nosetest、pytest

参考:Choosing The Perfect Python Testing Framework: Unittest Vs. Pytest Vs.

UnitTest vs Nose2 vs Pytest

Feature Unittest Pytest Nose2
Test Discovery Yes Yes Yes
Fixture Support Yes Yes Yes
Parameterization No Yes Yes
Plugin Ecosystem Limited Extensive Limited
Test Naming Convention Class/Method Function Class/Method
Assertion Methods Yes Yes Yes
Configuration Manual Automatic Automatic

Unittest: The Built-in Testing Framework

Unittest is a built-in testing framework in Python, inspired by the testing frameworks available in other programming languages like Java.

It follows a traditional xUnit-style approach, where tests are organized into classes and methods. Unittest provides a rich set of assertion methods to validate the expected behavior of code.

Key Features of Unittest
  • Supports test automation and discovery
  • Provides a rich set of assertion methods for accurate test validation
  • Enables test case customization through subclassing
  • Supports fixtures for test setup and teardown
  • Generates test reports and statistics
  • Integrates with other testing tools and libraries

Pytest: The Powerful and Flexible Testing Framework

Now let's talk about Pytest, another popular testing framework that developers adore for its simplicity. Pytest takes a more flexible and intuitive approach compared to Unittest. Its main goal is to make testing easy and straightforward.

Unlike Unittest, you don't have to write test classes with Pytest. Instead, you can write simple test functions, just like jotting down your tests on sticky notes.

Pytest also embraces the principle of "convention over configuration" and offers powerful features such as fixtures, parameterization, and advanced test selection.

By leveraging Pytest, developers can focus on writing tests without being overwhelmed by complex setup and configurations. It's all about making testing a breeze!

Key Features of Pytest
  • Supports test discovery and execution without the need for boilerplate code
  • Offers a wide range of powerful plugins for extended functionality
  • Simplifies test writing with intuitive and expressive syntax
  • Provides fixtures for test setup and teardown
  • Supports parallel test execution
  • Generates detailed test reports and code coverage analysis
pytest具有很多第三方插件

并且可以自定义扩展:如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)、pytest--ordering(控制测试运行的顺序);

测试用例的skip和xfail处理

运行后生成测试报告(htmlReport)

安装pytest-html:

复制代码
pip install -U pytest-html

如何使用:

复制代码
py.test test_pyexample.py --html=report.html
更详细的测试报告

安装 pytest-cov:

复制代码
pip install pytest-cov 

如何使用

复制代码
py.test --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,则认为失败
多进程运行

安装pytest-xdist:

复制代码
pip install -U pytest-xdist

如何使用:

复制代码
py.test test_pyexample.py -n NUM

其中NUM填写并发的进程数。

1个case扩展参数
复制代码
import pytest

@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+5",7),("7*5",30)])
def test_eval(self,test_input, expected):
    # eval 将字符串str当成有效的表达式来求值,并返回结果
    assert eval(test_input) == expected

Nose2: The Simplified and Extensible Testing Framework

参考:Note to Users --- nose 1.3.7 documentation

Last but not least, let's talk about Nose2. Nose2 is a testing framework that builds upon Unittest's foundation. It aims to enhance the test discovery and execution process by providing a more user-friendly interface and additional functionalities.

Nose2 can automatically discover and run your tests, generate detailed reports, and handle test fixtures and plugins efficiently.

Key Features of Nose2
  • Test Discovery: Automatically finds and runs test cases across modules and directories.
  • Test Execution: Runs tests selectively at the case, method, or class level.
  • Test Fixtures: Supports setup and teardown functions/methods for test environment management.
  • Test Runner: Generates detailed reports in various formats (console, XML, HTML).
  • Plugin System: Extensible with plugins for coverage analysis, test isolation, etc.
  • Parallel Execution: Runs tests concurrently for faster execution.
  • Test Configuration: Customizable options for test directories, exclusions, and discovery behavior.
nose执行相关命令
  1. nosetests -h 查看所有nose命令与说明

  2. nosetests 查看是否安装nose成功

  3. nosetests -with-xunit输出xml结果报告

  4. nosetests -v 查看运行信息和调试信息,例如会给出当前正在运行哪个测试。

  5. nosetests -w 目录:指定一个目录运行测试,目录可以是相对路径或绝对路径。

  6. nosetests -f 执行测试

  7. nosttests -p 查看可用plugins信息

  8. nosetests --tests=NAMES 执行这些测试

  9. nosetests -s,不捕获输出,会让你的程序里面的一些命令行上的输出显示出来。例如print所输出的内容。

  10. nosetests -a: -a myTagTestClass

    from nose.plugins.attrib import attr
    @attr(myTag='main')
    def test_func2(self):
    print ("this is test_func2")

1个case扩展参数
复制代码
from nose.plugins.attrib import attr
from parameterized import parameterized, parameterized_class

    @parameterized.expand([
        (a)
        (b)
        (c)
    ])
 @attr(myTag='main')
    def test_func2(self, param):
        print ("this is test_func2 " + param)
相关推荐
旦莫1 天前
AI驱动的纯视觉自动化测试:知识库里应该积累什么知识内容
人工智能·python·测试开发·pytest·ai测试
夏至春来-美美2 天前
python 使用pytest的ini配置
开发语言·python·pytest
Be reborn2 天前
CSV + YAML 怎么描述测试:H5 SDK 自动化框架的数据模型设计
运维·自动化·pytest
Be reborn3 天前
用 Playwright 做自动化测试:如何验证网络请求并做断言
网络·python·自动化·pytest
lifewange4 天前
pytest 找不到文件?直接在 pytest.ini 配置根目录 + 路径(最简单方案)
开发语言·python·pytest
老神在在0015 天前
保姆级教程:Pytest + Allure 接口自动化测试从 0 到 1 完整指南(含环境搭建、用例编写、报告生成、报错排坑全流程)
pycharm·pytest·测试
清水白石0085 天前
从手写初始化到 pytest fixture:让 Python 测试既干净、可复用,又能驾驭异步并发
开发语言·python·pytest
测试员周周5 天前
【AI测试数据及模型质量2】换一批测试数据,模型得分差20%——AI评测翻车的根子,90%在数据质量
人工智能·python·ui·单元测试·测试用例·集成测试·pytest
nbwenren13 天前
2026实测:Gemini 3.1 Pro 从需求文档到 pytest 测试用例一条龙教程
测试用例·pytest
我的xiaodoujiao13 天前
API 接口自动化测试详细图文教程学习系列16--项目实战演练3
python·学习·测试工具·pytest