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
…