Pytest for Unit Testing: Beginner Tutorial
Updated: May 12
In this post, I will be taking you through end to end steps to get started with Pytest, popular python based testing framework.
To visit more such posts, follow: #APITesting #Pytest #UnitTesting
Step 01 - INSTALL PYTHON
Step 1 - Install & Verify Pytest Step 2 - Before you create the first Pytest
Step 3 -Create your first Pytest
Step 4 - Run Test Step 5 - Add more tests Step 6 - Parametrize Tests Step 7 - Add fixtures
This post assumes that you have got python installed on your system and it's set in the environment variables. Verify same by going to command prompt and give the command:
In case you have not set up Python, follow steps here
Python comes bundled with pip utility. Install Pytest using PIP with below command:
pip install pytest
It would simply install pytest.
To verify, give below command:
It's so easy to install pytest & get started with it. To write our first test case, we will use Idle,
an editor which comes bundled with Python.
Create a folder named - Pytest_Project (any name will do for that matter)
We will create a file named first_test.py .
Further, we will create a function inside it which is a simple function named add_numbers to add 2 numbers.
Further to that in the same file, we will create a method named add_test to test this function
Name of pytest file should always begin or ends with the word "test"
Name of the method to test a functionality should start with the word "test".This is the way pytest differentiates normal python function with any test case.
Open Idle and create a file named us first_test under Pytest_Project directory as below:
Finally, you should have:
In first_test.py, create a sample function to add two numbers
In first_test.py, create a function to test the function created above by adding these lines. For this, we also need to import the pytest module in the script.
###Write function to test add method created above.Start with word test def test_add(): assert add_numbers(1,3)==4
So script now looks like:
Your first test case is ready!
Go to command prompt and directory Pytest_Project
Give below command to run test :
Below output is shown on the running test:
Note a dot after first_test, it represents one test case which is test_add method in our case.
If we need to get more info, we can run the test in verbose mode:
Here it gives the name of the test case as well, along with the result of test case
Let's add one more function to subtract two numbers:
###Write function to subtract two numbers def subtract_numbers(num1, num2): return num2 -num1
Let's add one more test case to verify this function:
###Write function to test subtract method.Start with word test def test_subtract(): assert subtract_numbers(1,3)==1
Note that I have intentionally given the wrong output so that this test case gets failed
Now complete script looks like:
Let's run this test and check the output:
Note that the passed test case is marked as dot & failed one is represented by F