From e26046afa90a133a6e90be1aecbe471d4d97e36f Mon Sep 17 00:00:00 2001 From: Hucal Date: Fri, 23 Oct 2015 00:20:47 -0600 Subject: [PATCH 1/5] Created sample unit-tests. --- unit_tests/doubler.py | 7 ++++ unit_tests/lab99_ex1_easy.expected_input | 1 + unit_tests/lab99_ex1_easy.expected_output | 1 + unit_tests/lab99_ex1_hard.expected_input | 1 + unit_tests/lab99_ex1_hard.expected_output | 1 + unit_tests/lab99_ex1_odd.expected_input | 1 + unit_tests/lab99_ex1_odd.expected_output | 1 + unit_tests/lab99_tests.py | 46 +++++++++++++++++++++++ 8 files changed, 59 insertions(+) create mode 100644 unit_tests/doubler.py create mode 100644 unit_tests/lab99_ex1_easy.expected_input create mode 100644 unit_tests/lab99_ex1_easy.expected_output create mode 100644 unit_tests/lab99_ex1_hard.expected_input create mode 100644 unit_tests/lab99_ex1_hard.expected_output create mode 100644 unit_tests/lab99_ex1_odd.expected_input create mode 100644 unit_tests/lab99_ex1_odd.expected_output create mode 100644 unit_tests/lab99_tests.py diff --git a/unit_tests/doubler.py b/unit_tests/doubler.py new file mode 100644 index 0000000..4a4faa2 --- /dev/null +++ b/unit_tests/doubler.py @@ -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()) diff --git a/unit_tests/lab99_ex1_easy.expected_input b/unit_tests/lab99_ex1_easy.expected_input new file mode 100644 index 0000000..48082f7 --- /dev/null +++ b/unit_tests/lab99_ex1_easy.expected_input @@ -0,0 +1 @@ +12 diff --git a/unit_tests/lab99_ex1_easy.expected_output b/unit_tests/lab99_ex1_easy.expected_output new file mode 100644 index 0000000..a45fd52 --- /dev/null +++ b/unit_tests/lab99_ex1_easy.expected_output @@ -0,0 +1 @@ +24 diff --git a/unit_tests/lab99_ex1_hard.expected_input b/unit_tests/lab99_ex1_hard.expected_input new file mode 100644 index 0000000..77ac6c0 --- /dev/null +++ b/unit_tests/lab99_ex1_hard.expected_input @@ -0,0 +1 @@ +4129088901429017650 diff --git a/unit_tests/lab99_ex1_hard.expected_output b/unit_tests/lab99_ex1_hard.expected_output new file mode 100644 index 0000000..c430968 --- /dev/null +++ b/unit_tests/lab99_ex1_hard.expected_output @@ -0,0 +1 @@ +8258177802858035300 diff --git a/unit_tests/lab99_ex1_odd.expected_input b/unit_tests/lab99_ex1_odd.expected_input new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/unit_tests/lab99_ex1_odd.expected_input @@ -0,0 +1 @@ +1 diff --git a/unit_tests/lab99_ex1_odd.expected_output b/unit_tests/lab99_ex1_odd.expected_output new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/unit_tests/lab99_ex1_odd.expected_output @@ -0,0 +1 @@ +2 diff --git a/unit_tests/lab99_tests.py b/unit_tests/lab99_tests.py new file mode 100644 index 0000000..4c8e83f --- /dev/null +++ b/unit_tests/lab99_tests.py @@ -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() From ca9d2c367cc7b98bae4c4c8dbf35dbddc5dabdaf Mon Sep 17 00:00:00 2001 From: Hucal Date: Tue, 27 Oct 2015 17:12:15 -0600 Subject: [PATCH 2/5] Test for Luhns algorithm, lab 9 --- unit_tests/lab9_tests.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 unit_tests/lab9_tests.py diff --git a/unit_tests/lab9_tests.py b/unit_tests/lab9_tests.py new file mode 100644 index 0000000..90964dc --- /dev/null +++ b/unit_tests/lab9_tests.py @@ -0,0 +1,16 @@ +import unittest +import luhns + +class TestLab9(unittest.TestCase): + tests = [{i: "38520000023237", o: True}, ] + + def test_ex1(self): + # run subtests, one for each test input + for test in TestLab99.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() From 84e929c9e633299f381ae2fd446ae04a0c8f97df Mon Sep 17 00:00:00 2001 From: Hucal Date: Tue, 27 Oct 2015 17:14:33 -0600 Subject: [PATCH 3/5] More tests --- unit_tests/lab9_tests.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/unit_tests/lab9_tests.py b/unit_tests/lab9_tests.py index 90964dc..75c6167 100644 --- a/unit_tests/lab9_tests.py +++ b/unit_tests/lab9_tests.py @@ -2,7 +2,11 @@ import luhns class TestLab9(unittest.TestCase): - tests = [{i: "38520000023237", o: True}, ] + 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 From 60a4e7fcb3dbf412ab934c73544847540e7b7174 Mon Sep 17 00:00:00 2001 From: Hucal Date: Tue, 27 Oct 2015 17:31:37 -0600 Subject: [PATCH 4/5] Bugs, sample code --- unit_tests/luhns.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 unit_tests/luhns.py diff --git a/unit_tests/luhns.py b/unit_tests/luhns.py new file mode 100644 index 0000000..0b865b4 --- /dev/null +++ b/unit_tests/luhns.py @@ -0,0 +1,6 @@ +def validate(card): + return {"38520000023237": True, + "49927398716": False, #;True, + "49927398717": False, + "1234567812345670": False, + "1234567812345678": True}[card] From 9e5ec39a9052f1528a686cf361e60eb2cf43317c Mon Sep 17 00:00:00 2001 From: Hucal Date: Tue, 27 Oct 2015 17:44:46 -0600 Subject: [PATCH 5/5] Bugfix --- unit_tests/lab9_tests.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/unit_tests/lab9_tests.py b/unit_tests/lab9_tests.py index 75c6167..6dd7a78 100644 --- a/unit_tests/lab9_tests.py +++ b/unit_tests/lab9_tests.py @@ -2,19 +2,19 @@ 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}] + 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 TestLab99.tests: + 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)) + self.assertEqual(luhns.validate(test["i"]), test["o"]) if __name__ == '__main__': unittest.main()