tldr; My unittests are running twice. Any idea why?
Consider that I have 2 files:
a.py
checker.py
I have a weird requirement for a.py. a.py has the following code and is required to have this code:
import checker
def factorial(n):
if n == -1:
raise Exception("Raised this exception on purpose")
result = 1
for i in range(1,n+1):
result *= i
return result
total = 53
checker.runTests()
I'd need to be able to run the tests for factorial in checker.py
import unittest
class MyTestCase(unittest.TestCase):
def test_factorial3(self):
import a
self.assertEqual(a.factorial(3), 6)
def test_factorial4(self):
import a
self.assertEqual(a.factorial(4), 24)
def test_factorial5(self):
import a
self.assertEqual(a.factorial(5), 120)
def test_factorialnegative1(self):
import a
self.assertEqual(a.factorial(-1), 0)
def test_total(self):
import a
self.assertEqual(a.total, 53)
def runTests():
runner = unittest.TextTestRunner(verbosity=2)
suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
runner.run(suite)
I'm open to suggestions on how to improve checker.py if there's any better ways to handle the circular dependencies. However, my issue is that when I run a.py, I don't get expected output, particularly the tests are run twice? See:
test_factorial3 (cisc106checker.MyTestCase) ... test_factorial3 (cisc106checker.MyTestCase) ... ok test_factorial4 (cisc106checker.MyTestCase) ... ok test_factorial5 (cisc106checker.MyTestCase) ... ok test_factorialnegative1 (cisc106checker.MyTestCase) ... ERROR test_total (cisc106checker.MyTestCase) ... ok
====================================================================== ERROR: test_factorialnegative1 (cisc106checker.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/tnj/PycharmProjects/AutoExam/cisc106checker.py", line 21, in test_factorialnegative1 self.assertEqual(x.factorial(-1), 0) File "/Users/tnj/PycharmProjects/AutoExam/mattsapQ16.py", line 10, in factorial raise Exception("Matt Raised this exception on purpose") Exception: Matt Raised this exception on purpose
---------------------------------------------------------------------- Ran 5 tests in 0.001s
FAILED (errors=1) ok test_factorial4 (cisc106checker.MyTestCase) ... ok test_factorial5 (cisc106checker.MyTestCase) ... ok test_factorialnegative1 (cisc106checker.MyTestCase) ... ERROR test_total (cisc106checker.MyTestCase) ... ok
====================================================================== ERROR: test_factorialnegative1 (cisc106checker.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/tnj/PycharmProjects/AutoExam/cisc106checker.py", line 21, in test_factorialnegative1 self.assertEqual(x.factorial(-1), 0) File "/Users/tnj/PycharmProjects/AutoExam/mattsapQ16.py", line 10, in factorial raise Exception("Matt Raised this exception on purpose") Exception: Matt Raised this exception on purpose
---------------------------------------------------------------------- Ran 5 tests in 0.005s
FAILED (errors=1)
However, when I run it from checker.py, it's only run once. What is going on? Why is it running twice?
python -m unittest discover, and it will find and run your tests.__import__lines withimport a, and you should fix the references toxto beathroughout the tests.__main__clause in it. Either you have misunderstood some part of this assignment, or the person who wrote the assignment needs to find a new job.