To unit test your code, you need to install the package pytest:
Assertions: are used to validate conditions in the unit tests
- assert output: Assert that 'output' is True
- assert not output: Assert that 'output' is False
- assert output == value: Assert that 'output' is equal to 'value'
- assert output != value: Assert that 'output' is not equal to 'value'
- assert output in collection: Assert that 'output' is an item in the collection 'collection'
- assert output not in collection: Assert that 'output' is not an item in the collection 'collection'
Simple unit test:
Create a file
hello.py that contains a simple function 'hello':
Create a unit test file
test_hello.py:
To execute the unit test, open a terminal and run 'pytest' command from the project folder:
If there's an error in the unit test, you should see:
Using fixtures: @pytest.fixture
They allow you to mockup classes and inject them dynamically in your unit tests.
Create a file
addition.py that contains a simple class 'Addition':
Create a unit test file
test_addition.py:
To execute the unit test, open a terminal and run 'pytest' command from the project folder: