Python tests while multiprocessing and hardwaring
Machine Learning Rants
This is a small tutorial for future me, on how to do python testing if there are hardware components and multiprocessing involved. The core of my Home Automation system is a RaspberryPi, running Python and managing some sensors and relays.
I want to develop and test all the logic on my main computer (x86 running Ubuntu) that does not have all the sensors and libraries available on the Raspberry.
The system is built as a collection of several processes that interact. I love the isolation that the multiprocessing brings but I don’t like that testing the code is a bit harder.
Of course pytest is the main actor with help from mocks, surrogate, logging and custom shared objects handled by the SyncManager.
Most of the chapters are independent.
00:00 Introduction 00:38 How to surrogate a missing hardware library 05:45 Introduction to testing while multiprocessing 07:55 An exception inside a process is silently ignored and does not propagate outside 11:02 Using logging system to collect exceptions throughout different processes 19:10 Mocking can inject behavior in another process but it can’t retrieve states 22:42 How to get information about function calls from a different process 30:18 Run lengthy experiments faster by accelerating the time 34:02 Actual test example with a complex behavior and multiple processes interacting 37:03 Few words on coverage reports while running code from different processes
Enjoy!
P.s. StackOverflow question: https://stackoverflow.com/questions/12585046/how-do-i-mock-the-hierarchy-of-non-existing-modules
And the answer: https://github.com/ikostia/surrogate
597911522 Bytes