CppUnit编译与入门

什么是CppUnit?

CppUnit是xUnit家族一员,xUnit是一种测试框架,最早在smalltalk上实现,后来被广泛的在各种语言上实现,除了CppUnit还有NUnit(C#版本)phpUnit(php版本我还用过),和CppUnit类似的还有gtest(google搞的C++测试工具)cmockery (C语言测试工具,也是google的)CppUnit以源码方式发布,所以想要使用这个工具需要先下载再编译,Windows下的CppUnit还带了一个MFC项目,以图形化的形式报告编译情况


如何编译:

0 : 编译环境 windows7, vs2010

1 :下载源码包 最新的版本是1.21.1(2008年最后一次更新)

地址 :http://sourceforge.net/projects/cppunit/ 或者直接点击 http://sourceforge.net/projects/cppunit/files/cppunit/1.12.1/cppunit-1.12.1.tar.gz/download

2 : 复制cppunt-1.12.1到c:\解压得到目录C:\cppunit-1.12.1

3 : 进入C:\cppunit-1.12.1\src 用VS2010打开CppUnitLibraries.dsw提示转换,转换之

4 : 在项目列表看见一堆项目,默认是以Debug的方式编译,如以该方式编译需要修改目标文件名

cppunit                $(ProjectName)   ->   $(ProjectName)d
cppunit_dll           $(ProjectName)   ->   cppunitd_dll
DllPlugInTester     $(ProjectName)   ->   $(ProjectName)d_dll
TestPlugInRunner $(ProjectName)   ->   $(ProjectName)d
TestRunner          $(ProjectName)  ->   $(ProjectName)d

需要修改目标文件名的原因是上述项目都设置了生成事件,在生成以后都会把生成的文件复制到lib目录下,个人判断d应该是代表了测试版的意思,如果是Release版本生成的目标文件都是项目名不需要修改

5 : 修改DSPlugIn入口
属性,配置属性,链接器,高级,无入口点 设置是

6 : 修改无法加载类型库(编译TestPlugInRunner,TestRunner)会报错其中7.0修改为8.0
#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("7.0") lcid("0") raw_interfaces_only named_guids

以上设置都全做完以后就可以整体编译解决方案了


 

测试方法:

1. 添加一个命令行项目,导入CppUnit库,编写testCase 调用看输出

2. 编写一个动态链接库项目,导入CppUnit库 , 编写testCase 把文件路径作为参数 调用 C:\cppunit-1.12.1\lib\DllPlugInTesterd_dll.exe 即所谓的plugIn的方式


第一个例子:

 

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>

class FirstTestCase : public CPPUNIT_NS::TestFixture
{
    //Macros , it's need HelperMacros.h
    CPPUNIT_TEST_SUITE(FirstTestCase);
    CPPUNIT_TEST(testHello);
    CPPUNIT_TEST_SUITE_END();

public:

    void setUp()
    {}

    void testHello()
    {
        CPPUNIT_ASSERT(1 == 1);
    }
};

//Macros, it'must , TestFactoryRegistry use it
CPPUNIT_TEST_SUITE_REGISTRATION( FirstTestCase );

int main()
{
    // Create the event manager and test controller
    CPPUNIT_NS::TestResult controller;

    // Add a listener that colllects test result
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener( &result );        

    // Add a listener that print dots as test run.
    CPPUNIT_NS::BriefTestProgressListener progress;
    controller.addListener( &progress );      

    // Add the top suite to the test runner
    CPPUNIT_NS::TestRunner runner;
    runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
    runner.run( controller );

    // Print test in a compiler compatible format.
    CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
    outputter.write(); 

    return result.wasSuccessful() ? 0 : 1;
}

 

新建一个C++语言的命令行程序,在项目里指定CppUnit的头文件和库目录 (打开项目属性,配置属性,VC++目录,找到包含目录和库目录,分别添加C:\cppunit-1.12.1\include;C:\cppunit-1.12.1\lib;)然后再添加一个TestMain.cpp 输入上面的源码,编译运行即可(说起来我已经全面开始接受骆驼命名法了,文件名是大驼峰)

CppUnit编译与入门_第1张图片

下面是测试通过和没有通过的输出

CppUnit编译与入门_第2张图片

你可能感兴趣的:(cpp)