Post Build Event for CxxTest in Visual Studio 2005

12 篇文章 7 订阅
8 篇文章 0 订阅

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);
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值