Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Install pytest and Write Your First Test?

Software TestingPython
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 30, 2026
6 min read

To start a pytest tutorial from zero: create a virtual environment, run pip install pytest, write a file named test_something.py with a function whose name starts with test_, and run pytest. A test passes when its assert statements hold and no exception is raised. This guide uses pytest 8 on Python 3.13, and everything also works on Python 3.12.

pytest is the most widely used Python testing framework because tests are plain functions with plain assert statements, with no classes or special assertion methods to learn. You will install it, write a small module, test it, read the output and run a single test.

Table of Contents

  • Installing pytest in a virtual environment
  • Setting up a small project to test
  • Writing your first test with assert
  • Testing that code raises an exception
  • Reading a failing test
  • Running a subset of tests
  • Frequently Asked Questions
    • Do I need a class to write pytest tests?
    • Why does pytest not find my test file?
    • What is the difference between pytest and unittest?
    • How do I stop after the first failure?
  • Summary
  • Related Articles

Installing pytest in a virtual environment

Always install tools per project so versions do not clash. In a new project folder:

bash

The last command prints something like pytest 8.x.x. Running python -m pip instead of bare pip guarantees you install into the Python you just activated.

To make the setup repeatable, record the dependency in a file:

bash

Modern projects usually declare it in pyproject.toml under an optional dependency group, but requirements-dev.txt is fine for learning.

Setting up a small project to test

Create this layout:

text

Put the code under test in calculator.py:

python

Keeping production code and tests in separate files means the tests can import the module the same way any user would.

Writing your first test with assert

Create test_calculator.py in the same folder:

python

Each test follows the arrange, act, assert pattern: set up the input, call the code, check the result. Names should describe the behaviour, so a failing test name tells you what broke before you open the file.

Run all tests from the project folder:

bash
text

Each dot is a passing test. The letters you will see in other runs are F for a failure, E for an error and s for a skipped test.

Testing that code raises an exception

Use pytest.raises as a context manager. The test passes only if the block raises the given exception type, and match checks the message with a regular expression:

python

For floating point results, do not compare with == after arithmetic that rounds. Use pytest.approx:

python

Reading a failing test

Change the first test to assert add(2, 3) == 6 and run pytest again. You will see output similar to this:

text

The > line marks the failing statement and the E lines show the real values. This detail comes from pytest rewriting your assert statements when it imports test modules, which is why you never need assertEqual.

Running a subset of tests

As the suite grows, you rarely run everything while developing. These options cover most needs:

CommandWhat it runs
pytest test_calculator.pyOne file
pytest test_calculator.py::test_add_two_numbersOne test by node ID
pytest -k divideTests whose name contains divide
pytest -xStops at the first failure
pytest --lfOnly the tests that failed last time
pytest -vOne line per test with its name
pytest -qQuiet output

You can also start pytest with python -m pytest, which adds the current directory to sys.path; this is a handy fix when imports fail in a simple project layout.

Frequently Asked Questions

Do I need a class to write pytest tests?

No. Plain functions are the normal style. Classes are optional and only useful for grouping related tests; a class name must start with Test and must not define __init__.

Why does pytest not find my test file?

By default pytest collects files named test_*.py or *_test.py, and inside them functions named test_*. A file called calculator_tests.py or a function called check_add is silently ignored.

What is the difference between pytest and unittest?

unittest is in the standard library and uses classes and methods such as assertEqual. pytest uses plain assert, has fixtures and parametrization built in, and can also run existing unittest test cases.

How do I stop after the first failure?

Run pytest -x, or pytest --maxfail=3 to stop after three failures.

Summary

Install pytest into a virtual environment with python -m pip install pytest, name files test_*.py and functions test_*, and use plain assert for checks. Use pytest.raises for exceptions, pytest.approx for floats, and options such as -k, -x and --lf to run only what you need.

Related Articles

  • How Do You Write Unit Tests for Legacy Code?
  • How Does pytest Discover Tests and Report Assertions?
  • Learn Software Testing: Complete Roadmap from Beginner to Advanced
Share

Similar posts

  • What Are pytest Fixture Scopes and Teardown with yield?
  • What Are pytest Fixtures and How Do You Use Them?
  • How Does pytest Discover Tests and Report Assertions?
  • How Do You Write Unit Tests for Legacy Code?
  • What Is Code Coverage and What Percentage Is Good Enough?
  • Stub, Mock, Fake and Spy: What Are Test Doubles?
  • What Is Behavior-Driven Development (BDD) with Given-When-Then?
  • What Is Test-Driven Development (TDD) Red-Green-Refactor?