home-assistant.io/source/developers/development_testing.markdown
2016-10-04 22:04:18 +02:00

2.6 KiB

layout, title, description, date, sidebar, comments, sharing, footer
layout title description date sidebar comments sharing footer
page Testing your code Make sure that your code passes the checks 2016-07-01 20:00 true false true true

Home Assistant enforces strict PEP8 style compliance on all code submitted. We automatically test every pull request with Coveralls and Travis CI.

{% linkable_title Local testing %}

Important: Run tox before you create your pull request to avoid annoying fixes. Local testing requires installing tox.

$ pip3 install tox

Start your code test with tox.

$ tox

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.

{% linkable_title Testing Tips %}

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.

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.

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:

$ flake8 homeassistant/core.py
$ pylint homeassistant/core.py
$ py.test tests/test_core.py

You can also run linting tests against all changed files, as reported by git diff upstream/dev --name-only, using the lint script:

home-assistant$ script/lint --changed

{% 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!

$ pip3 install flake8 flake8-docstrings
$ flake8 --install-hook=git

The flake8-docstrings extension will check docstrings according to PEP257 when running Flake8.

{% 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.