如何更改 QtTest 输出的编码?
How to change encoding of the QtTest output?
当我在 Visual Studio 2015 Test Explorer 下执行测试时,我得到了 unicode 编码的结果
void TestProduct::test_case1()
{
QString string = "Кириллица";
QString result = "кириллица";
qDebug() << string;
qDebug() << result;
QCOMPARE(string, result);
}
输出为
PASS : 'initTestCase()'
FAIL : 'test_case1()' Compared values are not the same
Actual ((string)): "\u041A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430"
Expected (result) : "\u043A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430"
QDEBUG : "Кириллица"
QDEBUG : "кириллица"
tst_testproduct.cpp(33)
有没有办法像 qDebug 那样以更易读的格式获取实际值和预期值的输出?
找到解决方案。
QT Testlib 在它打算输出 QString 值时在内部调用 toPrettyUnicode
。
为了修复它,我覆盖了 toString
函数:
char *toString(const QString &str)
{
return qstrdup(str.toUtf8().constData());
}
当我在 Visual Studio 2015 Test Explorer 下执行测试时,我得到了 unicode 编码的结果
void TestProduct::test_case1()
{
QString string = "Кириллица";
QString result = "кириллица";
qDebug() << string;
qDebug() << result;
QCOMPARE(string, result);
}
输出为
PASS : 'initTestCase()'
FAIL : 'test_case1()' Compared values are not the same
Actual ((string)): "\u041A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430"
Expected (result) : "\u043A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430"
QDEBUG : "Кириллица"
QDEBUG : "кириллица"
tst_testproduct.cpp(33)
有没有办法像 qDebug 那样以更易读的格式获取实际值和预期值的输出?
找到解决方案。
QT Testlib 在它打算输出 QString 值时在内部调用 toPrettyUnicode
。
为了修复它,我覆盖了 toString
函数:
char *toString(const QString &str)
{
return qstrdup(str.toUtf8().constData());
}