Tested on:Python 2.7
Prerequisites:Python project setup for test-driven development

Software testing: mock edge values or physical interactions

Mocking is very useful to simulate function calls. Instead of running through huge sample sets of data, edge values can be passed. It is especially useful for development on a Raspberry Pi, since it allows to replace physical interactions, like reading sensors or triggering pins.

Assume we have a Python project set up like this:

In the subdirectory tests/, we mock the time-intensive method count_errors by passing the ideal result instead. The next test could do the same for an error value which is too large, for example.

Now, further assert statements can be added to test the applications behaviour when zero errors occured.

Using an lambda functions allows an even more elegant way of defining a simple mock:

Mock pin triggering: http://www.knight-of-pi.org/test-complex-gpio-interactions-easily-with-call_args_list/
Lambda functions: http://www.knight-of-pi.org/lambda-functions

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


Time limit is exhausted. Please reload CAPTCHA.