What Is pytest? A Beginner's Guide

What Is pytest? A Beginner's Guide

Pytest is a versatile testing framework for Python that simplifies the process of writing and running tests. It supports simple unit tests as well as more complex functional tests. This tutorial will provide a deep dive into pytest, covering installation, writing tests, using fixtures, parameterization, and other advanced features.

As per the Python Developers Survey, 50% of Python developers use pytest, making it one of the most popular unit testing frameworks.

Article content
Most Popular Unit-Testing Frameworks

1. Installing Pytest

To get started with pytest, you need to install it. This can be done easily using pip:

pip install pytest        

2. Writing Your First Test

Pytest looks for files that start with test_ or end with _test.py, and within these files, it looks for functions that start with test_ to identify as test cases.

Example: Basic Test Function

Create a file named test_math.py with the following content:

# test_math.py
def test_addition():
    assert 1 + 1 == 2        

To run the test, navigate to the directory containing your test file and run:

pytest        

Pytest will automatically discover and run your test, providing a summary of the results.


Article content
Output of pytest command

3. Understanding Assertions

Assertions in pytest are straightforward. You use the standard assert keyword and pytest provides an informative output when tests fail, making debugging easier.

Example: Assert with Detailed Output

# test_math.py
def test_division():
    result = 10 / 2
    assert result == 5, "Expected result to be 5"        

If the assertion fails, pytest outputs a detailed message, showing the expected vs. actual results, which helps in quickly identifying the issue. For example, if we altered the last example to make the assertion fail, the output of the pytest command will be as the following:

Article content
Pytest - Faild Assertion

4. Grouping Tests with Classes

For better organization, especially in larger test suites, you can group tests using classes. Note that classes should not have an __init__ method because pytest will instantiate the class once per test function.

Example: Grouping Tests

# test_math.py
class TestMathOperations:
    def test_multiplication(self):
        assert 3 * 3 == 9

    def test_subtraction(self):
        assert 10 - 4 == 6        

5. Using Fixtures for Setup and Teardown

Fixtures are a powerful feature in pytest that allows you to set up and clean up resources needed by tests. They help in preparing the necessary state or environment for your tests.

Example: Fixtures

# conftest.py
import pytest

@pytest.fixture
def sample_data():
    return {"key1": "value1", "key2": "value2"}

# test_math.py
def test_data_retrieval(sample_data):
    assert sample_data["key1"] == "value1"
    assert "key2" in sample_data        

Fixtures can be defined in conftest.py to make them available across multiple test files.

6. Parameterizing Tests

Parameterization allows you to run the same test with different inputs. This is useful for testing functions with multiple sets of inputs and expected outputs.

Example: Parameterizing Tests

# test_math.py
import pytest

@pytest.mark.parametrize("num, expected", [(2, 4), (3, 9), (4, 16)])
def test_square(num, expected):
    assert num ** 2 == expected        

In this example, test_square runs three times with different values for num and expected.


Article content
Pytest command output with parameterizing tests

7. Handling Expected Failures

Sometimes you want to mark a test as expected to fail. This can be useful during development when working on incomplete features.

Example: Expected Failures

# test_math.py
import pytest

@pytest.mark.xfail
def test_not_implemented():
    assert 0        

Here, test_not_implemented is marked as xfail, which means "expected to fail." Pytest will not treat this as a failure if the test indeed fails.


Article content
Intended failure

8. Skipping Tests

There are scenarios where you may want to skip certain tests. This can be based on conditions like the environment, dependencies, or platform specifics.

Example: Skipping Tests

# test_math.py
import pytest
import sys

@pytest.mark.skip(reason="Not supported on Windows")
def test_linux_feature():
    if sys.platform == "win32":
        pytest.skip("Test skipped on Windows")
    assert True        

Use @pytest.mark.skip(reason="...") to skip tests unconditionally, or pytest.skip() to skip within the test function.


Article content
Pytest command output with skipping option

9. Command Line Options

Pytest provides various command-line options to customize test runs:

  • Run all tests: pytest
  • Run tests in a specific file: pytest test_math.py
  • Run a specific test function: pytest test_math.py::test_addition
  • Verbose output: pytest -v
  • Disable output capture: pytest -s
  • Run tests matching a pattern: pytest -k "addition or multiplication"
  • Run tests with a specific mark: pytest -m "slow"

10. Pytest Plugins and Extensions

Pytest has a rich ecosystem of plugins and extensions that can extend its functionality. Some popular plugins include:

  • pytest-cov: For measuring code coverage.
  • pytest-xdist: For running tests in parallel.
  • pytest-mock: For using mock objects in tests.

Example: Installing a Plugin

pip install pytest-cov        

Using pytest-cov for Coverage Reports

pytest --cov=my_project        

This command runs tests and reports on the code coverage of my_project.


Article content
Output of command pytest with pytest-cov enabled

Pytest Cheat Sheet: CLI Commands

Here's a quick reference guide for commonly used pytest CLI commands:


Article content
Pytest Cheat Sheet

Pytest is a powerful and flexible testing framework that can accommodate simple unit tests and more complex functional tests. Its ease of use, combined with a robust set of features like fixtures, parameterization, and plugins, makes it an essential tool for Python developers. To fully leverage pytest, explore its extensive documentation and community plugins, which can help streamline your testing workflow and improve code quality.

TestCrew and LambdaTest: Elevating Software Quality Through Strategic Partnership

TestCrew is a leading name in software testing, renowned for its expertise in delivering comprehensive quality assurance solutions. With a dedicated team of experts and a commitment to excellence, TestCrew ensures that software products are reliable, secure, and meet the highest industry standards. Our range of services, from functional to performance testing, helps businesses deliver seamless software experiences.

Our strategic partnership with LambdaTest enhances TestCrew's capabilities by leveraging LambdaTest's advanced cloud-based platform. This collaboration allows us to execute Python Selenium-based test scripts online, running tests in parallel at scale. By integrating LambdaTest’s robust infrastructure, TestCrew can accelerate test execution times, ensuring faster and more efficient testing cycles for our clients.


To view or add a comment, sign in

More articles by TestCrew | Quality Engineering & Software Testing

Insights from the community

Others also viewed

Explore topics