单元测试扫描
Unit Test Scanf
(对不起我的英语)我正试图从c++代码进行单元测试。在下一个函数(菜单)中,()中没有任何参数。但是在这个函数中有一个扫描器,我想测试一下,但是我不知道怎么做。
我可以从单元测试中测试扫描吗?
谢谢。
函数代码:
void principal::menu()
{
int choice;
system("cls");
printf("n--------MENU--------");
printf("n1 : Jugador X");
printf("n2 : Jugador O");
printf("n3 : Sortir");
printf("nTria el tipus de jugador: ");
scanf_s("%d", &choice);
turn = 1;
switch (choice)
{
case 1:
player = 1;
comp = 0;
player_first();
break;
case 2:
player = 0;
comp = 1;
start_game();
break;
case 3:
exit(1);
default:
menu();
}
}
代码来自test:
...
TEST_METHOD(menu){
principal p; //this is the class --> not matter now
//test code
}
如果代码有参数,我使用下一个代码:
...
TEST_METHOD(menu){
principal p; //this is the class --> not matter now
//test code
Assert::AreEqual(result, parameter to enter);
}
如果你使用c++函数cin和cout,你可以重定向缓冲区。
。
#include <iostream>
#include <sstream>
#include <random>
#include <ctime>
int main()
{
auto cout_buf = std::cout.rdbuf();
std::stringbuf sb;
std::cin.rdbuf(&sb);
std::cout.rdbuf(&sb);
std::mt19937 rand(time(nullptr));
std::cout << (rand() % 100);
int number;
std::cin >> number;
std::cout.rdbuf(cout_buf);
std::cout << "The number was " << number << std::endl;
return 0;
}
相关文章:
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- VC++本机单元测试,找不到调试符号
- 用于交叉编译和CMake的预处理器宏的单元测试
- C++ 用于单元测试的模板模板
- 提升 1.64 单元测试编译失败
- 单元测试欧拉到四元数实现失败
- 运行 C++ 单元测试时LNK2005链接错误
- 禁用自动捕获 Googletest 单元测试中的C++异常
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- 使用 Google Test 对自定义断言函数进行单元测试
- 如何将我的 CMake 项目配置为运行所有单元测试?
- 在Qt C++单元测试中动态加载QQuickWindow而不是QQuickWidget
- MS 本机单元测试 - 断言::线程失败不起作用
- 如何获取 CMake 单元测试的相对路径?
- QTimer 超时不会在单元测试中触发
- 如何在提升构建中设置环境变量以进行提升单元测试框架?
- 在 AtMega32a 上运行的C++的单元测试
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- 单元测试扫描