Repository navigation
Expand file tree
/
Copy pathtest_ipynb_test.py
More file actions
51 lines (46 loc) · 1.75 KB
/
Copy pathtest_ipynb_test.py
File metadata and controls
51 lines (46 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import nbformat
import pytest
from htmlbook import ipynb_test as runner
def test_registered_notebook_forwards_grading_option(tmp_path, monkeypatch):
calls = []
monkeypatch.setattr(
runner, "_run_notebook", lambda path, **kwargs: calls.append((path, kwargs))
)
namespace = {
"__file__": str(tmp_path / "test_notebooks.py"),
"ipynb_test": runner.ipynb_test,
}
exec('ipynb_test("solution.ipynb", grader_throws=True)', namespace)
namespace["test_solution"]()
assert calls == [(tmp_path / "solution.ipynb", {"grader_throws": True})]
@pytest.mark.parametrize(
"grader_throws,score,should_fail",
[(True, 0, True), (True, 1, False), (False, 0, False)],
)
def test_notebook_grading(tmp_path, monkeypatch, grader_throws, score, should_fail):
monkeypatch.setattr(runner, "_startup_prelude", lambda: "")
project_module = runner.get_project_name().replace("-", "_")
result = {
"score": score,
"tests": [{"name": "example", "score": score, "max_score": 1}],
}
notebook = nbformat.v4.new_notebook(
cells=[
nbformat.v4.new_code_cell(
"from pathlib import Path\n"
f"from {project_module}.exercises.grader import Grader\n"
f"Path('results.json').write_text({json.dumps(result)!r})\n"
"Grader.print_test_results('results.json')\n"
)
]
)
path = tmp_path / "grading.ipynb"
nbformat.write(notebook, path)
if should_fail:
with pytest.raises(
pytest.fail.Exception, match="Grader did not award full points"
):
runner._run_notebook(path, grader_throws=grader_throws)
else:
runner._run_notebook(path, grader_throws=grader_throws)