Pytest Tutorial - 3 | The Basics - Write Tests
In our previous article, we understood how to install pytest via requirements.txt.
Learning Goal:
In this tutorial we will start by writing some unit tests for a calculator app.
After finishing this tutorial you will be able to understand
How basic unit tests can be performed with the help of pytest?
How does pytest work?
Given 2 numbers the calculator app, it can perform below operations:
Add
Subtract
Multiply
Divide
If denominator is not 0
Throw a message (Can't divide by 0) if the denominator is 0.
Now let’s start with writing some unit tests to validate above functionalities and see pytest in action.
Prerequisites:
Clone the repo in your desired location:
git clone https://github.com/TheEngrNest/pytest-tutorial-3.git
We will start by opening this project in PyCharm.
Once you have opened the project in PyCharm,
kindly go through the contents of calculator.py inside calculator_app package.
(can be seen right above the highlighted tests package in below step’s screenshot)
let’s create a test package, the name should be tests
In the ‘tests’ folder lets create a python file called: ‘test_calculator.py’.
Lets import pytest and add 4 test functions in tests/test_calculator.py for all the 4 functions present in calculator_app.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # Contents of tests/test_calculator.py # ----------------------------------------------------- from calculator_app.calculator import add, subtract, multiply, divide # Unit test function to test the add function def test_add(): a = 8 b = 2 assert 10 == add(a, b) # Unit test function to test the subtract function def test_subtract(): a = 8 b = 14 assert -6 == subtract(a, b) # Unit test function to test the multiply function def test_multiply(): a = 8 b = 2 assert 16 == multiply(a, b) # Unit test function to test the divide function def test_divide(): a = 8 b = 4 assert 2 == divide(a, b) |
Explanation of above code:
So we started by importing all 4 functions from calculator_app.calculator.py in line #4.
We imported these to call them inside our test functions and assert their results.
In line #8, #15, #22 and #29, We have defined our test functions.
If you notice the function name starts with test_ .
This is one of the features in pytest where test functions names should either start or end with “test”,
in order to help pytest discover the tests and run.
The code inside each function is easy to understand,
So we have 2 variables hardcoded with some values, and called respective calculator functions inside with these 2 variables.
Lastly we are asserting against the expected value.
Now that you have understood the above code (step 4), theoretically,
let’s do some practical and run the tests to see pytest in action.
Pytest can be run by command: pytest
(venv) C:\Users\xxxx\PycharmProjects\pytest-tutorial-3>pytest ========================= test session starts ========================== platform win32 -- Python 3.10.0, pytest-7.1.2, pluggy-1.0.0 rootdir: C:\Users\xxxx\PycharmProjects\pytest-tutorial-3 collected 4 items tests\test_calculator.py .... [100%] ========================== 4 passed in 0.02s ===========================
If you see the above snippet of the console, it shows that
it collected 4 tests from tests/test_calculator.py
and all 4 tests passed successfully in 0.02s.
With this we were successfully able to
create 4 unit tests to test 4 functions of calculator_app/calculator.py
Run the tests using a terminal/cmd using the “pytest” command.
Now as part of small exercise, you can try below problems:
The above result of pytest run, is very summarized version, try to run pytest using below command:
Try to update variable “b” in the test_divide function and then run the tests, see what happens, maybe you can try to fix it if required.
pytest --verbose
Comments
Post a Comment