Installing pytest

We recommend you install pytest and pytest-cache. pytest is a testing tool that will give you more flexibility over running your unit tests.

If you want to install pytest for Python 2, then use pip.

1
pip install pytest pytest-cache

If you instead want the version of pytest for Python 3, then use pip3.

1
pip3 install pytest pytest-cache

If you get a command not found response from your system, you can find a tutorial on how to install pip here.

Note: Whichever version of pytest you install last will be the default one used whenever pytest is executed, regardless of whether you have installed both versions.

If you want to check what the default version of pytest being used is, run the following:

1
pytest --version

If you have either version of pytest installed and you want to specifically run one of the versions, you can run that version by using python with the -m flag.

For example, you could run the Python 3 version of pytest like so:

1
2
$ python3 -m pytest --version
This is pytest version 3.2.3, imported from /usr/local/lib/python3.5/dist-packages/pytest.py

If you choose not to install pytest, you can still run tests individually and skip the rest of this tutorial:

1
2
cd exercism/python/bob
python bob_test.py

Running the Tests

Run All Tests

To run all tests for a specific exercise (we will take the bob.py exercise as an example here), place yourself in the directory where that exercise has been fetched and run:

1
pytest bob_test.py

Note: To run the tests you need to pass the name of the testsuite file to pytest (generally, the file ending with _test.py), NOT the file you created to solve the problem (which is your implementation). This is because in the latter case, since there are no defined test cases in your implementation, pytest will just return a positive result, specifying that it ran zero tests. Like this:

1
2
3
4
5
6
7
=============================  bob.py  ==============================

---------------------------------------------------------------------

Ran 0 tests in 0.000s

OK

More pytest Examples

Stop After First Failure

The above will run all the tests, whether they fail or not. If you'd rather stop the process and exit on the first failure, run:

1
pytest -x bob_test.py

Failed Tests First

pytest-cache remembers which tests failed, and can run those tests first.

1
pytest --ff bob_test.py

Running All Tests for All Exercises

1
2
cd exercism/python/
pytest

We recommend you run this command while working on exercises.

1
2
cd exercism/python/bob
pytest -x --ff bob_test.py

PDB

Will drop you into the python debugger when a test fails. To learn how to use pdb, check out the documentation.

1
pytest --pdb bob_test.py

You may also be interested in watching Clayton Parker's "So you think you can pdb?" PyCon 2015 talk.

PEP8

PEP8 is the Style Guide for Python Code. If you would like to test for compliance to the style guide, install pytest-pep8.

1
pip install pytest-pep8

Then, just add the --pep8 flag to your command

1
pytest --pep8 bob_test.py

Read the pytest documentation and pytest-cache documentation to learn more.


Help us explain this better! File a GitHub issue at https://github.com/exercism/python/issues if you have suggestions, or submit a patch with improvements to the https://github.com/exercism/python/blob/master/docs/TESTS.md file.