Click here to Skip to main content
15,880,891 members
Articles / Programming Languages / C++

ShortCUT - A Short C++ Unit Testing Framework

Rate me:
Please Sign up or sign in to vote.
4.60/5 (9 votes)
15 Feb 2007CPOL7 min read 65.4K   948   44  
A very simple, customizable unit-testing framework for C++ developers
#ifndef _UNITTEST_H_
#define _UNITTEST_H_

// A basic test suite with a 'resource' for the tests to use
struct MySuite1 : TestSuite
{
    MySuite1() : resource(0) {}
    virtual void setup()    { resource = 1; }
    virtual void teardown() { resource = 0; }
    int resource;
};

// A test case in plain ole C++
struct MySuite1Test1 : TestCase
{
    const char* name() { return "Test case 1"; }
    void test(TestSuite* suite) 
    {
        MySuite1* data = static_cast<MySuite1*>(suite);
        data->resource++;
        // throw TestMessageException("manual exception");
    }
};

// A test case that uses some helper macros
struct MySuite1Test2 : TestCase
{
    T_NAME("Test case 2")
    void test(TestSuite* suite) 
    {
        T_DATA(data, MySuite1, suite);
        data->resource++;
        T_ASSERT(1 == 0); 
    }
};

// A test case with macros turned up to 11
T_TEST(MySuite1, MySuite1Test3)
{
    data->resource++;
    T_ASSERT(1 == 1); 
}

#endif _UNITTEST_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I work as a developer in the Seattle area. I am currently residing in France.

Comments and Discussions