# nbgrader_unittest
Drop-in replacement for the unittest module to use them in tests for
[nbgrader][nbgrader] assignments.
You can use this module just like the [unittest][unittest] module. Just don't
call the `main` function to run the tests. Use the `run_nbgrader_test`
function instead.
If your tests all pass, no output will be generated and nbgrader will assign
full marks to this cell. If one of your tests fail, the usual unittest output
will be created and printed to `sys.stderr` and no marks will be assigned to
this cell in nbgrader.
## Example
You prepare a new jupyter notebook assignment with parts, that should be
autograded with [nbgrader][nbgrader].Your assignment could be something like
this:
> Write a function that returns a list of numbers, such that $x_i=i^2$, for
> $1 \leq i \leq n$. Make sure it handles the case where $n<1$ by raising a
> `ValueError`.
Your code cell with the "Autograded answer" will look like this:
```python
def squares(n):
"""Compute the squares of numbers from 1 to n, such that the
ith element of the returned list equals i^2.
"""
### BEGIN SOLUTION
if n < 1:
raise ValueError("n must be greater than or equal to 1")
return [x**2 for x in range(1, n + 1)]
### END SOLUTION
```
In a new code cell you put your "Autograder test":
```python
import nbgrader_unittest
class SquaresTests(nbgrader_unittest.TestCase):
def test_square_of_one(self):
self.assertEqual(squares(1), [1])
def test_squares_up_to_ten(self):
self.assertEqual(
squares(10),
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
)
def test_negative_argument(self):
with self.assertRaises(ValueError):
squares(-4)
nbgrader_unittest.run_nbgrader_test(SquaresTests)
```
[nbgrader]: https://nbgrader.readthedocs.io/en/stable/
[unittest]: https://docs.python.org/3/library/unittest.html
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共11个文件
txt:3个
py:2个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:nbgrader_unittest_sebastian_stigler-0.0.3.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
nbgrader_unittest_sebastian_stigler-0.0.3.tar.gz (11个子文件)
nbgrader_unittest_sebastian_stigler-0.0.3
PKG-INFO 2KB
nbgrader_unittest_sebastian_stigler.egg-info
PKG-INFO 2KB
SOURCES.txt 334B
top_level.txt 18B
dependency_links.txt 1B
pyproject.toml 89B
LICENSE 1KB
setup.cfg 647B
nbgrader_unittest
mock.py 27B
__init__.py 1KB
README.md 2KB
共 11 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功