c++ - 比较字符串时 QtTest 失败,即使它们相同? C++

标签 c++ qt qttest

我有一个比较两个字符串值的 QtTest:

 // This definitely returns "5/6" 
 std::string actual_value = simple(actual);

 // Sets a string to the expected value
 std::string expected_value = "5/6";

然后是 QCOMPARE:

QCOMPARE(QString(actual_value.c_str()), QString(expected_value.c_str()));

/* I'm Aware this could just be the code below (but this doesnt print values to
console so i can check they are actually the same)  */
QCOMPARE(actual_value, expected_value)

我得到了错误:(我将两者都发布,所以很明显它不仅仅是 QString 的失败,所以下面是使用 QString 显示值的 QCOMPARE)

FAIL!  : TestFraction::testFraction() Compared values are not the same
Actual   (QString(actual_value.c_str())): 5/6

Expected (QString(expected_value.c_str())): 5/6

这是正常的 QCOMPARE 错误消息:

FAIL!  : TestFraction::testFraction() Compared values are not the same

这背后显然还有其他代码,但我现在不发布它,因为我认为它不是特别相关,但询问是否有你想知道的东西,据我所知,这个测试应该通过,但也许有一些我不知道的东西关于比较字符串(比如比较 double 如何失败,除非你使用模糊比较)

提前致谢

最佳答案

您的实际字符串有换行符。因此比较失败。

关于c++ - 比较字符串时 QtTest 失败,即使它们相同? C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15768885/

相关文章:

c++ - 尝试重载 [] 来遍历双向链表

c++ - 如何将 C 函数导入到尚未在 C 头文件中声明的 C++ 项目中?

C++ 从无符号字符数组创建 GUID

c++ - Qt错误: LNK1181: cannot open input file 'debug\main.obj'

qt - QAudioOutput缓冲区下溢

qt - 如何在通过 QTestLib 对 Qt 应用程序进行单元测试时构建项目

c++ - 从欠定系统中删除不可解的方程

c++ - 在 qt (mingw) 中启用 c++1y

c++ - QTest for C++ 每个测试用例消耗多少

unit-testing - 我应该为 Qt 使用什么单元测试框架?