forked from ucbds-infra/otter-grader
/
test_check.py
179 lines (155 loc) · 6.42 KB
/
test_check.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#################################
##### Tests for otter check #####
#################################
import os
import unittest
import subprocess
import contextlib
from textwrap import dedent
from subprocess import PIPE
from glob import glob
from io import StringIO
from unittest import mock
from otter import Notebook
# from otter.argparser import get_parser
from otter.check import main as check
from otter.runner import run_otter
from . import TestCase
# parser = get_parser()
TEST_FILES_PATH = "test/test-check/"
class TestCheck(TestCase):
def test_otter_check_script(self):
"""
Checks that the script checker works
"""
# run for each individual test
for file in glob(TEST_FILES_PATH + "tests/*.py"):
check_command = ["check",
TEST_FILES_PATH + "file0.py",
"-q", os.path.split(file)[1][:-3],
"-t", os.path.split(file)[0]
]
# args = parser.parse_args(check_command)
# args.func = check
# capture stdout
output = StringIO()
with contextlib.redirect_stdout(output):
# mock block_print otherwise they interfere with capture of stdout
with mock.patch("otter.check.block_print"):
# args.func(args)
run_otter(check_command)
if os.path.split(file)[1] != "q2.py":
self.assertEqual(
output.getvalue().strip().split("\n")[-1].strip(),
"All tests passed!",
"Did not pass test at {}".format(file)
)
# run checker command
check_command = ["check",
TEST_FILES_PATH + "file0.py",
"-t", TEST_FILES_PATH + "tests"
]
# args = parser.parse_args(check_command)
# args.func = check
# capture stdout
output = StringIO()
with contextlib.redirect_stdout(output):
# mock block_print otherwise they interfere with capture of stdout
with mock.patch("otter.check.block_print"):
run_otter(check_command)
self.assertEqual(
output.getvalue().strip(),
dedent("""\
[0. 0.02002002 0.04004004 0.06006006 0.08008008]
q1 results: All test cases passed!
q2 results:
q2 - 1 result:
Trying:
1 == 1
Expecting:
False
**********************************************************************
Line 2, in q2 0
Failed example:
1 == 1
Expected:
False
Got:
True
q2 - 2 result:
Test case passed!
q3 results: All test cases passed!
q4 results: All test cases passed!
q5 results: All test cases passed!"""),
"Did not pass correct tests"
)
def test_otter_check_notebook(self):
"""
Checks that the script checker works
"""
# run for each individual test
for file in glob(TEST_FILES_PATH + "tests/*.py"):
check_command = ["check",
TEST_FILES_PATH + "test-nb.ipynb",
"-q", os.path.split(file)[1][:-3],
"-t", os.path.split(file)[0]
]
# args = parser.parse_args(check_command)
# args.func = check
# capture stdout
output = StringIO()
with contextlib.redirect_stdout(output):
# mock block_print otherwise they interfere with capture of stdout
with mock.patch("otter.check.block_print"):
# args.func(args)
run_otter(check_command)
if os.path.split(file)[1] != "q2.py":
self.assertEqual(
output.getvalue().strip().split("\n")[-1].strip(),
"All tests passed!",
"Did not pass test at {}".format(file)
)
# run checker command
check_command = ["check",
TEST_FILES_PATH + "test-nb.ipynb",
"-t", TEST_FILES_PATH + "tests"
]
# args = parser.parse_args(check_command)
# args.func = check
# capture stdout
output = StringIO()
with contextlib.redirect_stdout(output):
# mock block_print otherwise they interfere with capture of stdout
with mock.patch("otter.check.block_print"):
# print(output.getvalue())
run_otter(check_command)
self.assertEqual(
output.getvalue().strip(),
dedent("""\
[0. 0.02002002 0.04004004 0.06006006 0.08008008]
q1 results: All test cases passed!
q2 results:
q2 - 1 result:
Trying:
1 == 1
Expecting:
False
**********************************************************************
Line 2, in q2 0
Failed example:
1 == 1
Expected:
False
Got:
True
q2 - 2 result:
Test case passed!
q3 results: All test cases passed!
q4 results: All test cases passed!
q5 results: All test cases passed!"""),
"Did not pass correct tests"
)
@classmethod
def tearDownClass(cls):
if os.path.exists(".OTTER_LOG"):
os.system("rm .OTTER_LOG")