forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_import.py
More file actions
65 lines (47 loc) · 1.87 KB
/
test_import.py
File metadata and controls
65 lines (47 loc) · 1.87 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import ast
from pythonwhat.Test import Test, DefinedCollTest, EqualTest
from pythonwhat.State import State
from pythonwhat.Reporter import Reporter
def test_import(name,
same_as=True,
not_imported_msg=None,
incorrect_as_msg=None,
state=None):
"""Test import.
Test whether an import statement is used the same in the student's environment as in the solution
environment.
Args:
name (str): the name of the package that has to be checked.
same_as (bool): if false, the alias of the package doesn't have to be the same. Defaults to True.
not_imported_msg (str): feedback message when the package is not imported.
incorrect_as_msg (str): feedback message if the alias is wrong.
:Example:
Student code::
import numpy as np
import pandas as pa
Solution code::
import numpy as np
import pandas as pd
SCT::
test_import("numpy") # pass
test_import("pandas") # fail
test_import("pandas", same_as = False) # pass
"""
rep = Reporter.active_reporter
student_imports = state.student_imports
solution_imports = state.solution_imports
if name not in solution_imports:
raise NameError("%r not in solution imports" % name)
if not_imported_msg is None:
not_imported_msg = "Did you import `%s`?" % name
_msg = state.build_message(not_imported_msg)
rep.do_test(DefinedCollTest(name, student_imports, _msg))
if (same_as):
if incorrect_as_msg is None:
incorrect_as_msg = "Did you set the correct alias for `%s`?" % name
_msg = state.build_message(incorrect_as_msg)
rep.do_test(
EqualTest(
solution_imports[name],
student_imports[name],
_msg))