How can I unit test Arduino code?

后端 未结 20 772
情深已故
情深已故 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:19

    I built arduino_ci for this purpose. Although it's limited to testing Arduino libraries (and not standalone sketches), it enables unit tests to be run either locally or on a CI system (like Travis CI or Appveyor).

    Consider a very simple library in your Arduino Library directory, called DoSomething, with do-something.cpp:

    #include 
    #include "do-something.h"
    
    int doSomething(void) {
      return 4;
    };
    

    You'd unit test it as follows (with a test file called test/is_four.cpp or some such):

    #include 
    #include "../do-something.h"
    
    unittest(library_does_something)
    {
      assertEqual(4, doSomething());
    }
    
    unittest_main()  // this is a macro for main().  just go with it.
    

    That's all. If that assertEqual syntax and test structure looks familiar, it's because I adopted some of Matthew Murdoch's ArduinoUnit library that he referred to in his answer.

    See Reference.md for more information about unit testing I/O pins, the clock, Serial ports, etc.

    These unit tests are compiled and run using a script contained in a ruby gem. For examples of how to set that up, see the README.md or just copy from one of these examples:

    • A practical example, testing a Queue implementation
    • Another set of tests on another Queue project
    • A complex example, simulating a library that controls an interactive device over a SoftwareSerial connection as part of the Adafruit FONA library
    • The DoSomething example library shown above, used to test arduino_ci itself

提交回复
热议问题