Getting Started with Pytest: A Simple Guide for Beginners
Knowledge Base

Getting Started with Pytest: A Simple Guide for Beginners

Deena P B
Deena P B
3 min read3059 views
Published Date: Jun 18, 2025

What is pytest?

Pytest is an awesome testing framework that is both powerful and easy to use. Commonly used for unit testing, functional testing and UI automation using Selenium and Appium. Pytest is a tool that is widely known for its simplicity, scalability, and flexibility, making it one of the developers' and testers' preferred tools.

Why choose pytest?

Pytest stands out for several reasons, making it a favourite among developers and testers alike:

Easy and Readable Syntax — Test cases can be written with just Python functions.

Built-in Fixtures: – Provide for the setup and teardown of tests without additional code.

Parameterization – Allow to run the same test with multiple data sets.

Assertive Tests – Delivers superior error reporting as compared to the standard unit-test.

●     Parallel Execution – To run multiple tests in parallel.

Getting Started with Pytest A Simple Guide for Beginners

Getting started

To get started with Pytest, install it using pip:

pip install pytest

Verify the installation:

pytest --version

Writing your first test with pytest

Pytest follows a function-based approach for writing test cases. Here’s a simple example:

# test_example.py

def test_addition():

assert 2 + 3 == 5, "Addition test failed!"

def test_subtraction():

assert 5 - 2 == 3, "Subtraction test failed!"

Run the test using the command:

pytest test_example.py

If the test passes, you’ll see output indicating success; otherwise, Pytest provides detailed error messages.

Using pytest fixtures

Fixtures in Pytest help set up preconditions for test cases, such as launching a browser, initializing a database, or setting up test data.

Example of a simple fixture:

import pytest

@pytest.fixture

def sample_data():

return {"name": "Nikhil", "age": 25}

def test_data_content(sample_data):

assert sample_data["name"] == "Nikhil"

assert sample_data["age"] == 25

 

Running Specific Tests

You can run specific tests by using the -k option:

pytest -k test_addition

Or run tests in verbose mode:

pytest -v

Parameterized tests in pytest

You can use pytest.mark.parametrize to run the same test with multiple inputs:

import pytest

@pytest.mark.parametrize("num1, num2, expected", [(2, 3, 5), (5, 5, 10), (0, 1, 1)])

def test_addition(num1, num2, expected):

assert num1 + num2 == expected

This approach helps reduce code duplication and improves test coverage.

Organizing tests

For larger projects, organizing tests into separate files and folders is a good practice.

/tests

   ├── test_login.py

   ├── test_signup.py

   ├── conftest.py  # Shared fixtures

   ├── reports/  # Test reports

This structure improves maintainability and makes it easier to scale test automation.

Running Tests in Parallel

To speed up execution, run tests in parallel using pytest-xdist:

pip install pytest-xdist

pytest -n 4  # Runs tests in 4 parallel threads

Generating Test Reports

Generate a detailed test report using:

pytest --html=report.html --self-contained-html

This will create an HTML report for easy analysis.

Conclusion

Pytest is a flexible and powerful testing framework that makes writing and managing tests easier. Whether you are working with unit tests, API tests, or UI automation, Pytest provides all the features needed to create efficient and maintainable test suites.

Start using Pytest today and improve the reliability of your Python applications!

 

 

 

Tags:QASoftware quality assuranceSoftware testing