From 6a5ab2f878edcc464d9a5c96f9f99364b21e5e9d Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Thu, 27 Jun 2024 22:00:12 +0530 Subject: [PATCH] Create unittest_in_python.md Added Content --- contrib/advanced-python/unittest_in_python.md | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 contrib/advanced-python/unittest_in_python.md diff --git a/contrib/advanced-python/unittest_in_python.md b/contrib/advanced-python/unittest_in_python.md new file mode 100644 index 0000000..961d59e --- /dev/null +++ b/contrib/advanced-python/unittest_in_python.md @@ -0,0 +1,185 @@ +# Unittest in Python + +The `unittest` module in Python is a powerful and flexible framework for writing and running tests. It provides a range of tools for constructing and running tests, making it easier to ensure your code behaves as expected. + +### Writing Tests + +A typical test case is created by subclassing `unittest.TestCase` and defining methods that start with `test`. + +### Example: Basic Test Case + +```python +import unittest + +class TestStringMethods(unittest.TestCase): + + def test_upper(self): + self.assertEqual('foo'.upper(), 'FOO') + + def test_isupper(self): + self.assertTrue('FOO'.isupper()) + self.assertFalse('Foo'.isupper()) + + def test_split(self): + s = 'hello world' + self.assertEqual(s.split(), ['hello', 'world']) + with self.assertRaises(TypeError): + s.split(2) + +if __name__ == '__main__': + unittest.main() +``` + +#### Output + +When you run this script, the output will be: + +```python +... +---------------------------------------------------------------------- +Ran 3 tests in 0.001s + +OK +``` + +### Running Tests + +You can run tests by calling the script directly. When executed, `unittest` will find all test cases and run them: + +```bash +python test_script.py +``` + +Alternatively, you can use the command line to discover and run tests: + +```bash +python -m unittest discover +``` + +### Test Fixtures + +Test fixtures are used to set up a known state for tests and clean up afterward. You can use `setUp()` and `tearDown()` methods for this purpose. + +### Example: Test Fixtures + +```python +import unittest + +class TestExample(unittest.TestCase): + + def setUp(self): + self.number = 42 + + def tearDown(self): + del self.number + + def test_addition(self): + self.assertEqual(self.number + 10, 52) + +if __name__ == '__main__': + unittest.main() +``` + +#### Output + +```python +. +---------------------------------------------------------------------- +Ran 1 test in 0.000s + +OK +``` + +### Organizing Tests + +Tests can be organized into test suites to run them collectively. This is useful for running related tests together. + +### Example: Test Suites + +```python +import unittest + +class TestMath(unittest.TestCase): + + def test_add(self): + self.assertEqual(1 + 1, 2) + + def test_subtract(self): + self.assertEqual(5 - 3, 2) + +class TestString(unittest.TestCase): + + def test_upper(self): + self.assertEqual('foo'.upper(), 'FOO') + + def test_isupper(self): + self.assertTrue('FOO'.isupper()) + +if __name__ == '__main__': + suite = unittest.TestSuite() + suite.addTests(unittest.makeSuite(TestMath)) + suite.addTests(unittest.makeSuite(TestString)) + runner = unittest.TextTestRunner() + runner.run(suite) +``` + +#### Output + +```python +.... +---------------------------------------------------------------------- +Ran 4 tests in 0.001s + +OK +``` + +### Assertions + +`unittest` provides a wide range of assertion methods to verify conditions in tests: + +- `assertEqual(a, b)`: Check `a == b` +- `assertNotEqual(a, b)`: Check `a != b` +- `assertTrue(x)`: Check `bool(x) is True` +- `assertFalse(x)`: Check `bool(x) is False` +- `assertIs(a, b)`: Check `a is b` +- `assertIsNot(a, b)`: Check `a is not b` +- `assertIsNone(x)`: Check `x is None` +- `assertIsNotNone(x)`: Check `x is not None` +- `assertIn(a, b)`: Check `a in b` +- `assertNotIn(a, b)`: Check `a not in b` +- `assertIsInstance(a, b)`: Check `isinstance(a, b)` +- `assertNotIsInstance(a, b)`: Check `not isinstance(a, b)` + +### Example: Using Assertions + +```python +import unittest + +class TestAssertions(unittest.TestCase): + + def test_assert_equal(self): + self.assertEqual(3 + 2, 5) + + def test_assert_true(self): + self.assertTrue('FOO'.isupper()) + + def test_assert_in(self): + self.assertIn('h', 'hello') + +if __name__ == '__main__': + unittest.main() +``` + +#### Output + +```python +... +---------------------------------------------------------------------- +Ran 3 tests in 0.001s + +OK +``` + + +The `unittest` module is a powerful framework that provides all the tools necessary to write comprehensive and maintainable tests. +By leveraging its features, you can ensure your code works correctly and prevent regressions, ultimately leading to more robust and reliable software.