简体   繁体   English

如何使用CppUnitTestFramework在Visual Studio本机C ++单元测试中获取$(ProjectDir)路径?

[英]How to get the $(ProjectDir) path in a Visual Studio native C++ Unit Testing using CppUnitTestFramework?

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. 如果解决方案结构和绝对路径将来发生变化,我不想对其进行硬编码。

Ok, this is how I did it: 好的,我就是这样做的:

  1. In my project Properties -> Configuration -> C/C++ -> Preprocessor I added this preprocessor definition UNITTESTPRJ="$(ProjectDir)." 在我的项目属性 - >配置 - > C / C ++ - >预处理器中,我添加了这个预处理器定义UNITTESTPRJ="$(ProjectDir)."

  2. Then in my cpp file I did: 然后在我的cpp文件中我做了:

#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. 最后有必要逃避项​​目目录中的尾随\\"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio中的CppUnitTestFramework(C ++)中设置超时? - How to set Timeout in CppUnitTestFramework (C++) in Visual Studio? 在 Visual Studio Code 中对本机 C++ 进行单元测试 - Unit Testing Native C++ In Visual Studio Code VS2013带有CppUnitTestFramework的C ++ DLL单元测试崩溃 - VS2013 Unit testing c++ dll with CppUnitTestFramework crashes 使用带有字符串而不是宽字符串的 Visual Studio 原生 C++ 单元测试框架 - Using Visual Studio native C++ unit testing framework with strings instead of wide strings 如何为 Visual Studio C++ 设置单元测试 - How to set up unit testing for Visual Studio C++ 如何使用Visual Studio CppUnitTestFramework访问我的代码 - How to access my code using Visual Studio CppUnitTestFramework Visual Studio 2010的单元测试功能是否可用于本机C ++代码? - Is Visual Studio 2010's unit testing feature usable for native C++ code? 在Visual Studio 2012中单元测试exe中的C ++代码时阻力最小的路径 - Path of least resistance when unit testing C++ code in an exe, in Visual Studio 2012 需要Visual Studio 2005兼容的C ++单元测试框架 - Need Visual Studio 2005 Compatible C++ Unit Testing Framework 在Visual Studio 2012中对C ++控制台应用程序进行单元测试 - Unit testing c++ console application in Visual Studio 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM