Unit Testing appdaemon apps

Has anyone figured out how to write unit tests that work with appdaemon apps, I’m pretty stumped and I see very few references. This thread includes someone who claims to have gotten it working, but doesn’t provide enough context to follow the example.

As an example, let’s say I have an app that detects a door binary sensor being opened, turns on a light, and then turns if off 10 minutes later. I want to mock that whole thing out including the clock, and make sure that it runs correctly without error from the command line.

I do not want to open my logger, run over to the door, open it, verify the light comes on, start at my watch for 10 minutes, then run back to my computer, only to discover I misspelled sefl.turn_off, fix that and then start again.

I’m not aware of a way but I can provide an alternative solution that I use sometimes. I tend to run new apps in a local environment on my computer and run AD through the debugger although that is not necessary but it’s nice to have an alternative environment for testing new apps. Additionally if there is something I am trying to do that is related to timing, I will use the timewarp (-t) setting to speed up testing my automations.

I appreciate the comment. I didn’t know about timewarp, for example. Still, this isn’t quite what I’m looking for. This is more “debugging” than “unit testing”. I’d like the tests to tell me if I’ve broken existing behavior as I’m adding new behavior, for example.

Hi @Gregable,

I remember answering something similar in this post (and the following ones): Import custom code - #8 by xaviml

I developed ControllerX, which is a very large AppDaemon app, and when the code was getting larger I needed a way to test things to ensure I was not breaking anything. However, the tests I wrote are about mocking AppDaemon calls that call HA, so I am not able to identifiy if the call is correct for HA, but I can ensure that the call X has been called with paramerts Y. If you go through the code, you will see that I have unit tests and integration tests where I am testing the whole cycle of the AppDaemon app.

In that post, I also mention a GitHub repository I found, which is a pytest plugin to test AppDaemon apps. However, I could not test the whole flow of the app with it.

Regards,
Xavi M.

1 Like