CPPunit 1.13.2 + vs2013 配置与使用

1. 前言

最近越来越发现测试的重要性, 我们在做头条的测试题的时候, 发现思路是正确的, 不过提交的时候总是存在各种各样的bug, 于是我们想, 如果能够找到一个比较方便的测试工具, 该多好, 于是我们想到了 cppunit。

2. 下载与配置

2.1 下载

我们首先找到cppunit 的官方站点 https://www.freedesktop.org/wiki/Software/cppunit/
发现这个一个git 存储的, 使用官方说明的git指令进行 download

git clone git://anongit.freedesktop.org/git/libreoffice/cppunit/

下载完毕之后文档结构是这样的:
这里写图片描述

2.2 编译

此时这个cppunit 还是无法直接使用的, 因为他还没有被编译!!!
由于我们是在windows 上面结合 vs 2013 进行编译配置, 没办法直接用类似linux 上的方法方便的编译处理。

=============【INSTALL-WIN32.txt】===================
Building:
* Open the src/CppUnitLibraries.dsw workspace in VC++.
* In the ‘Build’ menu, select ‘Batch Build…’
* In the batch build dialog, select all projects and press the build button.
* The resulting libraries can be found in the lib/ directory.

他告诉我们, 可以直接打开src/CppUnitLibraries.dsw, 进行编译。
然而, 在使用vs 2013 编译的过程中遇到各种坑。
这里写图片描述

这里主要问题是连接器中设置的输出文件名和默认的输出文件路径中的文件名不符合
这里写图片描述
这里写图片描述

我们这里, 非常蛋疼的将每个对应的工程的这两个项做了统一, 一般的release 版本基本上是木有问题的, 相应的debug 版本只需要在 目标文件名中(如上图所示) 后面加一个 d。 而对于 unicode 版本需要额外的加 u。 对于特殊的以 dll 结尾的, 就得直接写输出文件名了。

最终, 除了testpluginrunner 项目中 的dubug 和 release 还是报错外, 其余都编译成功
如图所示:
这里写图片描述

2.3 使用

由于我们主要还是用来处理leetcode上面的题目, 所以可以直接参考simple 项目
下面给出我们的配置文件
solution.h
编写 oj 上面的实现函数, 一般直接替换Solution 内部的内容就好了

#pragma once

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<vector<int>> res;
        vector<int> tmp;
        helper(res, tmp, candidates, target, 0);
        return res;
    }

private:
    void helper(vector<vector<int>> & res, vector<int> & cur_set, const vector<int>& candidates, int target, int depth){
        if (target == 0){
            res.push_back(cur_set);
            return;
        }

        if (target < 0 || depth == candidates.size())
            return;

        helper(res, cur_set, candidates, target, depth + 1);
        cur_set.push_back(candidates[depth]);
        helper(res, cur_set, candidates, target - candidates[depth], depth);
        cur_set.pop_back();
    }
};

test.h
// 固定写法

#pragma once

#include "cppunit/extensions/HelperMacros.h"

class Test : public CPPUNIT_NS::TestFixture
{
    CPPUNIT_TEST_SUITE(Test);
    CPPUNIT_TEST(testSolution);
    CPPUNIT_TEST_SUITE_END();

public:
    void testSolution();
};

test.cpp
// 这里的testSolution 用来写测试用例

#include "stdafx.h"

#include <cppunit/config/SourcePrefix.h>
#include "Test.h"
#include "solution.h"

CPPUNIT_TEST_SUITE_REGISTRATION(Test);

void Test::testSolution()
{
    Solution s;

    auto res = s.combinationSum(vector<int>{2, 3, 6, 7}, 7);
    vector<vector<int>> std_res{ { 7 }, { 2, 2, 3 } };

    for (int i = 0; i != std_res.size(); i++){
        for (int j = 0; j != std_res[i].size(); j++)
            CPPUNIT_ASSERT_EQUAL(res[i][j], std_res[i][j]);
    }

}

main.cpp
// 固定写法

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.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>

#pragma comment(lib, "cppunitd.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    CPPUNIT_NS::TestResult controller;
    CPPUNIT_NS::TestResultCollector result;
    controller.addListener(&result);

    CPPUNIT_NS::BriefTestProgressListener progress;
    controller.addListener(&progress);

    CPPUNIT_NS::TestRunner runner;
    runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());
    runner.run(controller);

    CPPUNIT_NS::CompilerOutputter outputter(&result, CPPUNIT_NS::stdCOut());
    outputter.write();

    return 0;
}

2.4 运行效果图

这里写图片描述
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值