3

i looked at Test C code with googletest but did not find any satisfactory answer.

In most of the unit testing frameworks, simple examples always work, but when it comes to medium or large code base, where there are 30 or more modules, and the dependencies are everywhere , HOW do use these tools ? plus gtest does not claim that it can test C code. but here is an example if using gtest to test c code test c code with gtest

Community
  • 1
  • 1
kamal
  • 9,637
  • 30
  • 101
  • 168
  • You can decompose a function into multiple modules or a single module. So I didn't see any difference between test a single module and 30 modules, which realize a single function. – Daniel Dec 23 '11 at 03:29
  • @Daniel The problem is at link time - if you're including modules that include unrelated code, you may end up linking a lot of things you don't need. – Timothy Jones Feb 10 '12 at 06:12

1 Answers1

1

Not really

As the blog post you linked says, the differences between the languages make it difficult to use a well designed C++ testing framework for testing C. Like the example in the article, you can shoe-horn C++ testing frameworks to test C using #define etc, but as you mentioned, you will run into problems when there are a lot of dependencies.

This is a common problem - this question about C testing includes a lot of C++ testing tools, but I really don't think they'd work well for larger projects.


Here are some suggestions

If you use autotools, you might want to look at Check. I've also been using test-dept, which is nice and lightweight, and allows for replacing functions on the fly. I've been using it to add tests to a very large codebase for a few months now, and it seems fine so far (though I ended up doing quite a bit of refactoring - but I think that would have been the case no matter the framework).

There are many other suggestions in the question I linked before.

Community
  • 1
  • 1
Timothy Jones
  • 21,495
  • 6
  • 60
  • 90