unittest、nosetest、pytest

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

UnitTest vs Nose2 vs Pytest

FeatureUnittest   Pytest Nose2
Test DiscoveryYesYesYes
Fixture SupportYesYesYes
ParameterizationNoYesYes
Plugin EcosystemLimitedExtensiveLimited
Test Naming ConventionClass/MethodFunctionClass/Method
Assertion MethodsYesYesYes
ConfigurationManual AutomaticAutomatic

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 --processes=NUM --process-timeout=SECONDS:启动多个进程并发执行case,processes一般设置为cpu核数

  11. nosetests -a: -a myTag TestClass

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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/667214.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Springboot简单设计两级缓存

两级缓存相比单纯使用远程缓存,具有什么优势呢? 本地缓存基于本地环境的内存,访问速度非常快,对于一些变更频率低、实时性要求低的数据,可以放在本地缓存中,提升访问速度 使用本地缓存能够减少和Redis类的远…

netlink学习

netlink是什么 netlink是Linux内核中的一种进程间通信(IPC)机制。它允许内核空间与用户空间之间,以及用户空间进程之间进行双向通信。 内核里的很多子系统使用netlink通信,包括网络管理(Routing,Netfilt…

项目安全问题及解决方法-----xss处理

XSS 问题的根源在于,原本是让用户传入或输入正常数据的地方,被黑客替换为了 JavaScript 脚本,页面没有经过转义直接显示了这个数据,然后脚本就被 执行了。更严重的是,脚本没有经过转义就保存到了数据库中,随…

【数据结构】实现顺序表

大家好,我是苏貝,本篇博客带大家了解顺序表,如果你觉得我写的还不错的话,可以给我一个赞👍吗,感谢❤️ 目录 一.概念及结构二.接口实现2.1 创建顺序表结构体2.2 初始化顺序表2.3 销毁顺序表2.4 打印顺序表…

Unity 设置鼠标

目录 前言 图标样式的设置 代码控制 编辑器直接修改 图标的显隐 CursorLockMode Cursor.visible 前言 本章主要对鼠标图标样式还有鼠标显隐进行设置 图标样式的设置 代码控制 有时候需要有改变鼠标样式的需求可以使用如下代码 Cursor.SetCursor(this.mouseTexture, Vec…

[word] 怎么删除文字底纹 #职场发展#其他

怎么删除文字底纹 怎么删除文字底纹?我们在录入文字到文档的时候,或者是复制网上内容时,都会带有格式,有时候还会遇到删除不掉的问题。今天给大家分享小技巧,解决你的问题。 1、删除文字底纹 文档自带的底纹,删除技…

C++实现智能指针(涉及知识点:重载运算符,内存泄露的风险)

案例 有时候代码很长,很容易就忘了释放P。导致内存泄露,在程序结束后才会释放。内存泄露的风险 如果代码需要的内存很大,前面的代码用完了new申请的内容不去释放,就会被一直占用着,后面可能不够用了造成程序崩溃。解决…

docker下拉(pull)镜像和生成容器,文章尾部有常用的linux命令

目录 1:docker镜像和容器是什么 2:docker初始化个容器,并进入容器安装mariaDb和httpd 1:用远程工具SecureCRT登录docker 2:拉取CentOS镜像并初始化一个容器 a:拉取镜像(这一步可能会有点久&…

linux文件权限备份、恢复-linux文件权限如何备份、恢复-getfacl/setfacl备份恢复文件权限

0、序 在运维这条路上走久了,你能听到或者遇到这样的事情就越多,甚至是你自己干过的: 一个信心满满的运维人员一个不小心,输入 "chmod -R 777 /" 导致一个巨大的悲剧,然后整个部门从上到下被撸一顿。虽然…

牛客周赛 Round 31

D. 思路&#xff1a;使用map构造两个链表。 #include <bits/stdc.h> using namespace std;map<int,int> l,r; int main() {int q;cin>>q;int op-1e9-1;int ed1e91;r[op]ed;l[ed]op;while(q--){int a;cin>>a;if(a1){int x,y;cin>>x>>y;int…

编程笔记 html5cssjs 069 JavaScript Undefined数据类型

编程笔记 html5&css&js 069 JavaScript Undefined数据类型 一、undefined数据类型二、类型运算小结 在JavaScript中&#xff0c;undefined 是一种基本数据类型&#xff0c;它表示一个变量已经声明但未定义&#xff08;即没有赋值&#xff09;或者一个对象属性不存在。 …

MIT6.5830 实验3

前置回顾 在实验2中&#xff0c;完成了增删查改、排序、分组、聚合、连接等基本操作&#xff0c;在已提供 sql 解析器的基础上&#xff0c;能够运行进本的 sql 语句。都是逻辑层的实现&#xff0c;没有涉及物理存储方面的内容。 实验目标 实现最简单的基于锁的transaction&am…

MicroPython ESP32开发:通过寄存器直接访问外围设备

可以通过直接读写寄存器来控制 ESP32 的外设。这就需要阅读数据手册&#xff0c;了解要使用哪些寄存器以及要写入哪些值。下面的示例展示了如何打开和更改 MCPWM0 外设的预分频器。 from micropython import const from machine import mem32# Define the register addresses …

面向对象OOP

一、面向对象的定义 基于"类"和"对象"的"组件化"编程思想 二、面向对象的核心思想 封装 继承 多态 (1)封装 : 隐藏代码实现细节&#xff0c;提高简洁性 (2)继承 &#xff1a;代码的复用&#xff0c;通过定义父类&#xff0c;子类在父类基础上…

【STM32+HAL库+CubeMX】UART轮询收发、中断收发、DMA收发方法及空闲中断详解

&#xff08;转载&#xff09;原文链接&#xff1a;https://blog.csdn.net/qq_39344192/article/details/131470735 1. 什么是UART&#xff1f; UART是一种异步串行通信接口&#xff0c;常用于通过串口与外部设备进行通信。它通过发送和接收数据帧来实现数据传输&#xff0c;使…

Android开发中,Vue 3处理回退按键事件

vue3有一些变化&#xff0c;按照网上有些文章的方法&#xff0c;发现行不通&#xff0c;通过一段时间的打印、尝试后&#xff0c;发现以下方法可行。 第一步&#xff09;首先定义一个处理回退事件的js函数&#xff0c;一定是vue.methods中的函数&#xff0c;否则找不到this&am…

postman 文档、导出json脚本 导出响应数据 response ,showdoc导入postman json脚本 导出为文档word或markdown

保存、补全尽可能多的数据、描述 保存响应数据 Response&#xff1a;&#xff08;如果导出接口数据&#xff0c;会同步导出响应数据&#xff09; 请求接口后&#xff0c;点击下方 Save as Example 可以保存响应数据到本地&#xff08;会在左侧接口下新增一个e.g. 文件用来保…

使用_NT_SYMBOL_PATH在启动VS前设置PDB路径

一、背景 由于公司相关项目的开发管理方式&#xff0c;导致公司会存在多个分支的版本正在开发/测试中。 在这样的背景下&#xff0c;我的日常工作中有时会出现存在某个分支的项目软件的某个功能出现了问题需要我去排查解决&#xff0c;而我当前并不在该分支上开发。于是只能安装…

C++泛编程(3)

类模板基础 1.类模板的基本概念2.类模板的分文件编写3.类模板的嵌套 &#xff08;未完待续...&#xff09; 在往节内容中&#xff0c;我们详细介绍了函数模板&#xff0c;这节开始我们就来聊一聊类模板。C中&#xff0c;类的细节远比函数多&#xff0c;所以这个专题也会更复杂。…

Llama2大模型开源,大模型的Android时代来了?

就昨天凌晨,微软和Meta宣布Llama2大模型开源且进一步放开商用,一下朋友圈刷屏。要知道,开源界最强大的模型就是过去Meta开源的Llama,而现在Llama2更强大,又开放商用,更有微软大模型霸主企业撑腰(微软既投资大模型界的IOS——ChatGPT,又联合发布大模型的Android——Llam…