|
1 | 1 | # Installing `pytest` |
2 | 2 |
|
3 | | -First of all, install `pytest` through `pip`. |
| 3 | +We recommend you install [pytest](http://pytest.org/latest/) and |
| 4 | +[pytest-cache](http://pythonhosted.org/pytest-cache/). `pytest` is a testing |
| 5 | +tool that will give you more flexibility over running your unit tests. |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install |
| 9 | +pytest pytest-cache |
4 | 10 | ``` |
5 | | -pip install pytest |
| 11 | + |
| 12 | +If you get a `command not found` response from your system, you can find a |
| 13 | +tutorial on how to install `pip` |
| 14 | +[here](https://pip.pypa.io/en/stable/installing/). |
| 15 | + |
| 16 | +If you choose not to install `pytest`, you can still run tests individually and |
| 17 | +skip the rest of this tutorial: |
| 18 | + |
| 19 | +```bash |
| 20 | +cd exercism/python/bob |
| 21 | +python bob_test.py |
6 | 22 | ``` |
7 | | -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/). |
8 | 23 |
|
9 | 24 | # Running the Tests |
10 | 25 |
|
11 | | -To run the 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: |
| 26 | +## Run All Tests |
12 | 27 |
|
13 | | -``` |
| 28 | +To run all tests for a specific exercise (we will take the `bob.py` exercise as |
| 29 | +an example here), place yourself in the directory where that exercise has been |
| 30 | +fetched and run: |
| 31 | + |
| 32 | +```bash |
14 | 33 | py.test bob_test.py |
15 | 34 | ``` |
16 | 35 |
|
17 | | -This will run all the tests, whether they fail or not. If you'd rather stop the process and exit on the first failure, run: |
| 36 | +**Note:** To run the tests you need to pass the name of the testsuite file to |
| 37 | +`pytest` (generally, the file ending with `_test.py`), **NOT** the file you |
| 38 | +created to solve the problem (which is your _implementation_). This is because |
| 39 | +in the latter case, since there are no defined test cases in your |
| 40 | +implementation, `pytest` will just return a positive result, specifying that |
| 41 | +it ran zero tests. Like this: |
| 42 | + |
| 43 | +``` |
| 44 | +============================= bob.py ============================== |
| 45 | +
|
| 46 | +--------------------------------------------------------------------- |
| 47 | +
|
| 48 | +Ran 0 tests in 0.000s |
18 | 49 |
|
| 50 | +OK |
19 | 51 | ``` |
| 52 | + |
| 53 | +## More `pytest` Examples |
| 54 | + |
| 55 | +### Stop After First Failure |
| 56 | +The above will run all the tests, whether they fail or not. If you'd rather stop |
| 57 | +the process and exit on the first failure, run: |
| 58 | + |
| 59 | +```bash |
20 | 60 | py.test -x bob_test.py |
21 | 61 | ``` |
22 | 62 |
|
23 | | -**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: |
| 63 | +### Failed Tests First |
24 | 64 |
|
| 65 | +`pytest-cache` remembers which tests failed, and can run those tests first. |
| 66 | + |
| 67 | +```bash |
| 68 | +py.test --ff bob_test.py |
25 | 69 | ``` |
26 | | -============================= bob.py ============================== |
27 | 70 |
|
28 | | ----------------------------------------------------------------------- |
| 71 | +### Running All Tests for All Exercises |
29 | 72 |
|
30 | | -Ran 0 tests in 0.000s |
| 73 | +```bash |
| 74 | +cd exercism/python/ py.test |
| 75 | +``` |
31 | 76 |
|
32 | | -OK |
| 77 | +## Recommended Workflow |
| 78 | + |
| 79 | +We recommend you run this command while working on exercises. |
| 80 | + |
| 81 | +```bash |
| 82 | +cd exercism/python/bob py.test -x --ff bob_test.py |
| 83 | +``` |
| 84 | + |
| 85 | +## PDB |
| 86 | + |
| 87 | +Will drop you into the python debugger when a test fails. To learn how to use |
| 88 | +pdb, check out the |
| 89 | +[documentation](https://docs.python.org/3/library/pdb.html#debugger-commands). |
| 90 | + |
| 91 | +You may also be interested in watching [Clayton Parker's "So you think you can |
| 92 | +pdb?" PyCon 2015 talk](https://www.youtube.com/watch?v=P0pIW5tJrRM) |
| 93 | + |
| 94 | +```bash |
| 95 | +py.test --pdb bob_test.py |
| 96 | +``` |
| 97 | + |
| 98 | +## PEP8 |
| 99 | + |
| 100 | +PEP8 is the [Style Guide for Python |
| 101 | +Code](https://www.python.org/dev/peps/pep-0008/). If you would like to test for |
| 102 | +compliance to the style guide, install |
| 103 | +[pytest-pep8](https://pypi.python.org/pypi/pytest-pep8) |
| 104 | + |
| 105 | +```bash |
| 106 | +pip install pytest-pep8 |
33 | 107 | ``` |
| 108 | + |
| 109 | +and add the pep8 flag to your command |
| 110 | + |
| 111 | +```bash |
| 112 | +py.test --pep8 bob_test.py |
| 113 | +``` |
| 114 | + |
| 115 | +Read the [pytest documentation](http://pytest.org/latest/contents.html#toc) and |
| 116 | +[pytest-cache](http://pythonhosted.org/pytest-cache/) documentation to learn |
| 117 | +more. |
0 commit comments