Learn how to test your Python exercises on Exercism
We recommend you install pytest with the following plugins:
The PyTest Getting Started Guide has quick general instructions, although they do not cover installing the plugins. Continue reading below for plugin installation.
We also recommend pylint, as it is part of our automated feedback on the website, and can be a very useful (if noisy!) code analysis tool.
Finally, this site is a great place to look up more human-readable descriptions of Pylint linting messages.
pytest, run the following command in the environment (active
vm or other project space with Python 3.8 installed):
pip3 install pytest pytest-cache pytest-subtests pytest-pylint
If you get a
command not found response from your system, you can find a
tutorial on how to install
Once the installation has completed, you can check what the default version of
pytest is by running the following:
To run all tests for a specific exercise (we will take the
bob.py exercise as
an example here), navigate to the directory where that exercise has been
downloaded and run the following in the terminal:
Note: To run the tests you need to pass the name of the testsuite file to
pytest (generally, this will be the file ending with
_test.py), NOT the file that was
created to solve the exercsim problem (which is the solution implementation).
PyTest needs the test definitions in the
_test.py file in order to know how to call, run, and exercise the solution implementation code.
Since there are no test cases defined in the solution implementation,
pytest will just return a positive result, specifying that it found and ran zero tests. Like this:
============================= bob.py ============================== --------------------------------------------------------------------- Ran 0 tests in 0.000s OK
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:
pytest -x bob_test.py
pytest-cache remembers which tests failed, and can run those tests first.
pytest --ff bob_test.py
cd exercism/python/ pytest
Try this command while working on exercises:
cd exercism/python/bob pytest -x --ff bob_test.py
Typing pdb on the command line will drop you into the python debugger when a test fails. To learn how to usepdb, check out the documentation.
pytest --pdb bob_test.py