Frage

I wrote a lot of software in C# and Python. I tried to make the overall architecture testable by using the "Clean Architecture" and Dependency Injection. This works well for C# (and python).

Now I move to a new company where I will program C (it will be a new codebase). Some years ago I already wrote much C, but without a strong focus on testing. This time I like to create a clean and testable architecture which allows to test anything.

Does a best pratice like the "Clean Architecture", "Onion Architecture" or some other architectural pattern exist that leads to well testable C code? (of course, given the code is well written)

War es hilfreich?

Lösung

The nice thing about architectural patterns is that they are largely language independent. Some languages may have better or worse support for the basic constructs that a particular pattern is built upon.

Many architectural patterns are built upon Object Oriented concepts. C does not have built-in OO features, but you can actually implement OO designs in C.

The standard techniques for testing code in C are

  • Swap out production code for stubs/mocks/fakes at the file level by linking a different file into the test executable
  • Use preprocessor macros to disable functionality not wanted during testing
  • Write OO-style code and use DI
Lizenziert unter: CC-BY-SA mit Zuschreibung
scroll top