<p>I worked on an embedded system this summer written in straight C.  It was an existing project that the company I work for had taken over.  I have become quite accustomed to writing unit tests in Java using JUnit but was at a loss as to the best way to write unit tests for existing code (which needed refactoring) as well as new code added to the system.</p>

<p>Are there any projects out there that make unit testing plain C code as easy as unit testing Java code with JUnit?  Any insight that would apply specifically to embedded development (cross-compiling to arm-linux platform) would be greatly appreciated.</p>

[{'answer_id': 65845, 'author': 'Adam Rosenfield', 'author_id': 9530, 'author_profile': 'https://Stackoverflow.com/users/9530', 'pm_score': 10, 'selected': True, 'text': '<p>One unit testing framework in C is <a href="https://libcheck.github.io/check/" rel="noreferrer">Check</a>; a list of unit testing frameworks in C can be found <a href="http://check.sourceforge.net/doc/check_html/check_2.html#SEC3" rel="noreferrer">here</a> and is reproduced below.  Depending on how many standard library functions your runtime has, you may or not be able to use one of those.</p>\n<blockquote>\n<h3>AceUnit</h3>\n<p>AceUnit (Advanced C and Embedded Unit) bills itself as a comfortable C code unit test framework. It tries to mimick JUnit 4.x and includes reflection-like capabilities. AceUnit can be used in resource constraint environments, e.g. embedded software development, and importantly it runs fine in environments where you cannot include a single standard header file and cannot invoke a single standard C function from the ANSI / ISO C libraries. It also has a Windows port. It does not use forks to trap signals, although the authors have expressed interest in adding such a feature. See the <a href="http://aceunit.sourceforge.net/" rel="noreferrer">AceUnit homepage</a>.</p>\n<h3>GNU Autounit</h3>\n<p>Much along the same lines as Check, including forking to run unit tests in a separate address space (in fact, the original author of Check borrowed the idea from GNU Autounit). GNU Autounit uses GLib extensively, which means that linking and such need special options, but this may not be a big problem to you, especially if you are already using GTK or GLib. See the <a href="http://autounit.tigris.org/" rel="noreferrer">GNU Autounit homepage</a>.</p>\n<h3>cUnit</h3>\n<p>Also uses GLib, but does not fork to protect the address space of unit tests.</p>\n<h3>CUnit</h3>\n<p>Standard C, with plans for a Win32 GUI implementation. Does not currently fork or otherwise protect the address space of unit tests. In early development. See the <a href="http://cunit.sourceforge.net/" rel="noreferrer">CUnit homepage</a>.</p>\n<h3>CuTest</h3>\n<p>A simple framework with just one .c and one .h file that you drop into your source tree. See the <a href="http://cutest.sourceforge.net/" rel="noreferrer">CuTest homepage</a>.</p>\n<h3>CppUnit</h3>\n<p>The premier unit testing framework for C++; you can also use it to test C code. It is stable, actively developed, and has a GUI interface. The primary reasons not to use CppUnit for C are first that it is quite big, and second you have to write your tests in C++, which means you need a C++ compiler. If these don’t sound like concerns, it is definitely worth considering, along with other C++ unit testing frameworks. See the <a href="http://cppunit.sourceforge.net/doc/cvs/index.html" rel="noreferrer">CppUnit homepage</a>.</p>\n<h3>embUnit</h3>\n<p>embUnit (Embedded Unit) is another unit test framework for embedded systems. This one appears to be superseded by AceUnit. <a href="http://sourceforge.net/projects/embunit/" rel="noreferrer">Embedded Unit homepage</a>.</p>\n<h3>MinUnit</h3>\n<p>A minimal set of macros and that’s it! The point is to show how easy it is to unit test your code. See the <a href="http://www.jera.com/techinfo/jtns/jtn002.html" rel="noreferrer">MinUnit homepage</a>.</p>\n<h3>CUnit for Mr. Ando</h3>\n<p>A CUnit implementation that is fairly new, and apparently still in early development. See the <a href="http://park.ruru.ne.jp/ando/work/CUnitForAndo/html/" rel="noreferrer">CUnit for Mr. Ando homepage</a>.</p>\n<p><em>This list was last updated in March 2008.</em></p>\n</blockquote>\n<h2>More frameworks:</h2>\n<h3>CMocka</h3>\n<p>CMocka is a test framework for C with support for mock objects. It\'s easy to use and setup.</p>\n<p>See <a href="https://cmocka.org/" rel="noreferrer">the CMocka homepage</a>.</p>\n<h3>Criterion</h3>\n<p>Criterion is a cross-platform C unit testing framework supporting automatic test registration, parameterized tests, theories, and that can output to multiple formats, including TAP and JUnit XML. Each test is run in its own process, so signals and crashes can be reported or tested if needed.</p>\n<p>See the <a href="https://github.com/Snaipe/Criterion" rel="noreferrer">Criterion homepage</a> for more information.</p>\n<h3>HWUT</h3>\n<p>HWUT is a general Unit Test tool with great support for C. It can help to create Makefiles, generate massive test cases coded in minimal \'iteration tables\', walk along state machines, generate C-stubs and more. The general approach is pretty unique: Verdicts are based on \'good stdout/bad stdout\'. The comparison function, though, is flexible. Thus, any type of script may be used for checking. It may be applied to any language that can produce standard output.</p>\n<p>See <a href="http://hwut.sourceforge.net/" rel="noreferrer">the HWUT homepage</a>.</p>\n<h3>CGreen</h3>\n<p>A modern, portable, cross-language unit testing and mocking framework for C and C++. It offers an optional BDD notation, a mocking library, the ability to run it in a single process (to make debugging easier). A test runner which discover automatically the test functions is available. But you can create your own programmatically.</p>\n<p>All those features (and more) are explained in <a href="https://cgreen-devs.github.io/" rel="noreferrer">the CGreen manual</a>.</p>\n<p>Wikipedia gives a detailed list of C unit testing frameworks under <a href="https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C" rel="noreferrer">List of unit testing frameworks: C</a></p>\n\n'}, {'answer_id': 65852, 'author': 'Paweł Hajdan', 'author_id': 9403, 'author_profile': 'https://Stackoverflow.com/users/9403', 'pm_score': 3, 'selected': False, 'text': '<p>Google has excellent testing framework. <a href="https://github.com/google/googletest/blob/master/googletest/docs/primer.md" rel="nofollow noreferrer">https://github.com/google/googletest/blob/master/googletest/docs/primer.md</a></p>\n\n<p>And yes, as far as I see it will work with plain C, i.e. doesn\'t require C++ features (may require C++ compiler, not sure).</p>\n'}, {'answer_id': 65916, 'author': 'Landon Kuhn', 'author_id': 1785, 'author_profile': 'https://Stackoverflow.com/users/1785', 'pm_score': 2, 'selected': False, 'text': '<p>cmockery at <a href="http://code.google.com/p/cmockery/" rel="nofollow noreferrer">http://code.google.com/p/cmockery/</a></p>\n'}, {'answer_id': 65918, 'author': 'Kwondri', 'author_id': 7691, 'author_profile': 'https://Stackoverflow.com/users/7691', 'pm_score': 2, 'selected': False, 'text': '<p>If you are familiar with JUnit then I recommend CppUnit. \n<a href="http://cppunit.sourceforge.net/cppunit-wiki" rel="nofollow noreferrer">http://cppunit.sourceforge.net/cppunit-wiki</a></p>\n\n<p>That is assuming you have c++ compiler to do the unit tests. if not then I have to agree with Adam Rosenfield that check is what you want.</p>\n'}, {'answer_id': 65921, 'author': 'quamrana', 'author_id': 4834, 'author_profile': 'https://Stackoverflow.com/users/4834', 'pm_score': 1, 'selected': False, 'text': '<p>One technique to use is to develop the unit test code with a C++ xUnit framework (and C++ compiler), while maintaining the source for the target system as C modules.</p>\n\n<p>Make sure you regularly compile your C source under your cross-compiler, automatically with your unit tests if possible.</p>\n'}, {'answer_id': 65958, 'author': 'prakash', 'author_id': 123, 'author_profile': 'https://Stackoverflow.com/users/123', 'pm_score': 4, 'selected': False, 'text': '<p>There is <a href="http://cunit.sourceforge.net/" rel="noreferrer"><strong>CUnit</strong></a></p>\n\n<p>And <a href="https://sourceforge.net/projects/embunit/" rel="noreferrer"><strong>Embedded Unit</strong></a> is unit testing framework for Embedded C System. Its design was copied from JUnit and CUnit and more, and then adapted somewhat for Embedded C System. Embedded Unit does not require std C libs. All objects are allocated to const area.</p>\n\n<p>And <a href="http://www.hitex.de/perm/tessy.htm" rel="noreferrer"><strong>Tessy</strong></a> automates the unit testing of embedded software.</p>\n'}, {'answer_id': 65962, 'author': 'Matteo Caprari', 'author_id': 9645, 'author_profile': 'https://Stackoverflow.com/users/9645', 'pm_score': 7, 'selected': False, 'text': '<p><a href="http://www.jera.com/techinfo/jtns/jtn002.html" rel="noreferrer">Minunit</a> is an incredibly simple unit testing framework.\nI\'m using it to unit test c microcontroller code for avr.</p>\n'}, {'answer_id': 66713, 'author': 'Kevin', 'author_id': 6386, 'author_profile': 'https://Stackoverflow.com/users/6386', 'pm_score': 2, 'selected': False, 'text': '<p>First, look here: <a href="http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C</a></p>\n\n<p>My company has a C library our customers use.  We use CxxTest (a C++ unit test library) to  test the code.  CppUnit will also work.  If you\'re stuck in C, I\'d recommend RCUNIT (but CUnit is good too).</p>\n'}, {'answer_id': 67718, 'author': 'Zing-', 'author_id': 8883, 'author_profile': 'https://Stackoverflow.com/users/8883', 'pm_score': 3, 'selected': False, 'text': '<p>I use <a href="http://cxxtest.tigris.org/" rel="nofollow noreferrer">CxxTest</a> for an embedded c/c++ environment (primarily C++).  </p>\n\n<p>I prefer CxxTest because it has a perl/python script to build the test runner.  After a small slope to get it setup (smaller still since you don\'t have to write the test runner), it\'s pretty easy to use (includes samples and useful documentation).  The most work was setting up the \'hardware\' the code accesses so I could unit/module test effectively.  After that it\'s easy to add new unit test cases.</p>\n\n<p>As mentioned previously it is a C/C++ unit test framework.  So you will need a C++ compiler.</p>\n\n<p><a href="http://cxxtest.sourceforge.net/guide.html" rel="nofollow noreferrer">CxxTest User Guide</a>\n<a href="http://cxxtest.com/index.php?title=Main_Page" rel="nofollow noreferrer">CxxTest Wiki</a></p>\n'}, {'answer_id': 69820, 'author': 'Ovid', 'author_id': 8003, 'author_profile': 'https://Stackoverflow.com/users/8003', 'pm_score': 5, 'selected': False, 'text': '<p>You also might want to take a look at <a href="https://github.com/zorgnax/libtap" rel="noreferrer">libtap</a>, a C testing framework which outputs the Test Anything Protocol (TAP) and thus integrates well with a variety of tools coming out for this technology.  It\'s mostly used in the dynamic language world, but it\'s easy to use and becoming very popular.</p>\n\n<p>An example:</p>\n\n<pre><code>#include &lt;tap.h&gt;\n\nint main () {\n    plan(5);\n\n    ok(3 == 3);\n    is("fnord", "eek", "two different strings not that way?");\n    ok(3 &lt;= 8732, "%d &lt;= %d", 3, 8732);\n    like("fnord", "f(yes|no)r*[a-f]$");\n    cmp_ok(3, "&gt;=", 10);\n\n    done_testing();\n}\n</code></pre>\n'}, {'answer_id': 72495, 'author': 'navicore', 'author_id': 7223, 'author_profile': 'https://Stackoverflow.com/users/7223', 'pm_score': 4, 'selected': False, 'text': '<p>I don\'t use a framework, I just use autotools "check" target support.  Implement a "main" and use assert(s).</p>\n\n<p>My test dir Makefile.am(s) look like:</p>\n\n<pre><code>check_PROGRAMS = test_oe_amqp\n\ntest_oe_amqp_SOURCES = test_oe_amqp.c\ntest_oe_amqp_LDADD = -L$(top_builddir)/components/common -loecommon\ntest_oe_amqp_CFLAGS = -I$(top_srcdir)/components/common -static\n\nTESTS = test_oe_amqp\n</code></pre>\n'}, {'answer_id': 73721, 'author': 'Alejandro Bologna', 'author_id': 9263, 'author_profile': 'https://Stackoverflow.com/users/9263', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://github.com/google/cmockery" rel="nofollow noreferrer">Cmockery</a> is a recently launched project that consists on a very simple to use C library for writing unit tests.</p>\n'}, {'answer_id': 75133, 'author': 'Michael Burr', 'author_id': 12711, 'author_profile': 'https://Stackoverflow.com/users/12711', 'pm_score': 5, 'selected': False, 'text': '<p>I\'m currently using the CuTest unit test framework:</p>\n\n<p><a href="http://cutest.sourceforge.net/" rel="noreferrer">http://cutest.sourceforge.net/</a></p>\n\n<p>It\'s ideal for embedded systems as it\'s very lightweight and simple.  I had no problems getting it to work on the target platform as well as on the desktop.  In addition to writing the unit tests, all that\'s required is:</p>\n\n<ul>\n<li>a header file included wherever\nyou\'re calling the CuTest routines</li>\n<li>a single additional \'C\' file to be\ncompiled/linked into the image</li>\n<li>some simple code added to to main to\nset up and call the unit tests - I\njust have this in a special main()\nfunction that gets compiled if\nUNITTEST is defined during the\nbuild.</li>\n</ul>\n\n<p>The system needs to support a heap and some stdio functionality (which not all embedded systems have). But the code is simple enough that you could probably work in alternatives to those requirements if your platform doesn\'t have them.</p>\n\n<p>With some judicious use of extern "C"{} blocks it also supports testing C++ just fine.</p>\n'}, {'answer_id': 194264, 'author': 'Johannes Passing', 'author_id': 4372, 'author_profile': 'https://Stackoverflow.com/users/4372', 'pm_score': 0, 'selected': False, 'text': '<p>In case you are targeting Win32 platforms or NT kernel mode, you should have a look at <a href="http://cfix.sf.net/" rel="nofollow noreferrer">cfix</a>.</p>\n'}, {'answer_id': 317020, 'author': 'Gerhard', 'author_id': 34989, 'author_profile': 'https://Stackoverflow.com/users/34989', 'pm_score': 2, 'selected': False, 'text': '<p>I used <a href="http://rcunit.sourceforge.net/" rel="nofollow noreferrer">RCUNIT</a> to do some unit testing for embedded code on PC before testing on the target. Good hardware interface abstraction is important else endianness and memory mapped registers are going to kill you. </p>\n'}, {'answer_id': 434114, 'author': 'Dushara', 'author_id': 54018, 'author_profile': 'https://Stackoverflow.com/users/54018', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re still on the hunt for test frameworks, <a href="http://code.google.com/p/cunitwin32/" rel="nofollow noreferrer">CUnitWin32</a> is one for the Win32/NT platform.</p>\n\n<p>This solves one fundamental problem that I faced with other testing frameworks. Namely global/static variables are in a deterministic state because each test is executed as a separate process.</p>\n'}, {'answer_id': 1410143, 'author': 'mikelong', 'author_id': 5775, 'author_profile': 'https://Stackoverflow.com/users/5775', 'pm_score': 8, 'selected': False, 'text': '<p>Personally I like the <a href="http://code.google.com/p/googletest/" rel="noreferrer">Google Test framework</a>.  </p>\n\n<p>The real difficulty in testing C code is breaking the dependencies on external modules so you can isolate code in units.  This can be especially problematic when you are trying to get tests around legacy code.  In this case I often find myself using the linker to use stubs functions in tests.</p>\n\n<p>This is what people are referring to when they talk about "<strong>seams</strong>".  In C your only option really is to use the pre-processor or the linker to mock out your dependencies.</p>\n\n<p>A typical test suite in one of my C projects might look like this:</p>\n\n<pre><code>#include "myimplementationfile.c"\n#include &lt;gtest/gtest.h&gt;\n\n// Mock out external dependency on mylogger.o\nvoid Logger_log(...){}\n\nTEST(FactorialTest, Zero) {\n    EXPECT_EQ(1, Factorial(0));\n}\n</code></pre>\n\n<p><strong>Note that you are actually including the C file and not the header file</strong>.  This gives the advantage of access to all the static data members.  Here I mock out my logger (which might be in logger.o and give an empty implementation.  This means that the test file compiles and links independently from the rest of the code base and executes in isolation.</p>\n\n<p>As for cross-compiling the code, for this to work you need good facilities on the target.  I have done this with googletest cross compiled to Linux on a PowerPC architecture.    This makes sense because there you have a full shell and os to gather your results.  For less rich environments (which I classify as anything without a full OS) you should just build and run on the host.  You should do this anyway so you can run the tests automatically as part of the build.</p>\n\n<p>I find testing C++ code is generally much easier due to the fact that OO code is in general much less coupled than procedural (of course this depends a lot on coding style).  Also in C++ you can use tricks like dependency injection and method overriding to get seams into code that is otherwise encapsulated.</p>\n\n<p>Michael Feathers has an <a href="https://rads.stackoverflow.com/amzn/click/com/0131177052" rel="noreferrer" rel="nofollow noreferrer">excellent book about testing legacy code</a>.  In one chapter he covers techniques for dealing with non-OO code which I highly recommend.</p>\n\n<p><strong>Edit</strong>: I\'ve written a <a href="http://meekrosoft.wordpress.com/2009/11/09/unit-testing-c-code-with-the-googletest-framework/" rel="noreferrer">blog post</a> about unit testing procedural code, with <a href="http://github.com/meekrosoft/helloc" rel="noreferrer">source available on GitHub</a>.</p>\n\n<p><strong>Edit</strong>: There is a <a href="http://pragprog.com/titles/jgade/test-driven-development-for-embedded-c" rel="noreferrer">new book coming out from the Pragmatic Programmers</a> that specifically addresses unit testing C code which <a href="http://meekrosoft.wordpress.com/2011/03/25/book-review-test-driven-development-for-embedded-c-beta/" rel="noreferrer">I highly recommend</a>.</p>\n'}, {'answer_id': 1412819, 'author': 'Frank Schwieterman', 'author_id': 32203, 'author_profile': 'https://Stackoverflow.com/users/32203', 'pm_score': 4, 'selected': False, 'text': '<p>Michael Feather\'s book "Working Effectively with Legacy Code" presents a lot of techniques specific to unit testing during C development.</p>\n\n<p>There are techniques related to dependency injection that are specific to C which I haven\'t seen anywhere else.</p>\n'}, {'answer_id': 2651710, 'author': 'bongo', 'author_id': 318329, 'author_profile': 'https://Stackoverflow.com/users/318329', 'pm_score': 1, 'selected': False, 'text': '<p>LibU (<a href="http://koanlogic.com/libu" rel="nofollow noreferrer">http://koanlogic.com/libu</a>) has an unit test module that allows explicit test suite/case dependencies, test isolation, parallel execution and a customizable report formatter (default formats are xml and txt).</p>\n\n<p>The library is BSD licensed and contains many other useful modules - networking, debugging, commonly used data structures, configuration, etc. - should you need them in your projects ...</p>\n'}, {'answer_id': 3155228, 'author': 'Keith Nicholas', 'author_id': 10431, 'author_profile': 'https://Stackoverflow.com/users/10431', 'pm_score': 3, 'selected': False, 'text': '<p>other than my obvious bias</p>\n\n<p><a href="http://code.google.com/p/seatest/" rel="noreferrer">http://code.google.com/p/seatest/</a></p>\n\n<p>is a nice simple way to unit test C code. mimics xUnit</p>\n'}, {'answer_id': 3309095, 'author': 'Philippe A.', 'author_id': 393262, 'author_profile': 'https://Stackoverflow.com/users/393262', 'pm_score': 4, 'selected': False, 'text': '<p>I didn\'t get far testing a legacy C application before I started looking for a way to mock functions. I needed mocks badly to isolate the C file I want to test from others. I gave cmock a try and I think I will adopt it.</p>\n\n<p>Cmock scans header files and generates mock functions based on prototypes it finds. Mocks will allow you to test a C file in perfect isolation. All you will have to do is to link your test file with mocks instead of your real object files.</p>\n\n<p>Another advantage of cmock is that it will validate parameters passed to mocked functions, and it will let you specify what return value the mocks should provide. This is very useful to test different flows of execution in your functions.</p>\n\n<p>Tests consist of the typical testA(), testB() functions in which you build expectations, call functions to test and check asserts.</p>\n\n<p>The last step is to generate a runner for your tests with unity. Cmock is tied to the unity test framework. Unity is as easy to learn as any other unit test framework.</p>\n\n<p>Well worth a try and quite easy to grasp:</p>\n\n<p><a href="http://sourceforge.net/apps/trac/cmock/wiki" rel="noreferrer">http://sourceforge.net/apps/trac/cmock/wiki</a></p>\n\n<p><strong>Update 1</strong></p>\n\n<p>Another framework I am investigating is Cmockery.</p>\n\n<p><a href="http://code.google.com/p/cmockery/" rel="noreferrer">http://code.google.com/p/cmockery/</a></p>\n\n<p>It is a pure C framework supporting unit testing and mocking. It has no dependency on ruby (contrary to Cmock) and it has very little dependency on external libs.</p>\n\n<p>It requires a bit more manual work to setup mocks because it does no code generation. That does not represent a lot of work for an existing project since prototypes won\'t change much: once you have your mocks, you won\'t need to change them for a while (this is my case). Extra typing provides complete control of mocks. If there is something you don\'t like, you simply change your mock.</p>\n\n<p>No need of a special test runner. You only need need to create an array of tests and pass it to a run_tests function. A bit more manual work here too but I definitely like the idea of a self-contained autonomous framework.</p>\n\n<p>Plus it contains some nifty C tricks I didn\'t know. </p>\n\n<p>Overall Cmockery needs a bit more understanding of mocks to get started. Examples should help you overcome this. It looks like it can do the job with simpler mechanics. </p>\n'}, {'answer_id': 3898649, 'author': 'Tony Bai', 'author_id': 207003, 'author_profile': 'https://Stackoverflow.com/users/207003', 'pm_score': 2, 'selected': False, 'text': '<p>try lcut! - <a href="http://code.google.com/p/lcut" rel="nofollow">http://code.google.com/p/lcut</a></p>\n'}, {'answer_id': 4794945, 'author': 'linuxbuild', 'author_id': 241776, 'author_profile': 'https://Stackoverflow.com/users/241776', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://lvc.github.io/api-sanity-checker/" rel="nofollow noreferrer">API Sanity Checker</a> — test framework for C/C++ libraries:</p>\n<blockquote>\n<p>An automatic generator of basic unit tests for a shared C/C++ library. It is able to generate reasonable (in most, but unfortunately not all, cases) input data for parameters and compose simple (&quot;sanity&quot; or &quot;shallow&quot;-quality) test cases for every function in the API through the analysis of declarations in header files.</p>\n<p>The quality of generated tests allows to check absence of critical errors in simple use cases. The tool is able to build and execute generated tests and detect crashes (segfaults), aborts, all kinds of emitted signals, non-zero program return code and program hanging.</p>\n</blockquote>\n<p>Examples:</p>\n<ul>\n<li><a href="http://upstream-tracker.org/tests/fontconfig/2.8.0/view_tests.html" rel="nofollow noreferrer">Test suite</a> for fontconfig 2.8.0</li>\n<li><a href="http://upstream-tracker.org/tests/freetype2/2.4.8/view_tests.html" rel="nofollow noreferrer">Test suite</a> for FreeType 2.4.8</li>\n</ul>\n'}, {'answer_id': 5046305, 'author': 'Roberto Vargas Caballero', 'author_id': 623747, 'author_profile': 'https://Stackoverflow.com/users/623747', 'pm_score': 3, 'selected': False, 'text': '<p>After reading Minunit I thought a better way was base the test in assert macro which I use a lot like defensive program technique. So I used the same idea of Minunit mixed with standard assert. You can see my framework (a good name could be NoMinunit) in <a href="http://www.shike2.com/blog/2010/12/written-unit-test-using-c.html" rel="noreferrer">k0ga\'s blog</a></p>\n'}, {'answer_id': 6270108, 'author': 'ratkok', 'author_id': 326954, 'author_profile': 'https://Stackoverflow.com/users/326954', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.cpputest.org/" rel="noreferrer">CppUTest</a> - Highly recommended framework for unit testing C code. </p>\n\n<p>The examples in the book that is mentioned in this thread <a href="http://pragprog.com/titles/jgade/test-driven-development-for-embedded-c" rel="noreferrer">TDD for embedded C</a> are written using CppUTest. </p>\n'}, {'answer_id': 7700365, 'author': 'chelmertz', 'author_id': 49879, 'author_profile': 'https://Stackoverflow.com/users/49879', 'pm_score': 4, 'selected': False, 'text': '<p>As a C newbie, I found the slides called <a href="http://www.slideshare.net/amritayan/test-driven-development-in-c"><strong>Test driven development in C</strong></a> very helpful. Basically, it uses the standard <code>assert()</code> together with <code>&amp;&amp;</code> to deliver a message, without any external dependencies. If someone is used to a full stack testing framework, this probably won\'t do :)</p>\n'}, {'answer_id': 8047799, 'author': 'Johan', 'author_id': 51425, 'author_profile': 'https://Stackoverflow.com/users/51425', 'pm_score': 6, 'selected': False, 'text': '<p>I say almost the same as ratkok but if you have a embedded twist to the unit tests then...</p>\n<p><a href="http://www.throwtheswitch.org/unity/" rel="noreferrer">Unity</a> - Highly recommended framework for unit testing C code.</p>\n<pre class="lang-c prettyprint-override"><code>#include &lt;unity.h&gt;\n\nvoid test_true_should_be_true(void)\n{\n    TEST_ASSERT_TRUE(true);\n}\n\nint main(void)\n{\n    UNITY_BEGIN();\n    RUN_TEST(test_true_should_be_true);\n    return UNITY_END();\n}\n</code></pre>\n<p>The examples in the book that is mentioned in this thread <a href="http://pragprog.com/titles/jgade/test-driven-development-for-embedded-c" rel="noreferrer">TDD for embedded C</a> are written using Unity (and CppUTest).</p>\n'}, {'answer_id': 14425102, 'author': 'asn', 'author_id': 577774, 'author_profile': 'https://Stackoverflow.com/users/577774', 'pm_score': 5, 'selected': False, 'text': '<p>There is an elegant unit testing framework for C with support for mock objects called <a href="http://cmocka.org/">cmocka</a>. It only requires the standard C library, works on a range of computing platforms (including embedded) and with different compilers.</p>\n\n<p>It also has support for different message output formats like Subunit, Test Anything Protocol and jUnit XML reports.</p>\n\n<p>cmocka has been created to also work on embedded platforms and also has Windows support.</p>\n\n<p>A simple test looks like this:</p>\n\n<pre><code>#include &lt;stdarg.h&gt;\n#include &lt;stddef.h&gt;\n#include &lt;setjmp.h&gt;\n#include &lt;cmocka.h&gt;\n\n/* A test case that does nothing and succeeds. */\nstatic void null_test_success(void **state) {\n    (void) state; /* unused */\n}\n\nint main(void) {\n    const struct CMUnitTest tests[] = {\n        cmocka_unit_test(null_test_success),\n    };\n    return cmocka_run_group_tests(tests, NULL, NULL);\n}\n</code></pre>\n\n<p>The <a href="http://api.cmocka.org/">API</a> is fully documented and several examples are part of the source code.</p>\n\n<p>To get started with cmocka you should read the article on LWN.net: <a href="https://lwn.net/Articles/558106/">Unit testing with mock objects in C</a></p>\n\n<p>cmocka 1.0 has been released February 2015.</p>\n'}, {'answer_id': 14461267, 'author': 'Kris', 'author_id': 266956, 'author_profile': 'https://Stackoverflow.com/users/266956', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m surprised that no one mentioned <a href="http://cutter.sourceforge.net/" rel="nofollow">Cutter (http://cutter.sourceforge.net/)</a>\nYou can test C and C++, it seamlessly integrates with autotools and has a really nice tutorial available.</p>\n'}, {'answer_id': 26183598, 'author': 'Tuplanolla', 'author_id': 1729079, 'author_profile': 'https://Stackoverflow.com/users/1729079', 'pm_score': 4, 'selected': False, 'text': '<p>We wrote <a href="http://users.jyu.fi/~sapekiis/cheat/">CHEAT</a> (hosted on <a href="http://github.com/Tuplanolla/cheat">GitHub</a>) for easy usability and portability.</p>\n\n<p>It has no dependencies and requires no installation or configuration.\nOnly a header file and a test case is needed.</p>\n\n<pre><code>#include &lt;cheat.h&gt;\n\nCHEAT_TEST(mathematics_still_work,\n    cheat_assert(2 + 2 == 4);\n    cheat_assert_not(2 + 2 == 5);\n)\n</code></pre>\n\n<p>Tests compile into an executable that takes care of running the tests and reporting their outcomes.</p>\n\n<pre><code>$ gcc -I . tests.c\n$ ./a.out\n..\n---\n2 successful of 2 run\nSUCCESS\n</code></pre>\n\n<p>It has pretty colors too.</p>\n'}, {'answer_id': 27771116, 'author': 'kirbyfan64sos', 'author_id': 2097780, 'author_profile': 'https://Stackoverflow.com/users/2097780', 'pm_score': 0, 'selected': False, 'text': '<p>I just wrote <a href="https://github.com/kirbyfan64/libcut" rel="nofollow">Libcut</a> out of frustration with existing C unit testing libraries. It has automatic type stringing of primitives (no need for test_eq_int, test_eq_long, test_eq_short, etc...; only two different sets for primitives and strings) and consists of one header file. Here\'s a short example:</p>\n\n<pre><code>#include &lt;libcut.h&gt;\n\nLIBCUT_TEST(test_abc) {\n    LIBCUT_TEST_EQ(1, 1);\n    LIBCUT_TEST_NE(1, 0);\n    LIBCUT_TEST_STREQ("abc", "abc");\n    LIBCUT_TEST_STRNE("abc", "def");\n}\n\nLIBCUT_MAIN(test_abc);\n</code></pre>\n\n<p>It works only with C11, though.</p>\n'}]
['https://Stackoverflow.com/questions/65820', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7049/']