core icon indicating copy to clipboard operation
core copied to clipboard

Rewrite homeassistant unittest tests to pytest style test functions

Open MartinHjelmare opened this issue 5 years ago • 12 comments

Problem

The homeassistant integration still contains unittest.TestCase based unit tests. We want to rewrite them to standalone pytest test functions.

Background

The Home Assistant core standard is to write tests as standalone pytest test functions. We still have some old tests that are based on unittest.TestCase. We want all these tests to be rewritten as pytest test functions.

Here are the docs for pytest: https://docs.pytest.org/en/stable/

Here's an example of an async pytest test function in Home Assistant core:

https://github.com/home-assistant/core/blob/4cce724473233d4fb32c08bd251940b1ce2ba570/tests/components/tradfri/test_light.py#L156-L176

There are many pytest fixtures to help writing the tests. See:

  • https://docs.pytest.org/en/stable/reference.html#fixtures
  • https://github.com/home-assistant/core/blob/dev/tests/conftest.py
  • The most common fixture to use is hass which will set up a HomeAssistant instance and start it.

Here's an example of a pull request that rewrote a module of unittest.TestCase tests to standalone pytest test functions: https://github.com/home-assistant/core/pull/40749

Here's an example command to run a single test module with pytest inside tox on Python 3.8:

tox -e py38 -- --cov-report term-missing --cov=homeassistant.components.command_line.switch tests/components/command_line/test_switch.py

It will print coverage information with lines that are missing coverage.

Prerequisites

Task

  • Rewrite the tests one module at a time and submit the changes as a pull request to this repository.
  • We want to limit the change scope to a single module to not have the pull request be too long, which would take longer time to review.
  • Remember to reference this issue in your pull request, but don't close or fix the issue until all tests for the integration are updated.

MartinHjelmare avatar Sep 30 '20 23:09 MartinHjelmare

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Dec 29 '20 23:12 github-actions[bot]

is this issue up for grubs?

rodneyosodo avatar Oct 02 '21 11:10 rodneyosodo

Yes, please go ahead. 👍

MartinHjelmare avatar Oct 02 '21 12:10 MartinHjelmare

Hey @MartinHjelmare I'd like to convert the Aurora Component. Since I'm new to testing, I'd like to take some time with this. Would that be alright?

Ajax-Light avatar Oct 17 '22 14:10 Ajax-Light

The Aurora integration doesn't need converting as it doesn't have any unittest.TestCase based tests.

MartinHjelmare avatar Oct 17 '22 15:10 MartinHjelmare

Oh my bad, I assumed we needed unittest.Mock to be converted as well. I couldn't find any instances of unittest.TestCase other than the ones used in test helper classes. image

Ajax-Light avatar Oct 17 '22 16:10 Ajax-Light

The helpers also need to be converted but may not be as straightforward as the integrations. Please go ahead if you like.

MartinHjelmare avatar Oct 17 '22 18:10 MartinHjelmare

i would like to contribute in this

fridady0 avatar Oct 30 '23 14:10 fridady0

Please go ahead.

MartinHjelmare avatar Oct 30 '23 16:10 MartinHjelmare

I would like to contribute in this !

Amine-bc avatar Oct 12 '24 21:10 Amine-bc

I can only find one remaining test class that uses unittest.TestCase. It's TestSecrets in tests/util/yaml/test_init.py. Please go ahead and update it. Thanks!

MartinHjelmare avatar Oct 14 '24 10:10 MartinHjelmare

There's already two open PRs for that, though: https://github.com/home-assistant/core/pull/127770 https://github.com/home-assistant/core/pull/127842

MartinHjelmare avatar Oct 14 '24 10:10 MartinHjelmare