From 6ff0e26c712618c29fa49741788f84e30390f9f9 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 13 Dec 2020 01:04:41 +0100 Subject: [PATCH] Added unittests --- tests/Program_test.py | 46 +++++++++++++++++++++++++++++++++++++++++++ tests/__init__.py | 0 tests/tester.py | 23 ++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 tests/Program_test.py create mode 100644 tests/__init__.py create mode 100644 tests/tester.py diff --git a/tests/Program_test.py b/tests/Program_test.py new file mode 100644 index 0000000..52d07a5 --- /dev/null +++ b/tests/Program_test.py @@ -0,0 +1,46 @@ +import unittest +from datetime import datetime + +from new.program import Program + + +class ProgramTest(unittest.TestCase): + + def setUp(self) -> None: + self._program = Program() + + @staticmethod + def _to_date(value: str): + return datetime.strptime(value, '%d.%m.%Y') + + def test_sqrt(self): + """ + Function to test the sqrt function of program, which uses the sqrt function of math package + :return: + """ + print(f'[ {__name__}.test_sqrt ]') + + result = self._program.sqrt(9.0) + self.assertEqual(result, 3.0) + + result = self._program.sqrt(144.0) + self.assertEqual(result, 12.0) + + def test_date(self): + """ + Function to test the date function of program, which calculates the difference between two dates + :return: + """ + print(f'[ {__name__}.test_date ]') + + result = self._program.date(self._to_date('01.01.2020'), self._to_date('31.12.2020')) + self.assertEqual(result, 365) + + result = self._program.date(self._to_date('31.12.2020'), self._to_date('01.01.2020')) + self.assertEqual(result, 365) + + result = self._program.date(self._to_date('01.12.2020'), self._to_date('31.12.2020')) + self.assertEqual(result, 30) + + result = self._program.date(self._to_date('01.01.2000'), self._to_date('31.12.2020')) + self.assertEqual(result, 7670) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tester.py b/tests/tester.py new file mode 100644 index 0000000..67255fa --- /dev/null +++ b/tests/tester.py @@ -0,0 +1,23 @@ +import unittest + +from tests.Program_test import ProgramTest + + +class Tester: + + def __init__(self): + self._suite = unittest.TestSuite() + + def create(self): + self._suite.addTest(ProgramTest('test_sqrt')) + self._suite.addTest(ProgramTest('test_date')) + + def start(self): + runner = unittest.TextTestRunner() + runner.run(self._suite) + + +if __name__ == '__main__': + tester = Tester() + tester.create() + tester.start()