Testing Your Code

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

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.

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
$ pydocstyle 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:

$ script/lint --changed

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.

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.

Maximum Line Length

As part of the linting process, all code is checked for a maximum line length of 79 characters. This comes directly from the PEP8 style guide, and is also used by the Python standard library. All code must pass these linting checks, and no exceptions will be made. There have already been numerous requests to increase the maximum line length, but after evaluating the options, the Home Assistant maintainers have decided to stay at 79 characters. This decision is final.