Can you write unit tests for Arduino stuff?

Pilot

Well-known member
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?
 
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.
 
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....

regresstest.jpg
 
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
}
 
Hi I am a student that is currently in formula student. I am developing software in our car. We are using teensys 4.1.
My problem is that in our last competition one judge said that we didn't have any plans to test our software and everything. Since I am studying computer science I have already dealed with a lot of testing (unit testing, gerkin, etc). What I am asking is that if there is something similar to use on the teensys in order to test my code. I can send you my code inside my teensys if it is better to someone help me.
 
When I was writing software as part of my job, I would test each and every function against it's requirement and outside it's expected extremes of input/output values.
Such documented testing with results should suffice.
If a function malfunctions when fed with valid/invalid data then the function should be modified to handle it correctly and/or return an error condition. It should NOT crash the software program.
 
I know that Arduino is starting to add some stuff, for unit testing, as was posted recently on the Arduino forum thread:
https://forum.arduino.cc/t/uarts-code-trying-to-enhance-the-performance-and-understand/1147711/23

Back when I used to do this for a living, our groups also included some Software Test Engineers whose jobe it was to come up with tests for the different units, subsystems....

@BriComp - I agree with you that we should try to make our code more bulletproof!

Code like this: (UNOR4)
Code:
void digitalWrite(pin_size_t pin, PinStatus val) {
	R_IOPORT_PinWrite(NULL, g_pin_cfg[pin].pin, val == LOW ? BSP_IO_LEVEL_LOW : BSP_IO_LEVEL_HIGH);
}
Drives me nuts. Pass in pin number out of range, it it grabs random memory...
 
I know that Arduino is starting to add some stuff, for unit testing, as was posted recently on the Arduino forum thread:
https://forum.arduino.cc/t/uarts-code-trying-to-enhance-the-performance-and-understand/1147711/23

Back when I used to do this for a living, our groups also included some Software Test Engineers whose jobe it was to come up with tests for the different units, subsystems....

@BriComp - I agree with you that we should try to make our code more bulletproof!

Code like this: (UNOR4)
Code:
void digitalWrite(pin_size_t pin, PinStatus val) {
	R_IOPORT_PinWrite(NULL, g_pin_cfg[pin].pin, val == LOW ? BSP_IO_LEVEL_LOW : BSP_IO_LEVEL_HIGH);
}
Drives me nuts. Pass in pin number out of range, it it grabs random memory...
Yes, at the time I was writing code as part of my job, I was using Modula2 which has a lot of built in error correction/capture.
I could be assured of catching 99+% of all errors at compile time.

Personally I think that with C / C++ in it's current state it SHOULD NOT be used as a REAL programming language.
There seems to so many instances where you cannot be sure what the compiler will spit out.

....But then Microsoft did champion it so what can you expect.

As someone trained as a mechanical engineer, then taking on electronics and software, I was aware that problems with system can occur.

I had a project where I was writing a control system for a machine tool. I made sure that every function which involved machine motion returned a PASS/FAIL (+ error Code).
In that way any error in the system was captured and fed back to the maintenance teams, be it Mech, Elec or Soft.

When I took the companies Software Department to task about error reporting, their stance was if the error was outside the software then it was nothing to do with them!!
I might add that the Software Manager at the time is no longer with the company, but is unfortunately still involved with the production of software.
 
Last edited:
Back
Top