Testing embedded systems with Python (or at all, really) is an extensive topic. This post is not going to dive too deep into the subject but instead suggest one potential approach from a high level as well as cover a few third party libraries that might make life a bit easier.
Before going any further, let's take a look at why you might want to incorporate Python into your test solution. Including a high-level scripting interface to your tester affords a great deal of flexibility but is not appropriate in all cases. The following are a few reasons you may want to go down the Python route:
- The device under test (DUT) is complex enough to require a variety of test cases to be written.
- The components of the test system may change and the details can be hidden under the scripting interface.
- Higher-level applications (e.g. GUI applications written in PyQt) could be implemented using the scripting interface.
The following is a rough progression for rapidly developing a test solution:
- Evaluate your requirements.
- Draft a solution.
- Develop a mock API and example script.
- Implement a proof-of-concept (POC).
- Iterate on the POC until functionality is covered.
- Stabilize the design and update if necessary.
Let's take a further look at each item of this progression.
Evaluate your requirements
The obvious first step when developing any solution. Determine what you need to achieve with the test system and whether Python is a good fit. A few questions to ask during this phase:
- What IO will be handled?
- How is power handled?
- What measurements need to be taken?
- Are there timing requirements?
Draft a solution
After answering the questions raised in the requirements phase, you should have a pretty good idea of what sort of test hardware is needed. Draw a diagram of the test system and show all the interfaces that will need to be handled. Research your equipment choices and see what protocols you might be working with; look into existing Python libraries to help you out. Here are a few useful libraries when working with test equipment:
- MinimalModbus - Easy to use Modbus library.
- PySerial - Essential for basic serial interface work.
- PyVisa - Useful for talking to common test equipment.
Develop a mock API and example script
Without having any hardware in place, sit down and write an example of what a test script might look like. Try to identify anything you may have overlooked during the previous steps. During this process begin laying out the test system API. Keep it generalized to the functionality that is being tested and plan on hiding implementation details. See if the standard library unittest framework suites your needs, otherwise look into other offerings such as nose.
Implement a proof-of-concept (POC)
Start assembling hardware and coding. Try to get functionality up and running quickly, verify it works properly, then develop the associated API calls. Look for anything that may impede progress and refactor if necessary.
Iterate on the POC until functionality is covered
Verify that the POC functions as expected and clean up the code and hardware as necessary. Try writing additional test scripts to make sure everything is covered.
Stabilize the design and update if necessary
Put the finishing touches on the design. Document the API and hardware design, check out Sphinx for a possible documentation toolchain. Consider the tester in beta until proven.
This article barely scratches the surface of the topic. Test solutions for embedded systems are very specialized projects and the process covered in this post is a generalization of one approach when very little existing resources are available. Just remember that the addition of a Python scripting interface can dramatically increase the capabilities of your next test system.