home-assistant.io/source/developers/development_testing.markdown
2016-08-25 22:05:31 +02:00

2.7 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. Every Pull Request is automatically tested with Coveralls and Travis CI after it is created.

{% linkable_title Local testing %}

It's highly recommanded to run tox before you create your Pull Request to avoid annoying fixes. Local testing requires tox to be installed.

$ pip3 install tox

Start the test of your code with tox.

$ tox

This will run unit tests against python 3.4 and 3.5 (if both are available locally), as well as run a set of tests which validate pep8 and pylint style of the code.

{% linkable_title Testing Tips %}

You can optionally run tests on only one tox target using the -e option to select an environment. For instance tox -e lint will run the linters only, tox -e py34 will run 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 date when requirements change causing test errors. Run tox -r to create new Tox virtual environments.

During development on a specific file, it can speed up your workflow to just run tests and linting related to 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 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 Prevent Linter Errors %}

You can save yourself the hassle of extra commits just to fix style errors by enabling the flake8 git commit hook. It will check your code when you attempt to commit to the repository. It will block the commit if there are any style issues, giving you a chance to fix it.

$ 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 %}

In case a PyLint warning cannot be avoided, add a comment to disable the PyLint check for that line. This can be done using the format # pylint: disable=YOUR-ERROR-NAME. Example of an unavoidable PyLint warning is if you do not use the passed in datetime if you're listening for time change.