| 1 |
Tests |
| 2 |
===== |
| 3 |
|
| 4 |
|
| 5 |
Quick Start |
| 6 |
----------- |
| 7 |
|
| 8 |
To add a simple unit test for a new feature you developed, open or create a |
| 9 |
`test-data/unit/check-*.test` file with a name that roughly relates to the |
| 10 |
feature you added. If you added a new `check-*.test` file, it will be autodiscovered during unittests run. |
| 11 |
|
| 12 |
Add the test in this format anywhere in the file: |
| 13 |
|
| 14 |
[case testNewSyntaxBasics] |
| 15 |
# flags: --python-version 3.10 |
| 16 |
x: int |
| 17 |
x = 5 |
| 18 |
y: int = 5 |
| 19 |
|
| 20 |
a: str |
| 21 |
a = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 22 |
b: str = 5 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 23 |
|
| 24 |
zzz: int |
| 25 |
zzz: str # E: Name "zzz" already defined |
| 26 |
|
| 27 |
- no code here is executed, just type checked |
| 28 |
- optional `# flags: ` indicates which flags to use for this unit test |
| 29 |
- `# E: abc...` indicates that this line should result in type check error |
| 30 |
with text "abc..." |
| 31 |
- note a space after `E:` and `flags:` |
| 32 |
- `# E:12` adds column number to the expected error |
| 33 |
- use `\` to escape the `#` character and indicate that the rest of the line is part of |
| 34 |
the error message |
| 35 |
- repeating `# E: ` several times in one line indicates multiple expected errors in one line |
| 36 |
- `W: ...` and `N: ...` works exactly like `E: ...`, but report a warning and a note respectively |
| 37 |
- lines that don't contain the above should cause no type check errors |
| 38 |
- optional `[builtins fixtures/...]` tells the type checker to use |
| 39 |
`builtins` stubs from the indicated file (see Fixtures section below) |
| 40 |
- optional `[out]` is an alternative to the `# E: ` notation: it indicates that |
| 41 |
any text after it contains the expected type checking error messages. |
| 42 |
Usually, `# E: ` is preferred because it makes it easier to associate the |
| 43 |
errors with the code generating them at a glance, and to change the code of |
| 44 |
the test without having to change line numbers in `[out]` |
| 45 |
- an empty `[out]` section has no effect |
| 46 |
- to add tests for a feature that hasn't been implemented yet, append `-xfail` |
| 47 |
to the end of the test name |
| 48 |
- to run just this test, use `pytest -n0 -k testNewSyntaxBasics` |
| 49 |
|
| 50 |
|
| 51 |
Fixtures |
| 52 |
-------- |
| 53 |
|
| 54 |
The unit tests use minimal stubs for builtins, so a lot of operations are not |
| 55 |
possible. You should generally define any needed classes within the test case |
| 56 |
instead of relying on builtins, though clearly this is not always an option |
| 57 |
(see below for more about stubs in test cases). This way tests run much |
| 58 |
faster and don't break if the stubs change. If your test crashes mysteriously |
| 59 |
even though the code works when run manually, you should make sure you have |
| 60 |
all the stubs you need for your test case, including built-in classes such as |
| 61 |
`list` or `dict`, as these are not included by default. |
| 62 |
|
| 63 |
Where the stubs for builtins come from for a given test: |
| 64 |
|
| 65 |
- The builtins used by default in unit tests live in |
| 66 |
`test-data/unit/lib-stub`. |
| 67 |
|
| 68 |
- Individual test cases can override the `builtins` stubs by using |
| 69 |
`[builtins fixtures/foo.pyi]`; this targets files in `test-data/unit/fixtures`. |
| 70 |
Feel free to modify existing files there or create new ones as you deem fit. |
| 71 |
|
| 72 |
- Test cases can also use `[typing fixtures/typing-full.pyi]` to use a more |
| 73 |
complete stub for `typing` that contains the async types, among other things. |
| 74 |
|
| 75 |
- Feel free to add additional stubs to that `fixtures` directory, but |
| 76 |
generally don't expand files in `lib-stub` without first discussing the |
| 77 |
addition with other mypy developers, as additions could slow down the test |
| 78 |
suite. |
| 79 |
|
| 80 |
- Some tests choose to customize the standard library in a way that's local to the test: |
| 81 |
``` |
| 82 |
[case testFoo] |
| 83 |
... |
| 84 |
[file builtins.py] |
| 85 |
class int: |
| 86 |
def next_fibonacci() -> int: pass |
| 87 |
``` |
| 88 |
Another possible syntax is: |
| 89 |
``` |
| 90 |
[fixture builtins.py] |
| 91 |
``` |
| 92 |
Whether you use `[file ...]` or `[fixture ...]` depends on whether you want |
| 93 |
the file to be part of the tested corpus (e.g. contribute to `[out]` section) |
| 94 |
or only support the test. |
| 95 |
|
| 96 |
Running tests and linting |
| 97 |
------------------------- |
| 98 |
|
| 99 |
First install any additional dependencies needed for testing: |
| 100 |
|
| 101 |
python3 -m pip install -U -r test-requirements.txt |
| 102 |
|
| 103 |
Configure `pre-commit` to run the linters automatically when you commit: |
| 104 |
|
| 105 |
pre-commit install |
| 106 |
|
| 107 |
The unit test suites are driven by the `pytest` framework. To run all mypy tests, |
| 108 |
run `pytest` in the mypy repository: |
| 109 |
|
| 110 |
pytest -q mypy |
| 111 |
|
| 112 |
This will run all tests, including integration and regression tests, |
| 113 |
and will verify that all stubs are valid. This may take several |
| 114 |
minutes to run, so you don't want to use this all the time while doing |
| 115 |
development. (The `-q` option activates less verbose output that looks |
| 116 |
better when running tests using many CPU cores.) |
| 117 |
|
| 118 |
Test suites for individual components are in the files `mypy/test/test*.py`. |
| 119 |
|
| 120 |
Note that some tests will be disabled for older python versions. |
| 121 |
|
| 122 |
If you work on mypyc, you will want to also run mypyc tests: |
| 123 |
|
| 124 |
pytest -q mypyc |
| 125 |
|
| 126 |
You can run tests from a specific module directly, a specific suite within a |
| 127 |
module, or a test in a suite (even if it's data-driven): |
| 128 |
|
| 129 |
pytest -q mypy/test/testdiff.py |
| 130 |
|
| 131 |
pytest -q mypy/test/testsemanal.py::SemAnalTypeInfoSuite |
| 132 |
|
| 133 |
pytest -n0 mypy/test/testargs.py::ArgSuite::test_coherence |
| 134 |
|
| 135 |
pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::testCallingVariableWithFunctionType |
| 136 |
|
| 137 |
To control which tests are run and how, you can use the `-k` switch: |
| 138 |
|
| 139 |
pytest -q -k "MethodCall" |
| 140 |
|
| 141 |
You can also run the type checker for manual testing without |
| 142 |
installing it by setting up the Python module search path suitably: |
| 143 |
|
| 144 |
export PYTHONPATH=$PWD |
| 145 |
python3 -m mypy PROGRAM.py |
| 146 |
|
| 147 |
You will have to manually install the `typing` module if you're running Python |
| 148 |
3.4 or earlier. |
| 149 |
|
| 150 |
You can also execute mypy as a module |
| 151 |
|
| 152 |
python3 -m mypy PROGRAM.py |
| 153 |
|
| 154 |
You can check a module or string instead of a file: |
| 155 |
|
| 156 |
python3 -m mypy PROGRAM.py |
| 157 |
python3 -m mypy -m MODULE |
| 158 |
python3 -m mypy -c 'import MODULE' |
| 159 |
|
| 160 |
To run mypy on itself: |
| 161 |
|
| 162 |
python3 -m mypy --config-file mypy_self_check.ini -p mypy |
| 163 |
|
| 164 |
To run the linter (this commands just wraps `pre-commit`, so you can also |
| 165 |
invoke it directly like `pre-commit run -a`, and this will also run when you |
| 166 |
`git commit` if enabled): |
| 167 |
|
| 168 |
python3 runtests.py lint |
| 169 |
|
| 170 |
You can also run all of the above tests using `runtests.py` (this includes |
| 171 |
type checking mypy and linting): |
| 172 |
|
| 173 |
python3 runtests.py |
| 174 |
|
| 175 |
By default, this runs everything except some mypyc tests. You can give it |
| 176 |
arguments to control what gets run, such as `self` to run mypy on itself: |
| 177 |
|
| 178 |
python3 runtests.py self |
| 179 |
|
| 180 |
Run `python3 runtests.py mypyc-extra` to run mypyc tests that are not |
| 181 |
enabled by default. This is typically only needed if you work on mypyc. |
| 182 |
|
| 183 |
Many test suites store test case descriptions in text files |
| 184 |
(`test-data/unit/*.test`). The module `mypy.test.data` parses these |
| 185 |
descriptions. |
| 186 |
|
| 187 |
Python evaluation test cases are a little different from unit tests |
| 188 |
(`mypy/test/testpythoneval.py`, `test-data/unit/pythoneval.test`). These |
| 189 |
type check programs and run them. Unlike the unit tests, these use the |
| 190 |
full builtins and library stubs instead of minimal ones. Run them using |
| 191 |
`pytest -k testpythoneval`. |
| 192 |
|
| 193 |
`pytest` determines the number of processes to use. The default (set in |
| 194 |
`./pytest.ini`) is the number of logical cores; this can be overridden using |
| 195 |
`-n` option. To run a single process, use `pytest -n0`. |
| 196 |
|
| 197 |
Note that running more processes than logical cores is likely to |
| 198 |
significantly decrease performance. |
| 199 |
|
| 200 |
To run tests with coverage: |
| 201 |
|
| 202 |
python3 -m pytest --cov mypy --cov-config setup.cfg --cov-report=term-missing:skip-covered --cov-report=html |
| 203 |
|
| 204 |
|
| 205 |
Debugging |
| 206 |
--------- |
| 207 |
|
| 208 |
You can use interactive debuggers like `pdb` to debug failing tests. You |
| 209 |
need to pass the `-n0` option to disable parallelization: |
| 210 |
|
| 211 |
pytest -n0 --pdb -k MethodCall |
| 212 |
|
| 213 |
You can also write `import pdb; pdb.set_trace()` in code to enter the |
| 214 |
debugger. |
| 215 |
|
| 216 |
The `--mypy-verbose` flag can be used to enable additional debug output from |
| 217 |
most tests (as if `--verbose` had been passed to mypy): |
| 218 |
|
| 219 |
pytest -n0 --mypy-verbose -k MethodCall |
| 220 |
|
| 221 |
Coverage reports |
| 222 |
---------------- |
| 223 |
|
| 224 |
There is an experimental feature to generate coverage reports. To use |
| 225 |
this feature, you need to `pip install -U lxml`. This is an extension |
| 226 |
module and requires various library headers to install; on a |
| 227 |
Debian-derived system the command |
| 228 |
`apt-get install python3-dev libxml2-dev libxslt1-dev` |
| 229 |
may provide the necessary dependencies. |
| 230 |
|
| 231 |
To use the feature, pass e.g. `--txt-report "$(mktemp -d)"`. |