我正在尝试使用本机单元测试项目在 Visual Studios 2012 中创建单元测试。
这是我的测试:
TEST_METHOD(CalculationsRoundTests)
{
int result = Calculations::Round(1.0);
Assert::AreEqual(1, result);
}
导出类:
#ifdef EXPORT_TEST_FUNCTIONS
#define MY_CALCULATIONS_EXPORT __declspec(dllexport)
#else
#define MY_CALCULATIONS_EXPORT
#endif
...
class CALCULATIONS_EXPORT Calculations {
...
public:
static int Round(const double& x);
函数本身:
int Calculations::Round(const double& x)
{
int temp;
if (floor(x) + 0.5 > x)
temp = floor(x);
else
temp = ceil(x);
return int(temp);
}
然而,测试几乎总是失败,并出现错误代码 c0000005(访问冲突)。
第一次使用 x 或可能在函数中声明的任何其他变量时,测试将失败。
我按照以下说明进行操作编译 Visual C++ 2012 单元测试时未解决的外部问题 https://stackoverflow.com/questions/14010627/unresolved-externals-when-compiling-unit-tests-for-visual-c-2012
这是一个已知错误 https://connect.microsoft.com/VisualStudio/feedback/details/804696/linking-visual-studio-2012-c-unit-testing-project-against-an-exe-causes-access-violations。不幸的是,微软认为这是“无法修复”。
简而言之,有两种解决方法:
- 在release模式下编译实际项目,在debug模式下编译测试项目。
- 将所有可测试函数外包给静态库项目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)