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)
相关推荐
qq_4337169517 小时前
测试分层:减少对全链路回归依赖的探索!
自动化测试·软件测试·功能测试·测试工具·回归·pytest·postman
开心呆哥2 天前
【Android Wi-Fi 操作命令指南】
android·python·pytest
小码哥说测试3 天前
测试分层:减少对全链路回归依赖的探索!
自动化测试·软件测试·人工智能·测试工具·appium·pytest·postman
帅得不敢出门5 天前
Python+Appium+Pytest+Allure自动化测试框架-安装篇
python·appium·自动化·pytest·测试·allure
blues_C5 天前
Pytest-Bdd-Playwright 系列教程(5):仅执行测试用例的收集阶段
自动化测试·测试用例·pytest·bdd
帅得不敢出门7 天前
Python+Appium+Pytest+Allure自动化测试框架-代码篇
python·appium·自动化·pytest·测试·allure
qq_433716958 天前
接口测试 —— Postman 变量了解一下!
自动化测试·软件测试·jmeter·单元测试·pytest·接口测试·压力测试
彳亍2618 天前
【Python单元测试】pytest框架单元测试 配置 命令行操作 测试报告 覆盖率
python·单元测试·pytest
鹿鸣悠悠8 天前
pytest脚本常用的执行命令
pytest