Update the testing documentation

This commit is contained in:
Paulus Schoutsen 2017-05-13 21:24:45 -07:00
parent 94957caacb
commit 8f36696795

View File

@ -9,31 +9,28 @@ sharing: true
footer: true
---
As states in the [Style guidelines section](/developers/development_guidelines/) all code is checked as part of the linting process and unit test were run.
### {% linkable_title Local testing %}
**Important:** Run `tox` before you create your pull request to avoid annoying fixes. Local testing requires installing `tox`.
```bash
$ pip3 install tox
```
Start your code test with `tox`.
As states in the [Style guidelines section](/developers/development_guidelines/) all code is checked to verify all unit tests pass and that the code passes the linting tools. Local testing is done using Tox, which has been installed as part of running `script/setup`. To start the tests, simply run it:
```bash
$ tox
```
**Important:** Run `tox` before you create your pull request to avoid annoying fixes.
This will run unit tests against Python 3.4 and 3.5 (if both are available locally), as well as tests that validate `pep8` and `pylint` style.
Running Tox will run unit tests against the locally available Pythons, as well as validate the code and document style using `pycodestyle`, `pydocstyle` and `pylint`. You can run tests on only one tox target -- just use `-e` to select an environment. For example, `tox -e lint` runs the linters only, and `tox -e py34` runs unit tests only on Python 3.4.
#### {% linkable_title Testing Tips %}
Tox uses virtual environments under the hood to create isolated testing environments. The tox virtual environments will get out-of-date when requirements change, causing test errors. Run `tox -r` to tell Tox to recreate the virtual environments.
You can run tests on only one tox target -- just use `-e` to select an environment. For example, `tox -e lint` runs the linters only, and `tox -e py34` runs unit tests only on Python 3.4.
If you are working on tests for a component or platform and you need the dependencies available inside the Tox environment, update the list inside `script/gen_requirements_all.py`. Then run the script and then run `tox -r` to recreate the virtual environments.
tox uses virtual environments under the hood to create isolated testing environments. The tox virtual environments will get out-of-date when requirements change, causing test errors. Run `tox -r` to create new tox virtual environments.
### {% linkable_title Testing single files %}
During development on a specific file, speed up your workflow by running tests and linting only for the file that you're working on. To run individual files:
Running tox will invoke the full test suite. Even if you specify which tox target to run, you still run all tests inside that target. That's not very convenient to quickly iterate on your code! To be able to run the specific test suites without Tox, you'll need to install the test dependencies into your Python environment:
```bash
$ bash pip3 install -r requirements_test_all.txt
```
Now that you have all test dependencies installed, you can run tests on individual files:
```bash
$ flake8 homeassistant/core.py
@ -48,8 +45,23 @@ You can also run linting tests against all changed files, as reported by `git di
$ script/lint --changed
```
#### {% linkable_title Py.test tips %}
Py.test has some great command line parameters to help you with the write-test-fix cycle.
```bash
# Stop after the first test fails
$ py.test tests/test_core.py -x
# Run test with specified name
$ py.test tests/test_core.py -k test_split_entity_id
# Fail a test after it runs for 2 seconds
$ py.test tests/test_core.py --timeout 2
# Show the 10 slowest tests
$ py.test tests/test_core.py --duration=10
```
### {% linkable_title Preventing Linter Errors %}
Save yourself the hassle of extra commits just to fix style errors by enabling the Flake8 git commit hook. Flake8 will check your code when you try to commit to the repository and block the commit if there are any style errors, which gives you a chance to fix them!
```bash
@ -61,5 +73,4 @@ The `flake8-docstrings` extension will check docstrings according to [PEP257](ht
### {% linkable_title Notes on PyLint and PEP8 validation %}
If you can't avoid a PyLint warning, add a comment to disable the PyLint check for that line with `# pylint: disable=YOUR-ERROR-NAME`. An example of an unavoidable PyLint warning is not using the passed-in datetime if you're listening for a time change.
If you can't avoid a PyLint warning, add a comment to disable the PyLint check for that line with `# pylint: disable=YOUR-ERROR-NAME`. Example of an unavoidable one is if PyLint incorrectly reports that a certain object doesn't have a certain member.