If you’re new to Python testing, you’ve probably heard of Pytest – a lightweight and powerful testing framework that makes writing and running tests simple and enjoyable. Whether you’re a developer ensuring code quality or diving into automated testing, Pytest is a fantastic choice. In this post, we’ll walk you through running your first test with Pytest, step by step.
As per Wikipedia –
Pytest is a Python testing framework that originated from the PyPy project. It can be used to write various types of software tests, including unit tests, integration tests, end-to-end tests, and functional tests. Its features include parametrized testing, fixtures, and assert re-writing.
But why to use PyTest
- Intuitive and Minimal Setup: Pytest works out of the box, so minimal configuration is required to get started.
- Readability and Simplicity: Tests written with Pytest are concise and easy to read, helping you focus on logic rather than boilerplate.
- Rich Assertions: Pytest provides detailed and helpful error messages, making debugging straightforward.
- Extensive Plugin Ecosystem: Pytest’s plugins can handle almost any testing need, from generating HTML reports to parallelizing tests.
- Scalability: Whether you’re testing a small script or a large-scale application, Pytest scales seamlessly.
So, let’s start to write our very first test in PyTest. We will do so in a series of simple steps –
- Install PyTest
- Create a file and write a test case in it
- Run Your Test
- Run Test and also show print statements
The only prerequisite for this article is that Python should already be installed on your system.
Install PyTest
We can install PyTest by running the below command in the terminal
pip install pytest
The output of the pip install pytest
command on a successful installation will look like this –
We can also verify the installation using the “pytest --version
” command.
That’s it. With this, the PyTest Is installed and ready to use.
Create a test file and write a test case in it
Let’s create a new test file where we will write our test cases.
Now, there are a few guidelines for naming both “test files” and “test methods.”
- Test files should start with “
test_
” or ends with “_test
“ - Test methods should also start with “
test_
“
So, let’s create a new test file, “test_codekru,” with the below test method added to it.
def test_addition():
assert 1 + 1 == 2
Here, notice that both the method and file names start with “test_
“, ensuring adherence to the naming conventions.
Run your test
To run your tests, open a terminal, navigate to the directory where test_codekru.py
is located, and type the pytest
command. This will automatically find all test files (files that either start with “test_” or end with “test”) and run all the test functions inside them that begin with “test_”.
Below is the complete video demonstrating the process of running the test case, starting from creating the file.
Run the test and also show print statements
Let’s try adding the print statement in the test method, as shown below
def test_addition():
print("Running the test..")
assert 1+1 == 2
And now, run the “pytest
” command once again.
As we can observe, the print statement did not appear in the console. This is because, by default, pytest hides print statements to maintain a clean and concise output.
To display the print statements in the console, use the pytest -s
command, as shown below
Now, we can see that the print statements are printed in the console.
This is it. We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.