首页 > 解决方案 > unity C unittest 框架可以用来对 C++ 代码进行单元测试吗?

问题描述

我正在使用统一框架在 AtMega32A 上对我的 C 代码进行单元测试。这很好用。现在我想知道我是否可以以某种方式欺骗统一来测试我的 C++ 代码。

我制作了一个小的概念证明程序,它通过一个公共函数公开了 C++ 类的所有成员。现在我想在该公共函数中放置一些断言宏。该程序可以在这里找到: https ://github.com/cdwijs/cpp_unit_test

在我的主要我有以下几点:

int main(void)
{
    UNITY_BEGIN();
    //RUN_TEST(myPrivate.executeTest); //unity_internals.h(707,65): error: invalid use of non-static member function
    //RUN_TEST((void*)myPrivate.executeTest); //error: invalid use of member function (did you forget the '()' ?)
    //RUN_TEST((func)))myPrivate.executeTest); //error: 'func' was not declared in this scope
    //RUN_TEST(&myPrivate.executeTest); //error: cannot convert 'void (Private::*)()' to 'UnityTestFunction {aka void (*)()}' for argument '1' to 'void UnityDefaultTestRun(UnityTestFunction, const char*, int)'
    UNITY_END();

    myPrivate.more();
    myPrivate.more();
    myPrivate.less();

    /* Replace with your application code */
    while (1)
    {
    }
}

在 Private_test.cpp 我有以下内容:

#include "private.h"
#include "unity.h"

void Private::executeTest (void)
{
    myNumber = 3;
    TEST_ASSERT_EQUAL_INT(3,myNumber);
    more();
    TEST_ASSERT_EQUAL_INT(4,myNumber);
}

我不知道如何运行测试功能。有任何想法吗?我是在寻找正确的方向,还是应该使用专门针对 C++ 的测试框架?

标签: c++cunit-testingavrunity-test-framework

解决方案


推荐阅读