question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rewrite homeassistant unittest tests to pytest style test functions

See original GitHub issue

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:

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinHjelmarecommented, Oct 17, 2022

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

0reactions
Ajax-Lightcommented, Oct 17, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Full Changelog for Home Assistant Core 2022.11
These are all the changes included in the Home Assistant Core 2022.11 release. For a summary in a more readable format: Release notes...
Read more >
UNIVERSIDADE FEDERAL DE MINAS GERAIS Instituto de ...
Rewrite graphite unittest tests to pytest style test functions #40850. The Home Assistant core standard is to write tests as standalone pytest test...
Read more >
stable PDF - pytest Documentation
pytest discovers all tests following its Conventions for Python test discovery, so it finds both test_ prefixed functions.
Read more >
AWS Greengrass - Unit test components
Hi there, I am trying to develop unit tests in python language for simple AWS Greengrass components, using pytest framework and simulating a...
Read more >
Unit Test with PyTest - Level Up Coding - gitconnected
However, PyTest is one of the most popular unit-testing libraries due to its many features. pip install pytest. Type pytest -h in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found