参考:
- https://github.com/minitorch/slides
(使用的时候,下载下来,把index.html
文件拖到浏览器,然后方向键下键,ppt以网页形式播放)
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