1

I'm sure this is documented somewhere but I'm struggling to find what I'm after. I am developing a project in C (and explicitly not C++), and having had good experiences developing Java projects with JUnit for TDD, I'd like to take a similar approach for this new project.

Can anyone provide a walkthrough for creating and running a simple C project, and running a couple of unit tests on them within Eclipse CDT? I have Eclipse CDT (Luna) and the unit-testing plugin as described in the frequently referenced blog. Most of the guidance appears to be for C++.

FWIW I'm running on Windows 7 and compiling with MinGW GCC.

beldaz
  • 4,299
  • 3
  • 43
  • 63
  • you may need to do something like http://stackoverflow.com/questions/9337757/unresolved-inclusion-error-with-eclipse-cdt-for-c-standard-library-headers/12869857#12869857 to get a compile. – Ray Tayek Jan 21 '16 at 22:02
  • I never liked the CDT Eclipse, was very inconsistent in my experience. I preferred Visual Studio (Express) to Eclipse for C/C++ development. – SGM1 Jan 21 '16 at 22:05
  • @SGM Yes I used to like Visual Studio, especially for debugging, but it feels very heavy-weight especially for small portable projects. I'm giving CDT a go to see if it fits somewhere between VS and Emacs (my previous C IDEs of choice). – beldaz Jan 21 '16 at 23:14

2 Answers2

2

Have you given google test a try? Its technically a c++ library, but has very little overhead to test your functions. A c++ test project would easily be able to consume the library generated from your c code, so should be pretty easy.

For example, this is what a test would look like

#include "gtest/gtest.h"
#include "MyProject/myFunc.h

TEST (myFuncTest, calculate) { 
    EXPECT_EQ (18.0, myFunc_calculate (324.0));
}

You can find more details at IBM Developerworks on YouTube etc.

Atif
  • 1,438
  • 16
  • 25
  • 1
    I think you've identified a key point for me: even if my code is pure C, the unit tests could be C++. – beldaz Jan 21 '16 at 23:16
  • 1
    But what's missing here for me is how do I set this up nicely for CDT? The IBM link, while useful, makes no reference to Eclipse. – beldaz Jan 21 '16 at 23:18
  • 1
    Setting up a separate C++ project this way, in conjunction with http://davidcozenssoftware.blogspot.com.au/2015/10/using-google-test-with-cdt-in-eclipse.html seems to get me there, thanks. – beldaz Jan 22 '16 at 05:24
0

After a lot of research, I tried most of the libraries for C and C++ testing and the one that was the right one for me was the Acutest. I have created a public repository with an example that also uses Github Actions and a makefile in order to accomplish Continuous Integration (CI) by compiling the code and the tests and running them on every push automaticly.

Repository Link : CPP_Unit_Testing

ilias
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 01 '22 at 15:09