2021-02-12 15:08:36 +01:00
|
|
|
# Tests
|
|
|
|
|
|
2018-07-16 09:12:25 -04:00
|
|
|
## Installing `pytest`
|
2016-01-26 16:56:50 +01:00
|
|
|
|
2019-08-07 15:22:06 -04:00
|
|
|
We recommend you install [pytest](http://pytest.org/en/latest/) and
|
2016-03-25 18:31:00 -04:00
|
|
|
[pytest-cache](http://pythonhosted.org/pytest-cache/). `pytest` is a testing
|
|
|
|
|
tool that will give you more flexibility over running your unit tests.
|
|
|
|
|
|
2019-06-10 09:41:02 -03:00
|
|
|
To install `pytest`, run the following command:
|
2017-10-24 17:13:34 -04:00
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
pip3 install pytest pytest-cache
|
|
|
|
|
```
|
2016-03-25 18:31:00 -04:00
|
|
|
|
|
|
|
|
If you get a `command not found` response from your system, you can find a
|
|
|
|
|
tutorial on how to install `pip`
|
|
|
|
|
[here](https://pip.pypa.io/en/stable/installing/).
|
|
|
|
|
|
2017-10-24 17:13:34 -04:00
|
|
|
If you want to check what the default version of `pytest` being used is, run the following:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
pytest --version
|
|
|
|
|
```
|
|
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
If you choose not to install `pytest`, you can still run tests individually and
|
|
|
|
|
skip the rest of this tutorial:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
cd exercism/python/bob
|
|
|
|
|
python bob_test.py
|
2016-01-26 16:56:50 +01:00
|
|
|
```
|
|
|
|
|
|
2018-07-16 09:12:25 -04:00
|
|
|
## Running the Tests
|
2016-01-26 16:56:50 +01:00
|
|
|
|
2018-07-16 09:12:25 -04:00
|
|
|
### Run All Tests
|
2016-01-26 16:56:50 +01:00
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```bash
|
2017-10-24 17:13:34 -04:00
|
|
|
pytest bob_test.py
|
2016-01-26 16:56:50 +01:00
|
|
|
```
|
|
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
**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:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
============================= bob.py ==============================
|
|
|
|
|
|
|
|
|
|
---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Ran 0 tests in 0.000s
|
2016-01-26 16:56:50 +01:00
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
OK
|
2016-01-26 16:56:50 +01:00
|
|
|
```
|
2016-03-25 18:31:00 -04:00
|
|
|
|
2018-07-16 09:12:25 -04:00
|
|
|
### More `pytest` Examples
|
2016-03-25 18:31:00 -04:00
|
|
|
|
2018-07-16 09:12:25 -04:00
|
|
|
#### Stop After First Failure
|
2016-03-25 18:31:00 -04:00
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
```bash
|
2017-10-24 17:13:34 -04:00
|
|
|
pytest -x bob_test.py
|
2016-01-26 16:56:50 +01:00
|
|
|
```
|
|
|
|
|
|
2018-07-16 09:12:25 -04:00
|
|
|
#### Failed Tests First
|
2016-01-26 16:56:50 +01:00
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
`pytest-cache` remembers which tests failed, and can run those tests first.
|
|
|
|
|
|
|
|
|
|
```bash
|
2017-10-24 17:13:34 -04:00
|
|
|
pytest --ff bob_test.py
|
2016-01-26 16:56:50 +01:00
|
|
|
```
|
|
|
|
|
|
2018-07-16 09:12:25 -04:00
|
|
|
#### Running All Tests for All Exercises
|
2016-01-26 16:56:50 +01:00
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
```bash
|
2016-05-08 20:48:29 +04:00
|
|
|
cd exercism/python/
|
2017-10-24 17:13:34 -04:00
|
|
|
pytest
|
2016-03-25 18:31:00 -04:00
|
|
|
```
|
2016-01-26 16:56:50 +01:00
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
## Recommended Workflow
|
|
|
|
|
|
|
|
|
|
We recommend you run this command while working on exercises.
|
|
|
|
|
|
|
|
|
|
```bash
|
2016-05-08 20:48:29 +04:00
|
|
|
cd exercism/python/bob
|
2017-10-24 17:13:34 -04:00
|
|
|
pytest -x --ff bob_test.py
|
2016-03-25 18:31:00 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## PDB
|
|
|
|
|
|
|
|
|
|
Will drop you into the python debugger when a test fails. To learn how to use
|
|
|
|
|
pdb, check out the
|
|
|
|
|
[documentation](https://docs.python.org/3/library/pdb.html#debugger-commands).
|
|
|
|
|
|
|
|
|
|
```bash
|
2017-10-24 17:13:34 -04:00
|
|
|
pytest --pdb bob_test.py
|
2016-03-25 18:31:00 -04:00
|
|
|
```
|
|
|
|
|
|
2017-10-24 17:13:34 -04:00
|
|
|
You may also be interested in watching [Clayton Parker's "So you think you can
|
|
|
|
|
pdb?" PyCon 2015 talk](https://www.youtube.com/watch?v=P0pIW5tJrRM).
|
|
|
|
|
|
2016-03-25 18:31:00 -04:00
|
|
|
## PEP8
|
|
|
|
|
|
|
|
|
|
PEP8 is the [Style Guide for Python
|
|
|
|
|
Code](https://www.python.org/dev/peps/pep-0008/). If you would like to test for
|
|
|
|
|
compliance to the style guide, install
|
2017-10-24 17:13:34 -04:00
|
|
|
[pytest-pep8](https://pypi.python.org/pypi/pytest-pep8).
|
2016-03-25 18:31:00 -04:00
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
pip install pytest-pep8
|
2016-01-26 16:56:50 +01:00
|
|
|
```
|
2016-03-25 18:31:00 -04:00
|
|
|
|
2017-10-24 17:13:34 -04:00
|
|
|
Then, just add the `--pep8` flag to your command
|
2016-03-25 18:31:00 -04:00
|
|
|
|
|
|
|
|
```bash
|
2017-10-24 17:13:34 -04:00
|
|
|
pytest --pep8 bob_test.py
|
2016-03-25 18:31:00 -04:00
|
|
|
```
|
|
|
|
|
|
2020-02-26 01:45:33 -08:00
|
|
|
Read the [pytest documentation](https://docs.pytest.org/en/latest/contents.html) and
|
2016-03-25 18:31:00 -04:00
|
|
|
[pytest-cache](http://pythonhosted.org/pytest-cache/) documentation to learn
|
|
|
|
|
more.
|