Skip to content

Guide · Open source

How to use pytest: install, write tests and run them

Updated · written with AI from the project's own documentation and checked against our catalogue data

pytest is an open-source testing framework for Python. It lets you write tests as plain Python functions that use simple assert statements, then runs them and reports the results. This guide is for Python developers and beginners who want to add automated tests to a project, and it covers installing pytest, writing a first test and deciding whether it fits your work.

pytestpytest-dev/pytest · ★ 14.7k · MIT

Quick start

  1. 1Check Python 3. Confirm that Python 3 is installed, since it is the prerequisite for pytest.
  2. 2Install pytest. Run pip install -U pytest to install the latest release from PyPI.
  3. 3Create a test file. Write a Python function whose name describes the behavior you are checking.
  4. 4Add an assert. Use a plain assert statement to state the expected result.
  5. 5Run pytest. Execute the pytest command from your project folder to run the tests.
  6. 6Read the report. Check which tests passed or failed and fix the failing code or test.

Install pytest with pip

pytest is installed with pip, the Python package manager, and requires Python 3. The project's README gives the install command for the latest release from PyPI.

  • Make sure Python 3 is available on your machine.
  • Run the install command below in your terminal.
pip install -U pytest

Write your first test function

In pytest, a test is a plain Python function. You check results with the ordinary assert statement, so there is no special assertion syntax to learn. If the assert fails, pytest reports the test as failed.

The project's description says the framework stays easy for small tests while also supporting complex functional testing, so the same basic style scales as your needs grow.

  • Write the test as a function in a Python file.
  • Use assert to state what the result should be.
  • Keep each test small and focused on one behavior.

Run your tests and read the results

pytest runs the tests and reports which passed and which failed. Use the command line from the root of your project to start a run.

  • Run the pytest command in the folder that holds your tests.
  • Read the report to find which test failed and why.
pytest

Organize tests for larger projects

The framework is designed to support more advanced test organization as a codebase grows. Teams maintaining larger Python projects can use it to structure many tests rather than only a handful of small ones.

The README excerpt provided does not describe the specific organization features in detail, so check the official documentation at pytest.org for those specifics.

Compare pytest with similar tools

Other Python testing tools include unittest, nose2 and Hypothesis. Choose based on your project's needs. pytest is aimed at keeping small tests simple while still handling complex functional testing.

  • unittest: a testing module for Python.
  • nose2: another Python test runner.
  • Hypothesis: a tool for property-based testing.

What people use it for

  • Adding automated tests to a Python project
  • Teaching yourself how to test code using simple functions
  • Structuring a larger Python codebase's test suite
  • Checking functional behavior of Python code with assert statements

Frequently asked questions

Is pytest free to use?

pytest is released under the MIT licence, according to the project listing. The source does not describe any paid plans.

What do I need to run pytest?

The prerequisite listed is Python 3. You install pytest with pip using the command pip install -U pytest.

Do I need to learn a new assertion syntax?

No. pytest tests are plain Python functions that use the standard assert statement.

Can pytest test code written in JavaScript or Java?

No. pytest is specific to Python, so projects that need tests in other languages should use a tool built for them.

How is pytest different from unittest?

unittest is another testing tool for Python that the source lists as a comparable project. pytest focuses on simple function-based tests that remain easy as they grow more complex.

Why won't my pytest command run?

The source excerpt does not include troubleshooting steps. Confirm Python 3 is installed and that pytest was installed with pip, then check the documentation at pytest.org.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

pytest suits Python developers, beginners and teams with larger Python codebases who want simple tests that can grow in complexity. If you do not write Python or need tests in another language such as JavaScript or Java, pick a tool built for that language.

Install options & downloads