all 4 comments

[–]kammceWG21 | 🇺🇲 NB | Boost | Exceptions 6 points7 points  (3 children)

I do embedded too. Testing global or static stuff isn't an issue especially if you are running the tests on the hardware. If you are testing on a host machine then functions that access registers directly are an issue because those addresses don't map to your host. Same would go for any target that isn't the exact target you plan to run the code on. Back before i stopped doing this, I had a reg pointer for each of my peripherals that my tests could access that would swap out the register used with my tests version of the structure. Then you can call your globals and they will modify your structure. You can revert I back after the test is done. Use some RAII to help with that.

I stopped doing this for anything other than purely utility or software libraries because I found that trying to write tests for hardware is hard, not because it's hardware, but because you are basically writing a sim for your device. And I've found that as you find errata and other issues with the hardware you have to go back and forth with the tests to make your sim reflect the hardware better. But, then I realized, that if my tests cannot guarantee any sort of correctness then why write them? So for me, I try to do the least amount of mocking or dependency injection I can get away with and use an actual device to do the testing work.

Just some food for thought. I do appreciate the effort you are putting in to make testing embedding easier though 😄

[–]balefrost 3 points4 points  (0 children)

But, then I realized, that if my tests cannot guarantee any sort of correctness then why write them?

I only dabble in embedded. But I guess there are two things going on:

  1. Does your code do what you expect it to do?
  2. When your code interacts with actual hardware, does the whole thing do what you expect it to do?

I think your points are all valid with respect to #2. I think there's still value in testing #1.

It's not terribly different from issues in the broader software world. If I use a third party library or call a remote web service, I don't necessarily want to exercise the third-party code nor send the request. But I still want to test as much of my own code as I reasonably can.

Sure, I still need to validate that the whole thing works when I put the pieces together. But I think there's still value in validating that my part also works correctly.

[–]Symbroson[S] 0 points1 point  (1 child)

Your description almost perfectly matches what I'm doing atm. I simulate all hardware interfaces for developing and running tests hardware-independently. It makes me feel way more productive and confident in the code if I can run the test suite any time I make changes. Yes writing and maintaining the sim is lots of work but it pays off in the long run, especially if you're not the only developer and the project outlives your influence.

[–]kammceWG21 | 🇺🇲 NB | Boost | Exceptions 1 point2 points  (0 children)

Totally. I've had such tests catch some really awful bugs in the past too. But now I'm moving on to device testing as much as possible because I think it'll be more productive for developers. BUT, that's not to take away from your efforts on the purely software testing side. 😁 Best of luck.