![[Formales]] ![[module_unittest__451361125.png]] [Unit testing Framework](https://docs.python.org/3/library/unittest.html) <br> From: *Python Documentation* > [!multi-column] > >> [!BLANK] >> - [[Functions]] >> - [[Try Except Else Finally]] >> - [[Errors - Three Categories]] > >> [!BLANK] >> - [[Errors - Team Trip]] >> - [[Try Except Else Finally]] >> - [[Unittest.SpecNeeds]] > [!TLDR] Assert Methods in `unittest` > The `unittest` framework is a crucial component of Python's standard library for developing and running tests. Unit testing in Python focuses on the reliability and maintainability of code. In the context of `unittest` in Python, "assert methods" are used to check for various conditions that should hold in your code. They are employed within the testing framework to verify that the tested code behaves as expected. # Assert ## assertIsInstance This method is used to check if an object is an instance of a specified class or data type. Syntax ```python assertIsInstance(object, classinfo, message='') ``` ### Unittest in a .py File This will pass as 3 is indeed an integer: ```python import unittest class TestMyInteger(unittest.TestCase): def test_number_type(self): self.assertIsInstance(3, int) if __name__ == '__main__': unittest.main() ``` ### Unittest in a .py File and there embedded in a Jupyter Cell When running unit tests in a Jupyter notebook or a Jupyter-like environment in Visual Studio Code (VS Code), where cells are used to segment and execute code, the traditional approach of calling unittest.main() won't work as expected. This is because unittest.main() looks for arguments in the system's command line interface (CLI) and tries to run all tests based on those arguments, which aligns poorly with how cells in notebooks or VS Code operate. Instead, you can use the unittest.TestLoader. TestLoader will load your test cases and then run them using a *unittest.TextTestRunner()* or the *unittest.run()* function. ```python #%% import unittest # Your test case class class TestMyInteger(unittest.TestCase): def test_number_type(self): self.assertIsInstance(3, int) # Code to run the test def run_tests(): # Create a test suite suite = unittest.TestLoader().loadTestsFromTestCase(TestMyInteger) # Run the test suite runner = unittest.TextTestRunner() runner.run(suite) # Execute the test function if __name__ == '__main__': run_tests() ``` ## assertEqual This method checks if the evaluated result of an expression is equal to an expected value. Syntax ```python assertEqual(first, second, msg=None) ``` This will pass as the expression 2 + 2 is indeed equal to 4. ```python self.assertEqual(2 + 2, 4) ``` ## assertTrue This method is used to verify whether the given expression or variable evaluates to `True`. Syntax ```python assertTrue(expr, msg=None) ``` This will pass as the condition is True. ```python self.assertTrue(1 == 1) ``` ## assertIs This method checks if two variables reference the same object. Syntax ```python assertIs(expr1, expr2, msg=None) ``` This will pass since both a and b refer to the same object. ```python a = [1, 2, 3] b = a self.assertIs(a, b) ``` ## assertIn This method checks if a value is present in a given iterable (like a list, set, tuple, dictionary). Syntax ```python assertIn(member, container, msg=None) ``` This will pass since 3 is present in the list with 1, 2, 3 ```python self.assertIn(3, [1, 2, 3]) ``` # Test Case You typically use these in a test case class that is derived from `unittest.TestCase`. ```python import unittest class TestExampleMethods(unittest.TestCase) def test_addition(self) self.assertEqual(1 + 1, 2) def test_instance(self) self.assertIsInstance(3, int) #... and so on for other assert methods if __name__ == '__main__' unittest.main() ``` # Notes - Each `assert` method optionally takes a `msg` argument that can be used to provide a more descriptive error message if the assertion fails. - Using these assertions helps to make your tests more readable and expressive, and the error messages provided upon a test failure are generally more helpful than those provided by the built-in `assert` statement. ![[Unittest.SpecNeeds#Module Unittest - Coordinate Special Needs Overview]]