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)
相关推荐
Warren982 天前
Pytest Fixture 作用域详解:Function、Class、Module、Session 怎么选
面试·职场和发展·单元测试·pytest·pip·模块测试·jira
我的xiaodoujiao3 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
我的xiaodoujiao3 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
月明长歌3 天前
全栈测试修炼指南:从接口策略到 Python+Pytest+Allure 企业级架构
python·架构·pytest
一晌小贪欢3 天前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
我送炭你添花4 天前
Pelco KBD300A 模拟器:20.搭建pytest集成测试基础框架 + 模拟器闭环测试
python·集成测试·pytest
我送炭你添花5 天前
Pelco KBD300A 模拟器:18. 按依赖顺序 + 复杂度由低到高逐步推进pytest单元测试
python·单元测试·log4j·pytest
生活很暖很治愈5 天前
Pytest-order插件
python·测试工具·测试用例·pytest
测试人社区—66797 天前
2025区块链分层防御指南:AI驱动的安全测试实战策略
开发语言·驱动开发·python·appium·pytest
我送炭你添花8 天前
pytest 入门指南:从零开始掌握 Python 测试框架的核心概念与使用方法
chrome·python·pytest