如何使用 CppUnitTestFramework 在 Visual Studio 原生 C++ 单元测试中获取 $(ProjectDir) 路径? [英] How to get the $(ProjectDir) path in a Visual Studio native C++ Unit Testing using CppUnitTestFramework?

查看:32
本文介绍了如何使用 CppUnitTestFramework 在 Visual Studio 原生 C++ 单元测试中获取 $(ProjectDir) 路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够检索我的单元测试的项目目录路径,以便加载测试所需的一些文件.我不想硬编码以防将来解决方案结构和绝对路径发生变化.

I need to be able to retrieve the project directory path of my unit testing in order to load some files required by the tests. I don't want to hard-code it in case the solution structure and absolute paths changes in the future.

推荐答案

好的,我是这样做的:

  1. 在我的项目属性 -> 配置 -> C/C++ -> 预处理器中,我添加了这个预处理器定义 UNITTESTPRJ="$(ProjectDir)."

然后在我的 cpp 文件中我做了:

Then in my cpp file I did:

#define STRINGIFY(x) #x

#define EXPAND(x) STRINGIFY(x)

string s = EXPAND(UNITTESTPRJ);
s.erase(0, 1); // erase the first quote
s.erase(s.size() - 2); // erase the last quote and the dot
string my_project_dir = s;

最后的愚蠢的.是为了转义项目目录中尾随的\".

The stupid . at the end was necessary to escape the trailing \" in the project directory.

这篇关于如何使用 CppUnitTestFramework 在 Visual Studio 原生 C++ 单元测试中获取 $(ProjectDir) 路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆