我想测试一个使用
获取主屏幕屏幕大小的方法void MouseMapper::getScreenProperties() {
QDesktopWidget *widget = QApplication::desktop();
QRect screenGeometry = widget->screenGeometry(widget->primaryScreen());
_screenHeight = screenGeometry.height();
_screenWidth = screenGeometry.width();
}
这很正常。但是当我尝试使用gmock和
进行测试时TEST(AMouseMapper, GetsTheScreenSize) {
MouseMapper mouse;
mouse.getScreenProperties();
//I know these hardcodes values don't always apply,
//thats not the point here
ASSERT_EQ(1900, mouse.getScreenWidth());
ASSERT_EQ(1200, mouse.getScreenHeight());
}
和
#include "gmock/gmock.h"
int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
我收到错误,因为我没有构建QApplication,这是正确的。 所以当我尝试
时#include "gmock/gmock.h"
#include <QApplication>
int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
QApplication app(argc,argv);
app.exec();
return RUN_ALL_TESTS();
}
我得到了同样的错误。当我尝试在测试函数中直接构造QApplication(如this answer中所提出的)测试崩溃时,也会发生这种情况,因为argc和argv要么不正确,要么
TEST(AMouseMapper, GetsTheScreenSize) {
char arg0[] = "programName";
char* argv[] = { &arg0[0], NULL };
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
QApplication app(argc,&argv[0]);
app.exec();
MouseMapper mouse;
mouse.getScreenProperties();
ASSERT_EQ(1900, mouse.getScreenWidth()); //I know that these hardcoded values don't always apply
ASSERT_EQ(1200, mouse.getScreenHeight());
}
Valgrind然后给了我
==7093== Conditional jump or move depends on uninitialised value(s)
==7093== at 0x102E437A: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.25)
==7093== by 0x7EA2473: ??? (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4200.0)
==7093== by 0x7EBC056: g_signal_emit_valist (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4200.0)
==7093== by 0x7EBCEF9: g_signal_emit_by_name (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4200.0)
==7093== by 0x7EA9BDA: g_object_set_valist (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4200.0)
==7093== by 0x7EAA46B: g_object_set (in /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0.4200.0)
==7093== by 0x5EB0BB5: ??? (in /home/elmewo/Libraries/Qt/5.3/gcc_64/lib/libQt5Widgets.so.5.3.2)
==7093== by 0x5E2D23E: QStyleFactory::create(QString const&) (in /home/elmewo/Libraries/Qt/5.3/gcc_64/lib/libQt5Widgets.so.5.3.2)
==7093== by 0x5DC9B4A: QApplication::style() (in /home/elmewo/Libraries/Qt/5.3/gcc_64/lib/libQt5Widgets.so.5.3.2)
==7093== by 0x5DCA51C: QApplicationPrivate::initialize() (in /home/elmewo/Libraries/Qt/5.3/gcc_64/lib/libQt5Widgets.so.5.3.2)
==7093== by 0x5DCA56F: QApplicationPrivate::construct() (in /home/elmewo/Libraries/Qt/5.3/gcc_64/lib/libQt5Widgets.so.5.3.2)
==7093== by 0x47B10E: AMouseMapper_GetsTheScreenSize_Test::TestBody() (MouseMapperTest.cpp:18)
我不知道我能在这做什么。有人可以帮忙吗?
P.s。:我不想在这里使用QTest。 :)
答案 0 :(得分:0)
我们使用以下方法:
int main(int argc, char** argv)
{
TestCoreApp app(argc, argv);
...
app.exec();
}
在TestCoreApp的构造函数中:
TestCoreApp::TestCoreApp(int& argc, char** argv)
: QCoreApplication(argc, argv)
{
InitGMockAndGTest(argc, argv);
}
初始化功能:
void TestCoreApp::InitGMockAndGTest(int& argc, char** argv) const
{
InitGoogleMock(&argc, argv);
InitGoogleTest(&argc, argv);
}
您的GoogleMockInit会在QCoreApplication初始化之前启动。
testing::InitGoogleMock(&argc, argv);
QApplication app(argc,argv);