How can I unit test Arduino code?

后端 未结 20 738
情深已故
情深已故 2020-12-07 06:53

I\'d like to be able to unit test my Arduino code. Ideally, I would be able to run any tests without having to upload the code to the Arduino. What tools or libraries can he

20条回答
  •  無奈伤痛
    2020-12-07 07:13

    I have considerable success unit testing my PIC code by abstracting out the hardware access and mocking it in my tests.

    For example, I abstract PORTA with

    #define SetPortA(v) {PORTA = v;}
    

    Then SetPortA can easily be mocked, without adding overhead code in the PIC version.

    Once the hardware abstraction has been tested a while I soon find that generally code goes from the test rig to the PIC and works first time.

    Update:

    I use a #include seam for the unit code, #including the unit code in a C++ file for the test rig, and a C file for the target code.

    As an example I want to multiplex four 7 segment displays, one port driving the segments and a second selecting the display. The display code interfaces with the displays via SetSegmentData(char) and SetDisplay(char). I can mock these in my C++ test rig and check that I get the data I expect. For the target I use #define so that I get a direct assignment without the overhead of a function call

    #define SetSegmentData(x) {PORTA = x;}
    

提交回复
热议问题