I'm a member of my high school's robotics club, and am responsible for programming the robot. One suggestion I keep hearing from various adults is that I should be writing unit tests to help validate my code. The code base is getting a bit big, and I do agree that unit tests would be really helpful in helping me catch bugs quicker.
However, I'm not entirely sure how I could accomplish this. To the best of my knowledge, unit testing is done by taking a function (or a subsystem of the code) and feeding it a set of input to make sure it comes out with the same output each time. The code that I currently have doesn't do any heavy data crunching, but rather directly manipulates the hardware components on the robot. Most of the complexity comes from making sure that the electronics are sound, that the code at the moment matches the actual hardware on the robot, etc. Often times, I can only see if there's a problem by loading the code to the robot itself, and attempting to run it.
By extension, how can unit tests be written for code meant to operate any mechanical device? It seems to me that you can only catch errors by physically observing the operation of the machine.
Or am I just misunderstanding how unit tests should work?
(If it matters, here's the code, it's written in C++, and I'm participating in FRC)
You will need to write a test harness to mock the input devices - hook the input device code as close to the hardware layer as possible
– mattnz Apr 25 '12 at 08:23...the great benefit of automated unit tests comes well after the tests have been written
. I agree with your implication however that it is questionable whether there is value writing tests after implementation given the relative effort required, however I'd assume there is work to do at the hardware layer, in which case it would make sense to implement some unit testing there in order to better support and debug calls to hardware. – S.Robins Apr 25 '12 at 13:34