- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要为测试生成临时文件。看起来我不能使用 mkstemp
因为我需要文件名有一个特定的后缀,但我不关心文件名的其余部分。 GTest 中有没有一种方法可以创建一个临时文件来处理文件的创建以及测试结束时的删除。
另一种方法是创建我自己的类来执行此操作。
最佳答案
虽然它不构建临时文件,googletest 提供了两个不同的测试宏,TEST 和 TEST_F,后者是固定的。请参阅 the Primer 上标题为“测试夹具:使用相同的数据配置...”的部分有关灯具的更多信息。
我对这个问题的解决方案是使用带有固定测试的 Boost.Filesystem。我希望能够拥有一个为所有测试共享的命名临时子目录。在这种情况下,我正在调整我的案例以适应 OP 对指定后缀的请求。
包括:
// Boost.Filesystem VERSION 3 required
#include <string>
#include <boost/filesystem.hpp>
测试类定义:
class ArchiveTest : public ::testing::Test {
protected:
boost::filesystem::path mTempFileRel;
boost::filesystem::path mTempFileAbs;
std::ofstream mExampleStream;
ArchiveTest() {
mTempFileRel = boost::filesystem::unique_path("%%%%_%%%%_%%%%_%%%%.your_suffix");
mTempFileAbs = boost::filesystem::temp_directory_path() / mTempFileRel;
mExampleStream.open(mTempFileAbs);
}
~ArchiveTest() {
if(mExampleStream.is_open())
{
mExampleStream.close();
}
}
// Note there are SetUp() and TearDown() that are probably better for
// actually opening/closing in case something throws
};
注意:虽然您可以在构造函数或 SetUp() 中创建文件对象并在析构函数或 TearDown() 中关闭,但我更喜欢在测试中这样做,因为我不使用在所有测试中创建的文件名固定的。因此,在使用流示例时要格外小心。
这是我使用的文件名:
// Tests that an ArchiveFile can be written
TEST_F(ArchiveTest, TestWritingArchive) {
try
{
TheInfo info_; // some metadata for the archive
MyArchive archive_; // Custom class for an archive
archive_.attachToFile(mTempFile, info_);
...
}
catch(const std::exception& e_)
{
FAIL() << "Caught an exception in " << typeid(*this).name()
<< ": " << e_.what();
}
}
如果你对'%'字符感到好奇,来自the reference on unique_path :
The unique_path function generates a path name suitable for creating temporary files, including directories. The name is based on a model that uses the percent sign character to specify replacement by a random hexadecimal digit.
注意事项:
关于c++ - 生成临时文件/文件夹 c++ GTEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38398288/
我在尝试使用 cmake 构建项目时遇到错误。 我在我的电脑上手动下载并安装了 gtest(即/usr/include 中提供了 gtest 头文件)和/usr/lib 中的 gtest、gtest_
我在构建我的项目时遇到以下错误 C:\gtest\gtest-1.6.0\include\gtest/gtest-printers.h(327) : error C2220: warning trea
你好, 我目前正在研究 CMake,以便在工作中使用 ExternalProject_add。因为我想测试 googletest,所以我尝试同时测试两者。我在编译单元测试代码时遇到问题。 所以我就按照
我一直在为这个问题苦苦挣扎一段时间:我在 Windows 10 下使用 CMake 和 GTest,但我得到一个 (Link target) -> LINK : fatal error LNK1104
如果内存不足,C++ 中的 new 关键字将抛出异常,但下面的代码会在 new 失败时尝试返回“NO_MEMORY”。这很糟糕,因为它会引发 std::bad_alloc 异常。 我正在写一个单元测试
我正在尝试运行一些Google测试,并且在每个测试装置中都有很多代码需要重复,因此我想使代码尽可能简短,我想使用的子类的SetUp方法Testing::test父类,但是TEST_F固定装置无法识别S
我正在尝试使用 gtest 类型参数化来确保我的类的基本功能。但是我被困在测试设置中。以下是示例: 我有两个名为 CatTest 和 DogTest 的类,以及一个名为 AnimalTest 的基类,
我想应用参数化测试并具有以下夹具: class MyTest: public ::testing::TestWithParam {...}; 我想设置两个参数化测试用例,其中一个小参数失败,但大参数成
如果我使用 g(oogle)Test,这些像 FRIEND_TEST(Test, Proc) 这样的小谷歌宏在我的代码中随处可见。是否有自动机制将它们从构建中排除,或者我真的必须用 #ifdefs 包
我有一个项目,我添加了一个 gtest 项目,然后将原始项目转换为 .lib 项目,并从第三个新项目调用其 main() 函数。这样,我就可以用项目 .exe 构建测试 .exe。除了,它不起作用。
注意:我知道这个问题以不同的方式被问到,但我无法根据我的限制解决它: 我正在使用谷歌测试版 1.7 使用平台工具集 V100 在 Visual Studio 2010 中编译 我正在为我创建的库编写
我的项目中有一个目录树: /project /build /src main.cpp student.cpp /include
在我们所知的 gtets 中,当控件找到 TEST 或 TEST_F 函数时,它会注册 测试用例 进入 gtest。但是按照我的要求,gtest注册完所有的测试用例之后我需要搜索 testcasena
我有一大套单元测试和一些集成测试是用谷歌测试框架或 gtest 实现的。 由于没有标记,我使用禁用约定将测试分成组或在它们前面加上 GROUPA_、GROUPB_ 等前缀。 这很好用。我可以过滤不同的
我想混合参数化测试和类型化测试。这是我的尝试: struct X {}; struct Y {}; template struct MyTestFixture: public ::testing::
我已阅读 this official document ,了解如何进行二进制比较和字符串比较。 ASSERT_EQ 和 ASSERT_STREQ 在数组比较情况下无法工作。 例如 li@li:~/po
我正在测试我的功能是否正确运行 bool Core::IsMeta(void) { return mProc->GetCode(mPC)->Meta; } 使用说明 EXPECT_EQ(true,
我想用 gtest 测试一个模板类。我在 Google Test manual 中阅读了有关 TYPED_TEST 的信息并查看了official example他们引用了,但我仍然无法全神贯注地获取
这是我的: /* Can't change 'base' struct. */ struct base { public: template void printVal() {
我想要等于 2 个对象,正好是卡片(使用 gtest 进行单元测试)。这是我的代码: #include "stdafx.h" #include #include class Card { p
我是一名优秀的程序员,十分优秀!