python测试模块

1.安装pytest或ipytest(用于jupyter notebook)

pip install pytest

2.pytest是Python中非常流行的测试框架之一,它使得编写和运行测试变得非常简单。
3.简单DEMO(ipytest)

导入ipytest库,ipytest.autoconfig() 是 ipytest 模块的一个函数,用于自动配置 ipytest 以在 Jupyter Notebook 中运行。

import ipytest
ipytest.autoconfig()

首先编写了一个过滤参数列表中能被5整除的函数

def get_divisible_by_five(numbers):
  """Returns a list of numbers which are divisible by five in the list got as an argument"""
  result = []
  for num in numbers:
      if not num % 5:
          result.append(num)

  return result

编写一个测试用例测试get_divisible_by_five函数能否正常工作

%%ipytest
#%%ipytest是一个Jupyter Notebook的魔法命令,它表明这个cell中的代码应该由ipytest模块来运行。

def test_get_divisible_by_five():
    assert get_divisible_by_five([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) == [5, 10]

如果测试成功
在这里插入图片描述> 如果测试失败
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小企鹅upup

有钱的捧个钱场,没钱的点的赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值