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?
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.
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.
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.
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.
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...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With