7

I am a professional web developer and therefore I'm used to using very high-level scripting languages and testing tools. I've recently been working, personally, more with C and writing many C programs for unix-based systems to do various tasks.

However, I still haven't gotten into a good groove for unit testing this code and I was wondering what tools C programmers use to create automated tests to verify code.

Mitchell
  • 32,511
  • 6
  • 41
  • 35

6 Answers6

2

Consider using CppUTest. It is written in C++, but hides the C++ so that C programmers can ignore the C++.

Unity is a C-only test harness that is good too. It uses ruby to generate test runners.

I'd stay clear of CppUnit. It requires C++ knowledge and each test has to be individually installed.

James

Nate
  • 17,918
  • 26
  • 69
  • 93
1

I work as developer and I prefer to use CMocka tool to provide UnitTesting for C language. It's simple tool based on GTest (mostly C++ unit testing) and it's really easy to understand. Some time ago I made a research for the best and the simplest testing tool and decided to this.

And after some time working with this I am really confident. With using preprocessor directives

#define MyFunction(iEntry) mock_MyFunction(iEntry)

you can also cut-off any function you want to simulate (even basic windows functions like sizeof(), malloc() and so on).

After this step you only have to create your mock functions (mock_MyFunction(iEntry)), include them into your project and simulate their behaviour. Prefered way is to set them some values before calling they should return ( will_return_always(mock_MyFunction, 5) ). And when this function is called, it will always return value 5.

Also you can totally avoid C++ language and stay working with plain C language. Dont be aware of simple index site of CMocka. You have to dive into that site and API of tool to find what you need.. :) Also check this for little tutorial of using CMocka...

I hope this helps, have a nice day.. :-)

Ken Y-N
  • 13,857
  • 21
  • 69
  • 105
Zdeno Pavlik
  • 670
  • 6
  • 15
1

Are you testing strictly C code, or can you mix in some C++? If C++ is OK, cppunit is probably worth checking out. If you are testing a library, you can simply link you library into your unit test application. If you are testing an app, you will probably have to create a library, and then link it into both your app and the unit test app.

mch
  • 6,875
  • 3
  • 29
  • 34
1

I had this question once and discovered CUnit. I only used it for a small project, but it seemed alright. Not nearly as convenient as JUnit or NUnit that I am used to though! For C++ projects I use cppunit which works quite well.

Jeremy
  • 3,434
  • 3
  • 21
  • 24
1

CUtest. It's really neat. Small, doesn't rely on a generator language, fast... what's not to like?

user60401
  • 678
  • 1
  • 4
  • 6
0

If you only want a quick'n'dirty test mechanism, have a look at the assert macro, part of ANSI C.

a2800276
  • 3,113
  • 19
  • 31