gpt4 book ai didi

c++ - 如何在 C++ 中编写单元测试?

转载 作者:行者123 更新时间:2023-12-02 09:51:14 25 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

3年前关闭。




Improve this question




我从未为我的 C++ 程序编写过单元测试或任何测试。我只知道它们是为了测试函数/程序/单元是否完全按照您的想法执行,但我不知道如何编写。
有人可以帮我测试我的示例函数吗?测试框架是什么意思?我是为代码的每个功能和所有分支编写测试,还是只为我认为可能棘手的功能编写测试?

doMode(int i) {

int a = fromString<int>(Action[i][1]);
int b = fromString<int>(Action[i][2]);

std::cout << "Parameter:\t" << a << "\t" << b << "\t" << std::endl;
Sleep(200);

return;
}
编辑:
我不是要一个框架。或者更好:这可能与我的问题有关。我只是不知道从哪里开始以及如何开始。我必须使用的语法是什么?是否因我使用的框架而异?

最佳答案

这就是您在没有框架的情况下编写单元测试的方式。

#include <iostream>

// Function to test
bool function1(int a) {
return a > 5;
}

// If parameter is not true, test fails
// This check function would be provided by the test framework
#define IS_TRUE(x) { if (!(x)) std::cout << __FUNCTION__ << " failed on line " << __LINE__ << std::endl; }

// Test for function1()
// You would need to write these even when using a framework
void test_function1()
{
IS_TRUE(!function1(0));
IS_TRUE(!function1(5));
IS_TRUE(function1(10));
}

int main(void) {
// Call all tests. Using a test framework would simplify this.
test_function1();
}

关于c++ - 如何在 C++ 中编写单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com