Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit tests in C++

I'm doing a project in C++ in my University and we need to unit test our classes. The tests are pretty straight-forward - we don't have any "problematic" classes that deal with databases, GUI, web stuff, etc. It's just a command line program.

What is a good unit-testing framework to use that is as simple as possible? Please provide a short example of a test in that framework.

EDIT: I see there are some answers, so I want to add another question: Where do I put the test methods? Are they declared in a different file? Where would that file be? How do I run all tests?

like image 319
Amir Rachum Avatar asked Dec 27 '10 18:12

Amir Rachum


People also ask

What is a unit test in C?

Unit testing is a method of testing software where individual software components are isolated and tested for correctness. Ideally, these unit tests are able to cover most if not all of the code paths, argument bounds, and failure cases of the software under test.

Does C have unit testing?

A Unit Testing Framework for CCUnit is a lightweight system for writing, administering, and running unit tests in C. It provides C programmers a basic testing functionality with a flexible variety of user interfaces.

What is example unit test?

A unit test is a type of software test that focuses on components of a software product. The purpose is to ensure that each unit of software code works as expected. A unit can be a function, method, module, object, or other entity in an application's source code.

What is unit test method?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.


2 Answers

Boost. Hands down.

#define BOOST_TEST_MODULE my_tests // use once per test program
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( case_x )
{
  ....
  BOOST_CHECK( ... boolean expression ... );
  BOOST_etc...etc...
}
like image 195
Edward Strange Avatar answered Sep 22 '22 23:09

Edward Strange


There're many, quite similar. My prefered one is Boost.Test library. It can be complicated if you need but also extremely simple for simple cases. e.g. the simplest possible case looks like:

#include <boost/test/minimal.hpp>

int add( int i, int j ) { return i+j; }

    int test_main( int, char *[] )             // note the name!
    {
        // six ways to detect and report the same error:
        BOOST_CHECK( add( 2,2 ) == 4 );        // #1 continues on error
        BOOST_REQUIRE( add( 2,2 ) == 4 );      // #2 throws on error
        if( add( 2,2 ) != 4 )
          BOOST_ERROR( "Ouch..." );            // #3 continues on error
        if( add( 2,2 ) != 4 )
          BOOST_FAIL( "Ouch..." );             // #4 throws on error
        if( add( 2,2 ) != 4 ) throw "Oops..."; // #5 throws on error

        return add( 2, 2 ) == 4 ? 0 : 1;       // #6 returns error code
    }

This example uses the Minimal Testing Facility.

like image 20
Andriy Tylychko Avatar answered Sep 18 '22 23:09

Andriy Tylychko