This is an old revision of the document!
Plugin Testing Tutorial
Once you have developed a functioning plugin, it's a good idea to add automated methods to test your code before publishing updates. Code testing is considered a best practice, and Python provides built-in tools to help facilitate testing. There are many ways to formulate your tests, and this guide demonstrates only one way to do it. The approach described below has advantages within the Indigo plugin framework because it allows direct access to the IOM (Indigo Object Model). It is extremely important to become familiar with the IOM and Indigo plugin development before attempting to tackle this guide.
Python Unit Testing
The main Python library for code testing is called unittest. There are lots of examples of unit testing online and it's probably a good idea to familiarize yourself with the unittest library before going any further with this guide. The main documentation for the unittest library is available at https://docs.python.org/3/library/unittest.html and is a great place to start learning about unit tests. Those who are familiar with unit testing in Python can skip ahead.
Unit Testing Indigo Plugins
Because Indigo runs plugins in a separate thread (to avoid having a misbehaving plugin bring down the whole system), plugin unit tests have to be constructed a little bit differently. For example, while some tests can be run in the “traditional” way, tests that need access to the IOM can't be run from within an IDE or from the command line without using the Indigo Integration API (which doesn't expose the full suite of IOM commands). However, it is possible to construct tests that do allow for full IOM access through the plugin itself.
Testing Elements
The following example relies on several testing elements:
- The
unittestlibrary - this is a standard Python library and should be already available. - The
python-dotenvlibrary - used for creating and managing Python environment variables. - An IDE that supports unit testing - which not required, having an IDE that supports unit testing can be very helpful.
Testing Structure
As mentioned above, this is only one way to accomplish plugin unit testing within the Indigo environment. It's meant to be simple and straightforward enough to get started, but allows lots of room for customization to different plugin frameworks. This example is written with references to PyCharm, but other IDEs should work equally well.
Environment Variables
While optional, it is a good idea to create an environment variable framework that allows you to create and make references to elements of your development environment. Once you have installed python-dotenv, create a .ENV file at the Server Plugin level of your plugin (or other location that Indigo's environment path search will find it). It is a plain text file. The advantage of using an environment file is that it is a great place to store references that are unique to your system and – for shared development – each developer can have their own individual environment. IMPORTANT! Remember to add the .ENV file to your .gitignore list.
.ENV file
# The .ENV file can contain comments SECRET_CODE=zm76%g215^8sdhe BASE_URL=https://localhost:8176/v2/api/ ACTION_GROUP_ID=12345678 ACTION_GROUP_FOLDER_ID=12345678
Folder Structure
While optional, it's probably best to organize your test files and keep them separate from your main plugin files. For the purposes of this tutorial, we will store them in a Tests folder of the Server Plugin. For example,
../Server Plugin
|_ Tests
|_ __init__.py
|_ test_plugin.py
Of course, you can put them anywhere that your plugin can see them. Depending on your development environment, you may also want to add the Tests folder to your .gitignore file to reduce the footprint of your published plugin.