非常简单的测试库
Extremely simple test library
是否存在具有友好许可证的易于嵌入的C++测试库?我想要一个单头文件。没有.cpp
文件,没有5 PB的include。因此,CppUnit和Boost.Test
都已退出。
基本上,我只想把单个文件放到项目树中,包括它,并能够编写
testEqual(a,b)
看看它是否失败。我会使用assert
,但它在非调试模式下不工作,也不能打印a
和b
的值,在重写assert
之前,我宁愿搜索现有的库。
我很想说"自己写",这就是我所做的。另一方面,您可能希望重用我所写的内容:test_util.hpp和test_util.cpp。将cpp文件中的一个定义内联到hpp文件中很简单。麻省理工学院。我也把它粘贴到下面的答案中。
这可以让您编写这样的测试文件:
#include "test_util.hpp"
bool test_one() {
bool ok = true;
CHECK_EQUAL(1, 1);
return ok;
}
int main() {
bool ok = true;
ok &= test_one();
// Alternatively, if you want better error reporting:
ok &= EXEC(test_one);
// ...
return ok ? 0 : 1;
}
在测试目录中浏览以获得更多灵感。
// By Magnus Hoff, from http://stackoverflow.com/a/9964394
#ifndef TEST_UTIL_HPP
#define TEST_UTIL_HPP
#include <iostream>
// The error messages are formatted like GCC's error messages, to allow an IDE
// to pick them up as error messages.
#define REPORT(msg)
std::cerr << __FILE__ << ':' << __LINE__ << ": error: " msg << std::endl;
#define CHECK_EQUAL(a, b)
if ((a) != (b)) {
REPORT(
"Failed test: " #a " == " #b " "
"(" << (a) << " != " << (b) << ')'
)
ok = false;
}
static bool execute(bool(*f)(), const char* f_name) {
bool result = f();
if (!result) {
std::cerr << "Test failed: " << f_name << std::endl;
}
return result;
}
#define EXEC(f) execute(f, #f)
#endif // TEST_UTIL_HPP
尝试谷歌测试https://github.com/google/googletest/
它真的很轻,跨平台和简单。
相关文章:
- Cmake无法在qt-creater/collect2上编译简单的测试程序:错误:ld
- 为什么这个简单的 C++ SIMD 基准测试在使用 SIMD 指令时运行速度较慢?
- 模型测试 + 简单表模式 = 父测试失败
- 多个 if-else 测试的更简单方法
- 简单的 for() 循环基准测试在任何循环绑定下花费相同的时间
- CMake 和 Visual Studio 2015 - C 编译器 cl.exe 无法编译简单的测试程序
- Centos6 gcc6:编译简单的 c++11 测试文件时,默认的 ABI 不拾取
- C++编译器 g++.exe 无法编译简单的测试程序 - 确定 CXX 编译器是否工作失败
- 为什么我不能编译这个简单的线程测试?
- 测试对象是什么类的简单方法
- CMake 在尝试编译简单测试程序时失败
- 无法运行简单的 std::async 和 std::future 测试程序。错误:"has initializer but incomplete type."发生了什么?
- NAS(使用 qt/linux)- 无法建立简单的音频服务器连接测试
- C 编译器无法编译简单的测试程序
- CL.EXE无法编译简单的测试程序VS2017
- 用谷歌基准测试简单代码的问题
- 简单的MPI_GATHER测试带有memcpy错误
- c++和gtest需要帮助理解为什么看似简单的测试失败了
- 使用 VS2010 MFC + 测试应用的简单 DLL
- 如何测试简单的 Cpp 文件