How to unit test action plugins?


I’m the author of the kicad-jlcpcb-tools plugin.

As teh plugin evolves and gets more complex I search for a way to automatically test it.
But I’ve not yet found a way to do that and hope somebody can point out a source that shows how to effectively unit test a python plugin for KiCAD.

I struggle a lot with various versions of wxpython on different operatings systems and KiCAD versions (I support 6.0 and 6.99 at the moment).

Every tip is highly appreciated!

Here’s a few tips that I use for InteractiveHtmlBom:

  1. Have a CLI mode for your plugin if possible. That enables running integration tests in command line.
  2. Decouple your business logic from gui as much as possible. Ideally GUI just creates another input for your actual logic and you can mock that input in normal unit tests. In my case GUI is one of the ways to initialize a config object (another is CLI parser) and that config just gets passed to the plugin core.
  3. Try to keep compatibility hacks for different versions of wxpython or kicad localized to one file. You could even write a shim layer to deal with those specifically.

I don’t unit test GUI but it can be done with input emulation tools on various OS.

1 Like

Thank you for your tips!

This sounds like a lot of work as at the moment business logic and gui a not separeted by any means but a massive overhaul is most likely the best option …

I don’t need actual GUI tests but a lot of my problems are cause by inconsistencies between various wxwidgets versions that case the plugin to fail at various places (startup, button clicks, etc.)

I thought about having several VMs with a variety of KiCAD versions / OS versions that I can use for testing but that takes a lot of resources i guess :neutral_face:

Anyway, thanks for your comment!

In my experience kicad python api is a much faster moving target than the wxwidgets api. I think the only inconsistency in wxpython that can’t be remedied without a version check and completely different code paths was the wxBitmapButton api that changed in 3.2. Everything else I found was either because I was using API wrong or has an easy workaround that works on all versions >=3.0 which is good enough for our purposes.

Maybe if you give more details on your issues with wxpython I can provide more specific help to ease your pain with that api, there is no helping with kicad api though :smiley: Not until the new stable python interface.

My solution for my Code/Stand-Alone programs to work with Kicad is to:

• Create my Program’s as Stand-Alone programs that can be booted from a Kicad Plugin. That way, they are not dependent on wx, Zelle, TKinter… others.

In fact, about half of mine are Java.

I simply call them (run them) from a Simple Kicad Plugin. Here’s an example of running:

• A Python, Executable Program (not running Python code in plugin)
• A LibreOffice DataBase of Project Mgmt
• A Windows Executable Program (CopperCAM) running from PlayOnMac, called by a Kicad plugin…
• A Java App

Thus, you can accomplish having your program as desired and simply have a Plugin call it. Naturally, your program and/or the plugin can do the tasks you decide and define/code as you like… Use your code to Call the PCB and Schematics and grab and parse the data of interest…

EDIT: Added Java App vid






I have almost no trouble with the KiCAD API itself.
But some Systems have wxwidgest 3.0.5, some have 3.1.4 some 3.1.7 and so on. And they all behave a little different. I guess on Windows KiCAD bundles its own wxwidgets, on linux the on installed on the system is used i guess and for MacOS I have no idea how things work there …

I thought about creating a GUI with something else than wx but so far I did not want that the user has to install dependencies for the plugin to run. Also I don’t know how this would turn out on different platforms, especially Flatpack is a bit of a pain when it comes to dependencies (at least what I’ve seen so far).

My ideal solution would be the plugin running a webserver and serving a Vuejs UI but I’m not yet ready to put in that much effort :sweat_smile:

Weirdly I had no issues with any minor revisions of wxwidgets. Only the 3.2 had a breaking change in the wxBitmapButton that I mentioned.

On Mac kicad also bundles it’s own wxwidgets, python and wxpython.

My point was/is showing that ‘whatever’ coded app you want to build run, you can run it from a very simple, non-dependent plugin. And/or as a Stand-Alone App.

Thus, Applets, C-based App’s, Java-based App’s, Basic-based App’s, Python…and/or a Link to Webpage that runs your code etc.

Therefore, you get to choose so, if not willing to put in the effort, grab your iPhone and push a button…

ADDED: Perhaps you forgot, user can ‘package’ all the dependencies into the final App such as to eliminate/reduce dependencies. Depends on your code… Packagers have options for setting this…

good luck

Yeah I was having he same issue and splitting GUI code and backend businesses logic is the only was to go.

You can browse Place Footprints action plugin code.

GUI is contained within, business logic is in and test code is in

The test will fail if you try to run them the first time, since they compare the board with known reference board. Since known reference boards change a lot, I don’t have them in repo. I generate them by running test once, checking the created boards, and if they are good I keep them as a reference boards.

1 Like