roboticsatiowa / Rover_Embedded

1 stars 0 forks source link

Unit testing- A viable solution #9

Open klastine opened 6 months ago

klastine commented 6 months ago

Hey guys, was poking around my old Github repos and found this. It was attached to a previous iteration of the Robotics at Iowa codebase which I originally created as club VP & Software Lead (2020-2022). I noticed in your README that it says you don't have a viable way to unit test, which I'd like to commend you for because unit testing wasn't even on my mind when writing rover software back then...

Anyways, I've since learned a thing or two about testing code designed embedded systems and I'd like to put a suggestion forward for your consideration. The Google test suite has a very well established feature set called "Gmock" that one can essentially use for simulating responses for the purposes of test. In essence, one could create a wrapper class around your real IO and override it for the purposes of the test, defining fake return values along the way. For example...

class DigitalOutPin { public: DigitalOutPin(int pin) { pin_num = pin; } virtual void WriteDigitalOut(int val) { digitalWrite(pin_num, val); } private: int pin_num; };

and then you can mock like

class MockDigitalOutPin : public DigitalOutPin { MOCK_METHOD(void, WriteDigitalOut, (int), (override)); }

Then, in the tests you use the subclass and can use the Gmock functions to just return a fake value instead of interacting with hardware. These docs have much much more (https://google.github.io/googletest/gmock_cook_book.html). Of course, for this to work you'd need to migrate your setup to be more class-based in order to inject the correct dependency, but based on how you have it set up here I think it would be fairly straightforward to do that. Let me know if you have any questions, happy to help with anything!

ethanholter commented 1 week ago

Hey Im sorry it took so long to see this - we havent been using the issues feature much. I would like to tackle unit testing this year. If you are still up for it we could really use your guidance on how to get started with this. Its great to hear youre still keeping up to with the club!

klastine commented 1 week ago

Absolutely! I don't really check my UIowa email anymore, but you can email me at kylejlastine@gmail.com or contact me some other way. Would definitely be happy to provide mentorship to you and the software team.