Python

Run tests with pytest

Install pytest in your project's virtual environment, then run all your tests, one file or one test by name.

Fill these in once
  1. Go to your project folder
    Terminal
    cd my-project

    Use your own folder name.

  2. Create a virtual environment
    Terminal
    python3 -m venv .venv

    Skip this if your project already has one.

  3. Switch it on
    Terminal
    source .venv/bin/activate

    Your prompt now starts with (.venv).

  4. Install pytest
    Terminal
    pip install pytest
  5. Run all your tests
    Terminal
    pytest

    pytest finds files named test_*.py or *_test.py and runs every function whose name starts with test.

  6. Run the tests in one file
    Terminal
    pytest tests/test_login.py
  7. Run only tests whose name matches
    Terminal
    pytest -k "test_login"

    The -k option picks tests by name. Any test with that text in its name runs.

  8. Stop at the first failure
    Terminal
    pytest -x

    Handy when many tests fail for the same reason.

  9. Show each test's name and result
    Terminal
    pytest -v

    You can mix options, for example pytest -x -v {{file}}.

Check it worked

  1. Check pytest is installed in the environment
    Terminal
    pytest --version
Undo it
  1. Remove pytest
    Terminal
    pip uninstall -y pytest
  2. Switch the environment off
    Terminal
    deactivate
  1. Go to your project folder
    Terminal · Ubuntu / Debian
    cd my-project

    Use your own folder name.

  2. Create a virtual environment
    Terminal · Ubuntu / Debian
    python3 -m venv .venv

    Skip this if your project already has one. On Debian and Ubuntu you may need sudo apt install python3-venv first.

  3. Switch it on
    Terminal · Ubuntu / Debian
    source .venv/bin/activate

    Your prompt now starts with (.venv).

  4. Install pytest
    Terminal · Ubuntu / Debian
    pip install pytest
  5. Run all your tests
    Terminal · Ubuntu / Debian
    pytest

    pytest finds files named test_*.py or *_test.py and runs every function whose name starts with test.

  6. Run the tests in one file
    Terminal · Ubuntu / Debian
    pytest tests/test_login.py
  7. Run only tests whose name matches
    Terminal · Ubuntu / Debian
    pytest -k "test_login"

    The -k option picks tests by name. Any test with that text in its name runs.

  8. Stop at the first failure
    Terminal · Ubuntu / Debian
    pytest -x

    Handy when many tests fail for the same reason.

  9. Show each test's name and result
    Terminal · Ubuntu / Debian
    pytest -v

    You can mix options, for example pytest -x -v {{file}}.

Check it worked

  1. Check pytest is installed in the environment
    Terminal · Ubuntu / Debian
    pytest --version
Undo it
  1. Remove pytest
    Terminal · Ubuntu / Debian
    pip uninstall -y pytest
  2. Switch the environment off
    Terminal · Ubuntu / Debian
    deactivate
  1. Go to your project folder
    PowerShell
    cd my-project

    Use your own folder name.

  2. Create a virtual environment
    PowerShell
    py -m venv .venv

    Skip this if your project already has one.

  3. Switch it on
    PowerShell
    .venv\Scripts\Activate.ps1

    Your prompt now starts with (.venv). If PowerShell says scripts are disabled, see the virtual environment page.

  4. Install pytest
    PowerShell
    pip install pytest
  5. Run all your tests
    PowerShell
    pytest

    pytest finds files named test_*.py or *_test.py and runs every function whose name starts with test.

  6. Run the tests in one file
    PowerShell
    pytest tests/test_login.py

    Forward slashes work fine in the path on Windows too.

  7. Run only tests whose name matches
    PowerShell
    pytest -k "test_login"

    The -k option picks tests by name. Any test with that text in its name runs.

  8. Stop at the first failure
    PowerShell
    pytest -x

    Handy when many tests fail for the same reason.

  9. Show each test's name and result
    PowerShell
    pytest -v

    You can mix options, for example pytest -x -v {{file}}.

Check it worked

  1. Check pytest is installed in the environment
    PowerShell
    pytest --version
Undo it
  1. Remove pytest
    PowerShell
    pip uninstall -y pytest
  2. Switch the environment off
    PowerShell
    deactivate