Skip to content

Commit 1629f6f

Browse files
committed
Edited down to be less verbose.
1 parent 499ef45 commit 1629f6f

File tree

1 file changed

+35
-96
lines changed

1 file changed

+35
-96
lines changed

exercises/shared/.docs/tests.md

Lines changed: 35 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,67 @@
11
# Tests
22

3+
34
We use [pytest][pytest: Getting Started Guide] as our website test runner.
4-
You will need to install `pytest` on your development machine if you want to download and run exercise tests for the Python track locally.
5+
You will need to install `pytest` on your development machine if you want to run tests for the Python track locally.
56
You should also install the following `pytest` plugins:
67

78
- [pytest-cache][pytest-cache]
89
- [pytest-subtests][pytest-subtests]
910

10-
We also recommend using the code linting program [pylint][pylint], as it is part of our automated feedback on the website and can be a very useful static code analysis tool.
11-
For ease-of-use, the [pytest-pylint][pytest-pylint] plugin for `pytest` will allow you to run `pylint` via `pytest` on the command line.
12-
13-
Pylint configuration can be a bit much, so this [tutorial][tutorial from pycqa.org] from pycqa.org can be helpful for getting started, as can this overview of [Code Quality: Tools and Best Practices][Code Quality: Tools and Best Practices] from Real Python.
14-
15-
16-
## Installing pytest
17-
18-
Pytest can be installed and updated using the built-in Python utility [`pip`][pip].
19-
20-
For additional tips, Brett Cannon has a nice [quick-and-dirty guide on how to install packages for python][brett cannon: quick-and-dirty], along with a great explanation on [why you should use `python -m pip`][brett cannon: python-m-pip].
21-
For more on Python's command line arguments, see [command line and environment][python command line arguments] in the Python documentation.
11+
Extended information can be found in our website [Python testing guide][Python track tests page].
2212

23-
**Note:** `Python3` may or may not be an alias for Python on your system.
24-
Please adjust the install commands below accordingly.
25-
To install `pytest` in a virtual environment, ensure the environment **is activated** prior to executing commands.
26-
Otherwise, the pytest installation will be global.
2713

14+
### Running Tests
2815

29-
#### Windows
30-
31-
```powershell
32-
PS C:\Users\foobar> python3 -m pip install pytest pytest-cache pytest-subtests pytest-pylint
33-
Successfully installed pytest-6.2.5 ...
34-
```
35-
36-
#### Linux / MacOS
16+
To run the included tests, navigate to the folder where the exercise is stored using `cd` in your terminal (_replace `{exercise-folder-location}` below with your path_).
17+
Test files usually end in `_test.py`, and are the same tests that run on the website when a solution is uploaded.
3718

19+
Linux/MacOS
3820
```bash
39-
$ python3 -m pip install pytest pytest-cache pytest-subtests pytest-pylint
40-
Successfully installed pytest-6.2.5 ...
41-
21+
$ cd {path/to/exercise-folder-location}
4222
```
4323

44-
45-
To check if installation was successful:
46-
47-
```bash
48-
$ python3 -m pytest --version
49-
pytest 6.2.5
24+
Windows
25+
```powershell
26+
PS C:\Users\foobar> cd {path\to\exercise-folder-location}
5027
```
5128

52-
## Running the tests
29+
<br>
5330

54-
To run the tests, go to the folder where the exercise is stored using `cd` in your terminal (_replace `{exercise-folder-location}` below with your path_).
31+
Next, run the `pytest` command in your terminal, replacing `{exercise_test.py}` with the name of the test file:
5532

33+
Linux/MacOS
5634
```bash
57-
$ cd {exercise-folder-location}
35+
$ python3 -m pytest -o markers=task {exercise_test.py}
36+
==================== 7 passed in 0.08s ====================
5837
```
5938

60-
The file you will want to run usually ends in `_test.py`.
61-
This file contains the tests for the exercise solution, and are the same tests that run on the website when a solution is uploaded.
62-
Next, run the following command in your terminal, replacing `{exercise_test.py}` with the location/name of the test file:
63-
64-
```bash
65-
$ python3 -m pytest -o markers=task {exercise_test.py}
39+
Windows
40+
```pwowershell
41+
PS C:\Users\foobar> py -m pytest -o markers=task {exercise_test.py}
6642
==================== 7 passed in 0.08s ====================
6743
```
6844

69-
### Common pytest options
7045

71-
- `-o` : override default `ini` (_you can use this to avoid marker warnings_)
46+
### Common options
47+
- `-o` : override default `pytest.ini` (_you can use this to avoid marker warnings_)
7248
- `-v` : enable verbose output.
7349
- `-x` : stop running tests on first failure.
7450
- `--ff` : run failures from previous test before running other test cases.
7551

76-
For other options, see `python3 -m pytest -h`.
52+
For additional options, use `python3 -m pytest -h` or `py -m pytest -h`.
7753

7854

7955
### Fixing warnings
8056

81-
If you do not use the `pytest -o markers=task` in the command above, is possible that you will get `warnings` about "unknown markers" when running a test that uses our _new_ syntax.
57+
If you do not use `pytest -o markers=task` when invoking `pytest`, you might receive a `PytestUnknownMarkWarning` for tests that use our new syntax:
8258

83-
To avoid typing `pytest -o markers=task` for every test you run, you can use a `pytest.ini` configuration file, which can be downloaded from the top level of the Python track directory: [pytest.ini][pytest.ini].
59+
```bash
60+
PytestUnknownMarkWarning: Unknown pytest.mark.task - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
61+
```
62+
63+
To avoid typing `pytest -o markers=task` for every test you run, you can use a `pytest.ini` configuration file.
64+
We have made one that can be downloaded from the top level of the Python track directory: [pytest.ini][pytest.ini].
8465

8566
You can also create your own `pytest.ini` file with the following content:
8667

@@ -90,65 +71,23 @@ markers =
9071
task: A concept exercise task.
9172
```
9273

93-
Placing this file in the _root_ or _working_ directory for the Python track exercises will register the marks and stop the warnings.
74+
Placing the `pytest.ini` file in the _root_ or _working_ directory for your Python track exercises will register the marks and stop the warnings.
9475
More information on pytest marks can be found in the `pytest` documentation on [marking test functions][pytest: marking test functions with attributes] and the `pytest` documentation on [working with custom markers][pytest: working with custom markers].
9576

96-
_More information on customizing pytest configurations can be found in the pytest documentation on [configuration file formats][pytest: configuration file formats]_
97-
98-
99-
### Recommended Workflow
100-
101-
We recommend using the following commands to make your debugging easier and (possibly) faster:
102-
103-
First change your working directory to the directory of the exercise you want to test:
104-
105-
```bash
106-
$(my_venv) cd path/to/exercise
107-
```
108-
109-
Then, run the tests together with the previously explained arguments `-x` and`--ff`:
110-
111-
```bash
112-
$(my_venv) python3 -m pytest -o markers=task -x --ff <example_file_test.py>
113-
```
114-
115-
This will test your solution.
116-
When `pytest` encounters a failed test, the program will stop and tell you which test failed.
117-
When you make fixes and run the test again, `pytest` will first run the previous test that failed, then continue with the remaining tests.
118-
119-
120-
### Using PDB, the Python Debugger, with pytest
121-
122-
You can use the `--pdb` argument after the `pytest` command and drop into the built-in [Python debugger][pdb], `PDB` on test failure:
123-
124-
```bash
125-
$(my_venv) python3 -m pytest -o markers=task -x --ff --pdb <example_file_test.py>
126-
=============== 4 passed in 0.15s ===============
127-
```
77+
Information on customizing pytest configurations can be found in the `pytest` documentation on [configuration file formats][pytest: configuration file formats].
12878

129-
Dropping into `PDB` will allow you to step through your code viewing the current scope, as well as checking the value of variables and the signature of different functions.
130-
More details on the `PDB` module can be found in the [Python documentation on PDB][pdb].
131-
Additionally, the [pytest docs on PDB][pytest-pdb] and [this guide from Real Python](https://realpython.com/python-debugging-pdb/) are extremely helpful.
13279

133-
## Extending your IDE
80+
### Extending your IDE or Code Editor
13481

135-
If you'd like to extend your IDE with some tools that will help you with testing and improving your code, the Python track [tools page][Python track tools page] on exercism.org goes over some options.
82+
Many IDEs and code editors have built-in support for using `pytest` and other code quality tools.
83+
Some community-sourced options can be found on our [Python track tools page][Python track tools page].
13684

137-
[Code Quality: Tools and Best Practices]: https://realpython.com/python-code-quality/
13885
[Pytest: Getting Started Guide]: https://docs.pytest.org/en/latest/getting-started.html
13986
[Python track tools page]: https://exercism.org/docs/tracks/python/tools
140-
[brett cannon: python-m-pip]: https://snarky.ca/why-you-should-use-python-m-pip/
141-
[brett cannon: quick-and-dirty]: https://snarky.ca/a-quick-and-dirty-guide-on-how-to-install-packages-for-python/
142-
[pdb]: https://docs.python.org/3.9/library/pdb.html
143-
[pip]: https://pip.pypa.io/en/stable/getting-started/
144-
[pylint]: https://pylint.pycqa.org/en/latest/user_guide/
87+
[Python track tests page]: https://exercism.org/docs/tracks/python/tests
14588
[pytest-cache]:http://pythonhosted.org/pytest-cache/
146-
[pytest-pdb]: https://docs.pytest.org/en/6.2.x/usage.html#dropping-to-pdb-python-debugger-on-failures
147-
[pytest-pylint]:https://github.com/carsongee/pytest-pylint
14889
[pytest-subtests]:https://github.com/pytest-dev/pytest-subtests
14990
[pytest.ini]: https://github.com/exercism/python/blob/main/pytest.ini
15091
[pytest: configuration file formats]: https://docs.pytest.org/en/6.2.x/customize.html#configuration-file-formats
15192
[pytest: marking test functions with attributes]: https://docs.pytest.org/en/6.2.x/mark.html#raising-errors-on-unknown-marks
15293
[pytest: working with custom markers]: https://docs.pytest.org/en/6.2.x/example/markers.html#working-with-custom-markers
153-
[python command line arguments]: https://docs.python.org/3/using/cmdline.html
154-
[tutorial from pycqa.org]: https://pylint.pycqa.org/en/latest/tutorial.html

0 commit comments

Comments
 (0)