Post Build Event for CxxTest in Visual Studio 2005

来源:互联网 发布:windows posready 7 编辑:程序博客网 时间:2024/05/21 17:35

I'm trying to write some unit tests for my c++ code in VS 2005. It is hard to use CppUnit. And CxxTest seems clean, simple and easy to use. It's non invasive. So I recommend CxxTest personally.

 

If you use VC6, there is a sample project for VC6 on the package of cxxtest-3.10.1.tar.gz.

Unfortunately, I use VS 2005, It's not so easy to integrate CxxTest in my project. I've been working on it about a noon and a night. Finally it works. My solution is just add a post build scripts to my project.

 

The requirement:

1. my project is a console project, and testcases(such as MyTestSuite.h) is in the same project. CxxTest cases would be run in executables.  I compile and link the Cxx Tester in post build scripts.

 

2. the post event would call CxxTest perl script to generate code and executable to run test cases. (generated code is in runner.cpp, and the executable file to run cases is run.exe).

 

3. So the project has 2 main functions, one is the main output, another is in the generated code. It needs bit trick to use #ifndef ... #endif around the main function to avoid compiling error.

 

 

(PS, firstly I added PERL variable to c:/perl/bin/perl.exe and added "E:/Program Files/cxxtest" (where cxxtest is extracted) to my PATH variable, so I can invoke cxxtestgen.pl from command line directly, of course perl is installed.  And I wrote my test cases in file MyTestSuite.h. Here's the Scripts in Post Build Event, attention these bold text are added based on the original compiling/linking command when I changed the project output as DLL:

 

rem post build for VS.Net 2005 :

echo ##############generating runner.cpp##############
cxxtestgen.pl --error-printer -o runner.cpp $(ProjectDir)/*.h
sleep 1

echo ##############compiling runner.exe##############
cl.exe /D "_UNICODE" /D "UNICODE" /I "E:/Program Files/cxxtest" /I "E:/workspace/SICExtension/SICExt" /I "include" /I"."  /FD /EHsc /MT  -o ../release/runner.exe runner.cpp *.cpp Nafxcw.lib Libcmt.lib /link /NODEFAULTLIB:Nafxcw.lib /NODEFAULTLIB:Libcmt.lib /LIBPATH:"lib" /PDB:"e:/workspace/SICExtension/release/runner.pdb"
../release/runner.exe

 

 

After added the post build scripts and the test suites, each time you build the project,

it will run the test, when all test cases pass, it shows like this:

 

1>Running 3 tests
1>setup
1>tearDown!
1>.
1>setup
1>tearDown!
1>.
1>setup
1>tearDown!
1>.OK!

 

When some tests fail, it shows like this:

1>Running 3 tests
1>setup
1>tearDown!
1>.
1>setup
1>tearDown!
1>.
1>setup
1>In MyTestSuite::testRegextMatch:
1>e:/workspace/SICExtension/SICExt/MyTestSuite.h:38: Error: Expected (1 == RESULT), found (1 != 0)
1>tearDown!
1>Failed 1 of 3 tests
1>Success rate: 66%

 

Here's MyTestSuite.h where 3 cases are added to test Addition, Multiplication, and matchRegex, ohhh, only testRegextMatch is real case for my project, it test matchRegex(string, regularExp) which match a string against regular expression.

 

#include <cxxtest/TestSuite.h>
#include <stdio.h>
#include <stdlib.h>
#include "SICExt.h"

class MyTestSuite : public CxxTest::TestSuite
{
private:
    boost::regex reg;

public:
    void setUp() {
        printf("/n");
        printf( "setup /n" );
        reg =  regex("[0-9]{4}");
    }

    void tearDown() { printf("tearDown!/n"); }

    void testAddition( void )
    {
        TS_ASSERT( 1 + 1 > 1 );
        TS_ASSERT_EQUALS( 1 + 1, 2 );
    }

    void testMultiplication( void )
    {
        TS_ASSERT_EQUALS( 2 * 2, 4 );
    }

    void testRegextMatch(void)  // test regext
    {
        std::string regstr = "a+";
        boost::regex expression(regstr);
        std::string testString = "aaa";

        BOOL RESULT = matchRegex(testString,expression);
         TS_ASSERT_EQUALS(TRUE, RESULT);
    }
};

原创粉丝点击