首页 > 其他分享 >CS50P: 5. Unit Tests

CS50P: 5. Unit Tests

时间:2024-07-16 19:19:08浏览次数:5  
标签:__ Tests square except assert def CS50P test Unit

assert

Python: assert.

calculator.py :

def main():
    x = int(input("What's x? "))
    print("x squared is", square(x))
def square(n):
    return n + n	#刻意为之
if __name__ == "__main__":
    main()

test_calculator.py :

from calculator import square
def main():
    test_square()
def test_square():
    assert square(2) == 4
    assert square(3) == 9
if __name__ == "__main__":
    main()

在终端运行,得到 AssertionError ,同样可以用 try & except 语句

def test_square():
    try:
        assert square(2) == 4
    except AssertionError:
        print("2 squared is not 4")
    try:
        assert square(3) == 9
    except AssertionError:
        print("3 squared is not 9")
    try:
        assert square(-2) == 4
    except AssertionError:
        print("-2 squared is not 4")
    try:
        assert square(-3) == 9
    except AssertionError:
        print("-3 squared is not 9")
    try:
        assert square(0) == 0
    except AssertionError:
        print("0 squared is not 0")

pytest

Pytest’s documentation of: pytest

可以测试函数的第三方库,相当于帮我们做了 try & except

示例

test_calculator.py :

from calculator import square
def test_square():
    assert square(2) == 4
    assert square(3) == 9
    assert square(-2) == 4
    assert square(-3) == 9
    assert square(0) == 0

注意到没有 main 函数了

运行 pytest test_calculator.py

categories test

上例中遇到错误即停止,但我们往往希望一次获得更多的错误信息,以便debug,需要分割测试数据

代码:

def test_positive():
    assert square(2) == 4
    assert square(3) == 9

def test_negative():
    assert square(-2) == 4
    assert square(-3) == 9
    
def test_zero():
    assert square(0) == 0

运行结果:(F == Fail;. == Pass)

某一函数测试出错,之后的函数照旧进行测试。

特例——用户输入字符串

应用 pytest 库中的 raises 函数

import pytest
def test_str():
    with pytest.raises(TypeError):
        square("cat")

括号内是可能发生的错误类型,什么时候期望错误被提出?当输入字符时会

tests文件夹

当需要对多个函数进行测试时,往往将测试文件整理为一个文件夹

Command window :

mkdir test
code test/__init__.py

空的 __init__.py 指示出 test 文件夹不单单是file或module,是一个package,里面有可以进行测试的文件

测试时:

pytest test

标签:__,Tests,square,except,assert,def,CS50P,test,Unit
From: https://www.cnblogs.com/chasetsai/p/18305732

相关文章

  • (02)Unity使用在线AI大模型(调用Python)
    目录一、概要二、改造Python代码三、制作Unity场景一、概要    查看本文需完成(01)Unity使用在线AI大模型(使用百度千帆服务)的阅读和实操,本文档接入指南的基础上使用Unity+C#调用百度千帆大模型,需要阅读者有一定的Unity开发基础。此功能本质上就是拿Python大模......
  • VINS-Fusion源码逐行解析:updateLatestStates()函数与slideWindow()
    初始化并优化位姿后,接下来做的事是将这些位姿更新给上一帧,我们来看下updateLatestStates()源码:voidEstimator::updateLatestStates(){//锁定mPropagate,确保对最新状态的更新是线程安全的mPropagate.lock();//更新最新的时间戳,等于当前帧的时间戳加上时间延......
  • 【Unity】凸包算法对比及实现
    背景在做闵可夫斯基差的可视化的时候,获得了很多个点,想要知道其是否包含原点,需要连接一个包裹这些点的最小凸多边形。因此就单独研究了这个部分,实现了功能并进行分析对比。凸包算法可以在多个散落的点中找到最小能包裹它的外壳,像套上一个橡皮筋一样。这里主要采用Graham算法进行代......
  • Day1_1--通过jdbc驱动程序连接mysql数据库+测试(hamcrest+junit)
    idea项目导入mysql对应版本jar包驱动File->ProjectStructure->Libraries点击加号添加驱动并Apply参考代码importorg.junit.Test;importjava.sql.*;/***@authornanzhi*@date2024/7/159:52*/publicclassk1_jdbc{publicstaticvoidmain(String[]ar......
  • CS50P: 4. Libraries
    libraries,moduleslibrariesarebitsofcodewrittenbyyouorotherswecanuseinourprogramPythonallowsustosharefunctionsorfeatureswithothersas"modules"randompython:randomrandomisalibrarythatcomeswithPythonthatweco......
  • Unity 基础知识点
    废话不多说,上教程。......
  • 【Unity】自制PolygonCollider2D
    防止和UnityEngine的PolygonCollider2D重名,所有类包裹在了我自己定义的名称空间JimDevPack中,名称空间的声明部分在文章代码中略去了。定义PolygonCollider2D和基类基类publicclassCollider2D:MonoBehaviour{}PolygonCollider2DpublicclassPolygonCollider2D:Coll......
  • [Unity] Dreamteck Splines实现沿路径移动功能
    DreamteckSplines实现沿路径移动功能最近有一个“让物体沿固定路径移动”的需求,因此接触到了DreamteckSplines插件。DreamteckSplines可以很方便地绘制各种插值曲线,但在实现物体移动的时候却遇到了很多坑,因此在这里记录一下。1.绘制路径线首先,让我们在场景上创建一个空物......
  • python接口自动化(二十五)--unittest断言——下(详解)
    1.简介 本篇还是回归到我们最初始的话题,想必大家都忘记了,没关系看这里:传送门 没错最初的话题就是登录,由于博客园的登录机制改变了,本篇以我找到的开源免费的登录API为案例,结合unittest框架写2个用例。同样我们先来看一下接口文档。2.接口文档2.1登录接口请求方式......
  • unity is running as administrator 管理员权限问题
    每次打开工程弹出unityisrunningasadministrator的窗口unity版本2022.3.34f1,电脑系统是win11系统解决方法一:解决方法二:unity版本2022.3.34f1,电脑系统是win11系统每次打开工程都会出现unityisrunningasadministrator的窗口,真的很烦人。本人使用第二种方法......