Files
.github
.themes
_deploy
credits_generator
plugins
sass
source
_addons
_components
_cookbook
_data
_docs
_faq
_includes
_layouts
_posts
addons
assets
blog
components
cookbook
demo
developers
documentation
hassio
intent
add_new_platform.markdown
api.markdown
architecture.markdown
architecture_components.markdown
asyncio.markdown
asyncio_101.markdown
asyncio_categorizing_functions.markdown
asyncio_misc.markdown
asyncio_working_with_async.markdown
cla.markdown
cla_sign.html
cla_sign_start.html
code_of_conduct.markdown
code_review_component.markdown
code_review_platform.markdown
component_deps_and_reqs.markdown
component_discovery.markdown
component_events.markdown
component_generic_discovery.markdown
component_loading.markdown
component_states.markdown
component_visibility.markdown
creating_components.markdown
credits.markdown
development.markdown
development_101.markdown
development_catching_up.markdown
development_checklist.markdown
development_config.markdown
development_environment.markdown
development_events.markdown
development_guidelines.markdown
development_hass_object.markdown
development_services.markdown
development_states.markdown
development_submitting.markdown
development_testing.markdown
development_validation.markdown
frontend.markdown
frontend_add_card.markdown
frontend_add_more_info.markdown
frontend_creating_custom_panels.markdown
frontend_creating_custom_ui.markdown
helpers.markdown
index.markdown
license.markdown
maintenance.markdown
multiple_instances.markdown
platform_example_light.markdown
platform_example_sensor.markdown
python_api.markdown
releasing.markdown
rest_api.markdown
server_sent_events.markdown
websocket_api.markdown
docs
faq
font
getting-started
hassio
help
images
javascripts
static
CNAME
atom.xml
favicon.png
googlef4f3693c209fe788.html
index.html
robots.txt
service_worker.js
version.json
.editorconfig
.gitattributes
.gitignore
.gitmodules
.powrc
.project
.ruby-version
.slugignore
.travis.yml
CLA.md
CODE_OF_CONDUCT.md
Gemfile
Gemfile.lock
LICENSE.md
README.markdown
Rakefile
_config.yml
config.rb
config.ru
home-assistant.io/source/developers/development_testing.markdown
2017-05-23 09:39:37 -07:00

3.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

As states in the Style guidelines section 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:

$ tox

Important: Run tox before you create your pull request to avoid annoying fixes.

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.

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.

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.

{% linkable_title Running single tests using Tox %}

You can pass arguments via Tox to py.test to be able to run single test suites or test files. Replace py36 with the Python version that you use.

# Stop after the first test fails
$ tox -e py36 -- tests/test_core.py -x
# Run test with specified name
$ tox -e py36 -- tests/test_core.py -k test_split_entity_id
# Fail a test after it runs for 2 seconds
$ tox -e py36 -- tests/test_core.py --timeout 2
# Show the 10 slowest tests
$ tox -e py36 -- tests/test_core.py --duration=10

{% linkable_title Testing outside of Tox %}

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 pip3 install -r requirements_test_all.txt

Now that you have all test dependencies installed, you can run tests on 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

{% 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. Example of an unavoidable one is if PyLint incorrectly reports that a certain object doesn't have a certain member.