One important thing to remember about python unit test cases is that the file structure you use is absolute, and student submissions will have to match it exactly.
Due to the complexity of relative python imports, we do not dynamically determine dhow to match up all of the imports, but instead leave it up to you to make sure the files are in the right place. This allows us to support very complex Python applications as well as simple helper methods files like the one below.
Unless your project contains multiple modules, you probably want to keep your python files in your perfect code (and therefore student submissions as well) in the direct root of the zip folder, not in any folders beneath that.
Creating unit tests for Python is a bit more difficult, since there is less focus on Objects compared to Java. Here is some sample code we used as a perfect code file:
rot13 = string.maketrans(
return string.translate(str, rot13)
caesarstr = ""
for ch in str:
tmp = ord(ch) + shift
if tmp > ord('z'):
tmp -= 26
fin = chr(tmp)
caesarstr += fin
if num is 1:
return num * factorial(num-1)
def between(n, n1, n2):
return n1 < n < n2
As you can see, this contains 5 different methods that really have nothing in common but could be used to make other tasks easier. We have 2 ciphers, 2 math functions, and a string reverse-er.
Now we can use this and make Python unit tests using these functions. Remember that a project with Python unit tests cannot contain any files with main functions or code running without being called as a function or your output will be affected.
Make sure that you put your test code inside the text box named 'Input' in the test case view, as this is the actual code that will be run for the unit test.
For unit testing, we use the unittest module, so you can use normal python asserts or self.assert* methods when testing. Make sure to check out the link above for some examples! The most common are as follows:
self.assertEqual(var0, var1) - Equal
self.assertTrue(stmt) - True
self.assertFalse(stmt) - False
self.assertRaises(Exception) - For checking exceptions
Here are some sample unit tests:
#Test rev method
str1 = "hello world"
str2 = rev(str1)
assert rev(str2) == str1
assert rot13("hello world") == "uryyb jbeyq"
#Test Caesar Shift
assert caesar("helloworld", 13) == "uryybjbeyq"
#Test Caesar Shift with wrap
assert caesar("helloworld", 26) == "helloworld"
#Tests recursive factorials
assert factorial(3) == 6
#Tests the between method
#order of args is number, lower, higher
assert between(1, 0, 2)
Mimir Classroom is able to handle imports in Python unit tests. Below is an example:
seed = 1024
random_number = random.randrange(0, 10)
assert random_number == 0
Keep in mind that imports should be done at the top, before any actual code. You can import any of the standard libraries by default, plus anything that we may have added already with pip. If you try to import a pip library and find that it is not installed, contact us using the chat bubble and we can install it for you ASAP!