Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you use to unit test C code? [duplicate]

Tags:

c

unit-testing

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.

like image 726
Mitchell Avatar asked Jan 29 '09 22:01

Mitchell


People also ask

What is a unit test in C++?

A unit is the smallest testable part of an application. Unit tests assess code in isolation. In C++ this means writing tests for methods or functions. Tests only examine code within a single object.

How do I unit test my code?

To unit test your code, you need a unit testing framework. A unit testing framework is a library of classes and methods that help you write unit tests. There are many unit testing frameworks available, but the most popular ones for C# development are: xUnit – xUnit is a unit test framework for C#.

Why write unit tests for code?

Writing unit tests for code means writing code that can be broken down into discrete units. Testable code is modular because discretely tested units are loosely coupled. If this reason for unit testing, combined with the previous one, reads like an advertisement for test-driven development (TDD), that’s because they are.

What is an example of unit testing?

The most basic unit test verifies that the code behaves as expected when given specific inputs. For example, you might have a unit test that checks if a function returns the correct value when given an input string. More complex unit tests can verify that several code units work together as intended. What is a unit testing framework?


1 Answers

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

like image 132
James Grenning Avatar answered Sep 22 '22 10:09

James Grenning