My notes on TDD in python
What is TDD?
A process where the developer takes personal responsibiltiy for the quality of their code. Unit tests are written before the productin code.
Red > Green > Refactor
Why do we need unit test?
Virtual envs solve the dependency issue by creating isolated python environments thst can be customized per project Virtual Envs are directoris containing links ot the system’s python install and providing sub-directories for installing additional python packages in that particular VE. The PATH env variable is updated to point to the virtual env when that ve is activated.
pip install virtualenvvirtualenv<Namesoruce./<Name>/bin/activatedeactivateor venv in python3 (comes by default in python>3) except for creating venv -> python3 -m venv <Name>
Tests are python function with “test” at the begining of the function name. Tests does standard verification of values using assert method.
PyTest will automatically discover tests when we follow some standard conventions. Test functions should have tests in the beginning of the function name. Test Classes should have “Test” in the beginning of the class and name and SHOULD NOT have init method. File name should start with test_example.py or end with example_test.py
One key feature of all unit test frameworks is providing the ability to execute setup code before and after the tests. Pytest provides this capability with both XUnit style setup/teardown functions and with Pytest fixtures. … The XUnit style setup and teardown functions allow you to execute code before and after: Test modules
, Test Functions , Test Classes , and Test Methods in Test Classes. ... Using these setup and teardown functions can help reduce code duplication by letting you specify setup and teardown code once at each of the different levels as necessary rather than repeating the code in each individual unit test.
python3 -m pytest -v -s test_Xunit.py
By default Pytest runs all tests that it finds in the current working directory and sub-directory using the naming conventions for automatic test discovery.
- There are several pytest command line arguments that can be specified to try and be more selective about which tests will be executed.
- You can simply pass in the module name to execute only the unit tests in one particular module.
- You can also simply pass in a directory path to have pytest run only the tests in that directory.
- You can use the “-k” option to specify an evaluation string based on keywords such as: module name, class name, and function name.
- You can use the “-m” option to specify that any tests that have a “pytest.mark” decorator that matches the specified expression string will be executed.
Here are some additional command line arguments that can be very useful.
* Dummy - Objects can be passed around as necessary but do not have any type of test implementation and should be never used.
* Fake - These objects generally have a simplified functional implementation of a particular interface that is adequate for testing not for production.
* Stub - These objects provide implementation with canned answers that are suitable for the test.
* Spies - These objects provide implementations that record the values that were passed in so they can be tested.
* Mocks- These objects are preprogrammed to expect specific calls and parameters and can throw exceptions when necessary.
unittest.mock - Python mock library