PyTest简明教程

参考:

1. 代码示例

minitorch->operators.py

import math

# ## Task 0.1

# Implementation of a prelude of elementary functions.


def mul(x, y):
    ":math:`f(x, y) = x * y`"
    # TODO: Implement for Task 0.1.
    return x * y


def id(x):
    ":math:`f(x) = x`"
    # TODO: Implement for Task 0.1.
    return x


def add(x, y):
    ":math:`f(x, y) = x + y`"
    # TODO: Implement for Task 0.1.
    return x + y

tests->test_operators.py

from minitorch.operators import (
    mul,
    add,
from hypothesis import given
from hypothesis.strategies import lists
from .strategies import small_floats, assert_close
import pytest
from minitorch import MathTest

@pytest.mark.task0_1
@given(small_floats, small_floats)
def test_same_as_python(x, y):
    "Check that the main operators all return the same value of the python version"
    assert_close(mul(x, y), x * y)
    assert_close(add(x, y), x + y)

使用的时候,如果要测试任务1,可以

pytest -m task0_1

如果要测整个脚本文件,可以

pytest tests/test_operators.py

2. 其他(minitorch)

minitorch/slides-master/docs/slides+build2/module0.1.html#/16
在这里插入图片描述
minitorch/slides-master/docs/slides+build2/module0.1.html#/17
在这里插入图片描述
minitorch/slides-master/docs/slides+build2/module0.1.html#/19
在这里插入图片描述
minitorch/slides-master/docs/slides+build2/module0.1.html#/20
在这里插入图片描述
minitorch/slides-master/docs/slides+build2/module0.1.html#/22
在这里插入图片描述
标准的单元测试,其实使用最多的就是assert语句。

minitorch/slides-master/docs/slides+build2/module0.1.html#/23
在这里插入图片描述
使用assert进行属性测试

minitorch/slides-master/docs/slides+build2/module0.1.html#/24
在这里插入图片描述

minitorch/slides-master/docs/slides+build2/module0.1.html#/25
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吨吨不打野

解决了问题,觉得还行就给点

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

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

打赏作者

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

抵扣说明:

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

余额充值