from python_test_case import PythonTestCase, run_tests import unittest # Handle capturing what the students print from io import StringIO from unittest.mock import patch class HelloWorldTest(PythonTestCase): def test(self): """ Should print 'Hello, World!' """ # The above docstring is used as the description # of the test to the student with patch("sys.stdout", new=StringIO()) as output: # Import the module here, as the code will print as soon # as we import it import attempt self.assertEqual(output.getvalue().strip(), "Hello, World!") if __name__ == "__main__": run_tests(HelloWorldTest)
def test_W_end(self): """The last value for the W array is correct""" self.assertAlmostEqual(attempt.W[-1], 9.494852380803035) def test_Z_end(self): """The last value for the Z array is correct""" self.assertAlmostEqual(attempt.Z[-1], 41.47999849170943) def test_W_start(self): """The first value for the W array is correct""" self.assertAlmostEqual(attempt.W[0], 12) def test_Z_start(self): """The first value for the Z array is correct""" self.assertAlmostEqual(attempt.Z[0], 40) def test_lengths(self): """The arrays all have the correct lengths """ self.assertEqual(size(attempt.Z), 201) self.assertEqual(size(attempt.W), 201) def test_maxIndex(self): """The maxIndex variables are correct""" self.assertEqual(attempt.maxIndexZ, 113) self.assertEqual(attempt.maxIndexW, 134) # Run the unit tests if __name__ == "__main__": run_tests(Tests)
def test_executive_defined(self): """ Tests that the Executive class is defined """ self.assertClassDefined(attempt, "Executive") def test_executive_inheritance(self): """ Tests that the Executive inherits from Employee """ self.assertIsSubclass(attempt.Executive, attempt.Employee) def test_executive_init(self): """ Tests that the Executive has a correct __init__ method """ self.assertMethodDefined(attempt.Executive, "__init__", 4) executive = attempt.Executive("Joseph Bloggs", 25000, 10000) @patch("attempt.Employee.wage") def test_executive_wage(self, emp_wage): """ Test that the Executive's wage function is correct """ emp_wage.return_value = 25000/26 executive = attempt.Executive("Joseph Bloggs", 25000, 10000) expected_wage = 25000/26 + 10000/26 self.assertEqual(executive.wage(), expected_wage) self.assertCalled(emp_wage, "super().wage()") if __name__ == "__main__": run_tests(InheritanceTests)