如何在本地c++单元测试(Visual Studio)中打开项目中的文件
How to open a file from the project in a native C++ unit test (Visual Studio)?
我在Visual Studio(2012)中有一个本地c++单元测试项目。
在我的一个测试中,我想读取包含在我的单元测试项目中的文件。这可能吗?我应该设置文件的哪些属性?我应该使用什么路径?
我在我的项目中添加了一个test.txt文件(并试图将其Content属性设置为true)。在单元测试中,我尝试使用如下的相对路径打开文件:
std::ifstream file("text.txt");
但它不工作。
我猜这个文件应该被复制到单元测试运行的地方。有没有简单的解决办法?
这可以通过使用__FILE__
宏来完成,在我的例子中,我是这样做的:
//Returns my solution's directory
#define TEST_CASE_DIRECTORY GetDirectoryName(__FILE__)
string GetDirectoryName(string path){
const size_t last_slash_idx = path.rfind('');
if (std::string::npos != last_slash_idx)
{
return path.substr(0, last_slash_idx + 1);
}
return "";
}
TEST_METHOD(MyTest)
{
string filename = std::string(TEST_CASE_DIRECTORY) + "MyTestFile.txt";
TestOutputForFile(filename);
}
相关文章:
- 如何在 VSCODE 中设置项目文件夹并链接所有 CPP 文件
- 使用 Premake5 构建库,每个构建没有单独的项目文件
- CMake 如何强制third_party库将项目文件包含在当前项目源目录中?
- 当包含头文件的文件不在根项目文件夹中时,如何包含它
- Qt创建者:加载位于子目录中的项目文件
- 配置过程项目文件中的错误可能无效
- 哪些 Netbeans 8.2 项目文件应提交到C++项目的版本控制?
- C++错误LNK2005和不同项目文件的常规继承
- 为什么我的QTRPT项目文件未在QTCreator中运行
- 严重性代码说明项目文件行抑制状态错误 C2676 二进制">>":"std::ifstream"
- 如何在运行qmake时将变量传递到qmake项目文件
- 如何使用QPixmap,以便在地址中只需要使用项目文件夹
- CMake 插入不带文件夹前缀的项目文件
- QT无法解析项目文件
- 将静态库链接行添加到我的QT项目文件没有区别
- CMAKE QT创建者:将标题文件添加到项目文件中
- Qt C++:将所有库文件包含在项目文件夹中
- 对单元测试(googletest)和项目/文件夹/文件的混淆
- C++项目文件夹外部依赖项仅包含 .h 和 .hpp 文件
- 有没有办法将文件包含在C++中的所有项目文件中