How to run your first test with Pytest?

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.

  1. Intuitive and Minimal Setup: Pytest works out of the box, so minimal configuration is required to get started.
  2. Readability and Simplicity: Tests written with Pytest are concise and easy to read, helping you focus on logic rather than boilerplate.
  3. Rich Assertions: Pytest provides detailed and helpful error messages, making debugging straightforward.
  4. Extensive Plugin Ecosystem: Pytest’s plugins can handle almost any testing need, from generating HTML reports to parallelizing tests.
  5. 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 –

The only prerequisite for this article is that Python should already be installed on your system.

We can install PyTest by running the below command in the terminal

pip install pytest
pip intsall pytest

The output of the pip install pytest command on a successful installation will look like this –

successful installation of pytest

We can also verify the installation using the “pytest --version” command.

pytest --version

That’s it. With this, the PyTest Is installed and ready to use.

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.

add test method

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_”.

running the pytest command

Below is the complete video demonstrating the process of running the test case, starting from creating the file.

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.

running test with console statement

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

running test with pytest -s command

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.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *