Summary
Highlights
The video introduces key keywords in the Python unit test framework: setup, teardown, setupClass, teardownClass, setupModule, and teardownModule. It aims to explain how to use them practically.
The `setup` method executes before every test method within a class, while the `teardown` method executes after every test method. This means they run multiple times, once for each test method.
A practical example in PyCharm demonstrates the use of `setup` and `teardown`. Four test methods are created, and `setup` is used to simulate a 'login' action before each test, and `teardown` simulates a 'logout' after each test. The output confirms that 'login' and 'logout' statements are printed before and after every individual test.
The `setupClass` method executes only once before all test methods in a class start, and `teardownClass` executes only once after all test methods in the class have completed. This contrasts with `setup` and `teardown` which run for every test method.
The video adds `setupClass` to open an application once before all tests and `teardownClass` to close the application once after all tests. The execution output shows 'opening application' at the very beginning and 'closing application' at the very end, confirming their single execution per class.
The `setupModule` and `teardownModule` methods execute once at the module level. `setupModule` runs before any class or method in the module, and `teardownModule` runs after everything in the module is complete. These are defined outside of any class.
The video concludes by illustrating the complete execution hierarchy: `setupModule` (once for the module), `setupClass` (once per class), `setup` (before every test method), actual test methods, `teardown` (after every test method), `teardownClass` (once per class), and finally `teardownModule` (once for the module).