python unittest和pytest,选哪个?

文章目录

1.背影

近期项目中,纠结使用unittest 还是pytest, 这篇博文将解决这个问题,
测试过程中, 有两种方式:

  • 自动化测试 (automated testing)
  • 手动测试(manual testing)

我想你会毫不犹豫的选择自动化测试

  1. pytest VS unittest的区别

2.1 pytest (推荐使用)

  • 仅需要编写函数,并断言验证功能即可
  • 更易用使用, 代码简单,紧凑,高效

2.2 unittest

python unittest 是受到了java的 junit 启发开发的,

  • 必须编写类, 在类中编写函数,并断言验证功能
  • python 内置默认的测试框架
  • 必须导入模块,创建一个类, 并在类中定义测试功能

  • 使用方法

3.1 pytest

  • 安装及验证安装是否成功
(sse38)  mac$ pip install -U pytest
(sse38)  mac$ pytest --version
pytest 7.0.1
(sse38)  mac$ pytest -V
pytest 7.0.1
  • 最简单的使用方法

(sse38)  mac$ cat mycode.py
def add(x:int, y:int)->int:
    return x+y

(sse38)  mac$ cat test_mycode.py
from mycode import add

def test_add():
    assert(add(3,5), 8)

(sse38)  mac$ pytest test_mycode.py
========================================================================= test session starts =========================================================================
platform darwin -- Python 3.8.13, pytest-7.0.1, pluggy-1.0.0
rootdir: /Users/mac/projects/
collected 1 item

test_mycode.py .                                                                                                                                                [100%]

========================================================================== 1 passed in 0.01s ==========================================================================

(sse38)  mac$ pytest test/

Original: https://blog.csdn.net/weixin_39791387/article/details/125273334
Author: 诸葛老刘
Title: python unittest和pytest,选哪个?

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/774961/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球