Unit tests for integration

I’m struggeling to get started writing unit tests for home assistant integration. I couldn’t find any help neither on this forum nor on the discord channel.
Thus i decided to setup this topic with the aim to collect all information/help reagarding the setup of unit tests.

Setup Development environement

I started to setup the development environement using this link

After the environement was ready i had to lookup how to run unit tests on existing integrations. This would allow me to understand more in detail how the existing unit tests are working.

Setup the pytest framework

I tried to configure the python testing framework (see figure below)

Then i choosed the pytest framework

The refresh icon started spinning for a long time

Once ready, all the unit tests showed up and it was hard to filter for tests related to a specific integration.
I figured out a way to apply a filter to show only the tests related to a specific integration.
In the folder “.vscode” you have to modify the file “settings.json”.
After a fresh setup of the pytest framework the content of the file looks like this:

{
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true
}

I wanted to filter only for the unit tests related to the pi_hole integration and thus enhanced the filter like this:

{
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.testing.pytestArgs": [
        "--exitfirst",
        "--verbose",
        "tests/components/pi_hole/",
    ],
}

If you want to filter for more integrations simultaneously you only have to add the path of the unit tests as new entry to the pytestArgs

Now you can try and test the existing unit tests:

Understanding the pytest framework

I discovered this tutorial on youtube that helped me understanding the Fixtures and the Mocking concepts.
Fixtures
Mocking

Now my next step is to try and understand how existing unit tests work.

Discover the basic unit tests to setup for a new integration

1 Like

So, when i am developing an integration for home assistant core and use the development docker environment, all dependencies were already setup.

I had wrote my tests and executed pytest command to run my unit tests. That is it, i didn’t do anything specific further, other than adding my external package as test dependency and writing the unit tests.

What is the name of your integration, is it already available ?

It is getting in infinite review process :slight_smile: https://github.com/home-assistant/core/pull/62451

I am even not sure if it is all running correctly, having a custom component is much more easier, but you know, it is time to streamline :stuck_out_tongue:

it’s the opengarage integration ?

I sent the pull request link which is about Hyundai Kia Connect integration.

@OM_luxbg Have you gotten any further with your unit test experience? I was trying to update an existing integration with only minimal unit tests and was hoping to add some missing coverage. But, I just don’t know where to start.

Have you checked here? Add Hyundai/Kia Connect integration by fuatakgun · Pull Request #62451 · home-assistant/core · GitHub

@anon63427907 . Actually, yes I did. I’m trying to add tests to the tuya integration and it already has a config_flow test. I was looking for examples for handling the switches and sensor type tests.There appears to be a bunch of boilerplate, but I don’t yet understand what is the minimum for testing.