Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions unit_tests/doubler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def d(fname="lab99_ex1_easy.expected_input"):
with open(fname) as f:
n = int(f.read().split()[0])
return str(n * 2 + n % 2)

if __name__ == "__main__":
print(d())
1 change: 1 addition & 0 deletions unit_tests/lab99_ex1_easy.expected_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
1 change: 1 addition & 0 deletions unit_tests/lab99_ex1_easy.expected_output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24
1 change: 1 addition & 0 deletions unit_tests/lab99_ex1_hard.expected_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4129088901429017650
1 change: 1 addition & 0 deletions unit_tests/lab99_ex1_hard.expected_output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8258177802858035300
1 change: 1 addition & 0 deletions unit_tests/lab99_ex1_odd.expected_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions unit_tests/lab99_ex1_odd.expected_output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
46 changes: 46 additions & 0 deletions unit_tests/lab99_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
This file is basic example of a unit test for the labs.
The TestClass keeps a list of filenames which refer to sample input and
corresponding output files.

The test_ex1 function tries to pass the contents of each lab99_ex1_*.in to the
function in the "doubler.py" file; output should match that in lab99_ex1_.*out.

Use ``python3 lab99_tests.py -v`` to run tests.
"""


# ideas:
# tarball the tests, have students decompress the files
# make sure students know how to run the tests
# have .tex read test cases and insert them into the exercises
# change exercises to require filename as argument to main (use default args)
# and have strict return type requirements
# can also redirect stdout; or offer students the option

# have the TestCase class make sure filenames are valid

import unittest
import doubler

def get_output(fname):
return fname + ".expected_output"

def get_input(fname):
return fname + ".expected_input"

class TestLab99(unittest.TestCase):
ex1_test_fnames = ["lab99_ex1_easy", "lab99_ex1_hard", "lab99_ex1_odd"]

def test_ex1(self):
# run subtests, one for each test input
for test in TestLab99.ex1_test_fnames:
# know which test file caused the code for the exercise to fail
with self.subTest(i=test):
with open(get_output(test)) as f:
# test the function!
self.assertEqual(doubler.d(get_input(test)),
f.read().split()[0])

if __name__ == '__main__':
unittest.main()
20 changes: 20 additions & 0 deletions unit_tests/lab9_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
import luhns

class TestLab9(unittest.TestCase):
tests = [{"i": "38520000023237", "o": True},
{"i": "49927398716", "o": True},
{"i": "49927398717", "o": False},
{"i": "1234567812345670", "o": False},
{"i": "1234567812345678", "o": True}]

def test_ex1(self):
# run subtests, one for each test input
for test in TestLab9.tests:
# know which test file caused the code for the exercise to fail
with self.subTest(i=test):
# test the function!
self.assertEqual(luhns.validate(test["i"]), test["o"])

if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions unit_tests/luhns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def validate(card):
return {"38520000023237": True,
"49927398716": False, #;True,
"49927398717": False,
"1234567812345670": False,
"1234567812345678": True}[card]