Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Can you write unit tests for Arduino stuff?

  1. #1
    Senior Member
    Join Date
    Jul 2020
    Posts
    103

    Can you write unit tests for Arduino stuff?

    For some years I have been using test-driven development (TDD) to good effect in my career, which is writing software that runs on "full" computers. The learning curve is steep, but it makes code so much easier to develop and maintain that I can't see a reason to go back to the old way.

    Well, maybe one reason, where Arduino-based stuff is concerned: I don't know if it can be done. There is a lot of stuff like delayMicroseconds() that isn't in a standard libc, so I don't know how it would compile for my dev machine.

    Obviously, I can't test things like interrupts and GPIO on an x86-64 target, but the majority of code is at a higher level of abstraction and could be unit-tested, if only it could be made to compile.

    Anyone have any luck with this?

  2. #2
    Senior Member
    Join Date
    Jul 2020
    Posts
    322
    Quote Originally Posted by Pilot View Post
    For some years I have been using test-driven development (TDD) to good effect in my career, which is writing software that runs on "full" computers. The learning curve is steep, but it makes code so much easier to develop and maintain that I can't see a reason to go back to the old way.

    Well, maybe one reason, where Arduino-based stuff is concerned: I don't know if it can be done. There is a lot of stuff like delayMicroseconds() that isn't in a standard libc, so I don't know how it would compile for my dev machine.

    Obviously, I can't test things like interrupts and GPIO on an x86-64 target, but the majority of code is at a higher level of abstraction and could be unit-tested, if only it could be made to compile.

    Anyone have any luck with this?
    You need a test-harness version of the Arduino libraries to compile against, one that can be programmed to behave in certain
    ways that you expect the hardware to do, then you can test the software against that, for various scripts. The test harness
    would use virtual time so delays are magiced away.

    Its a lot of work of course, both to write such a library and to construct harnesses for each hardware setup. Its often easier
    to just use the real hardware and automated test equipment (or people) talking to that hardware.

    Its a good reason to separate I/O and hardware interfacing into small modules so the the rest of the code can be tested
    in isolation.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,467
    I recently started some work towards automated regression testing, but it's barely past the planning phase, and I'm not ready to write a lengthy post about it.

    But here's a photo....

    Click image for larger version. 

Name:	regresstest.jpg 
Views:	12 
Size:	137.6 KB 
ID:	21327

  4. #4
    Senior Member
    Join Date
    Jul 2020
    Posts
    103
    Some good stuff here.

    I think it would be somewhat "arduous" to make Arduino stuff compile on regular old g++.

    I guess you could split the difference: write as much code in a platform-agnostic way as possible, and put Arduino-specific stuff like delayMicroseconds() into utility classes that look something like this:

    Code:
    void Time::delayMicroseconds(int uS) {
        #IFDEF ARDUINO
        delayMicroseconds(uS);
        #ELSE
        usleep(uS);
        #ENDIF
    }

  5. #5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •