Course Content
Python Advanced Concepts
Python Advanced Concepts
1. Mastering Python Modules and Imports
2. Mastering Error Handling in Python
3. Mastering File Handling in Python
Introduction to File Handling in PythonReading and Writing Files: Handling Text and Data EfficientlyAdvanced File Operations: Modifying and Managing File ContentWorking with File Paths in PythonUsing Context Managers for File Handling in PythonCreating Custom Context ManagersHandling Binary Files in Python
6. Mastering Iterators and Generators in Python
Using Assert Methods in Unittest: Validating Test Results
The assert
methods are part of the unittest.TestCase
class and are used to check conditions in your tests.
Simply put, each test method in the Test class concludes with a statement such as self.assert
.
Commonly Used assert Methods
Method | Check | Example |
---|---|---|
assertEqual(a, b) | a == b | assertEqual(sum([1, 2, 3]), 6) |
assertNotEqual(a, b) | a != b | assertNotEqual(1, 2) |
assertTrue(x) | bool(x) is True | assertTrue(isinstance(123, int)) |
assertFalse(x) | bool(x) is False | assertFalse(isinstance("hello", int)) |
assertIs(a, b) | a is b | a = 1, b = a |
assertIsNone(x) | x is None | book.price = None |
assertIn(a, b) | a in b | assertIn(2, [1, 2, 3]) |
assertIsInstance(a, b) | isinstance(a, b) | assertIsInstance(123, int) |
Also, assertRaises(Error, func, *args, **kwargs)
is used to test that an error is raised. For example:
This checks that converting "xyz" to integer raises ValueError
.
Everything was clear?
Thanks for your feedback!
Section 5. Chapter 2