c++ - 我如何编写用于测试 gui c++ 的单元测试
问题描述
我正在尝试遵循本教程。http://www.remy.org.uk/tech.php?tech=1407951209
测试编译并运行除了没有得到正确的结果。由于某种原因,测试 gui 没有得到关注。
我试图单步执行它在当前窗口上附加文本的代码,我关注的是文本编辑器。
我从这个例子中遗漏了什么?
#pragma once
#include <wx/wx.h>
#include <wx/uiaction.h>
#include "gtest/gtest.h"
class TestFrame : public wxFrame
{
wxTextCtrl *textIn;
wxButton *button;
wxTextCtrl *textOut;
FRIEND_TEST(GuiTest, CopyTest);
public:
TestFrame() : wxFrame(NULL, wxID_ANY, "wxUnitTest", wxPoint(50, 50), wxSize(450, 340))
{
textIn = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0);
button = new wxButton(this, wxID_ANY, wxT(" => "), wxDefaultPosition, wxDefaultSize, 0);
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &TestFrame::OnButton, this);
textOut = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0);
wxBoxSizer *boxSizer = new wxBoxSizer(wxHORIZONTAL); boxSizer->Add(textIn, 1, wxALL, 5);
boxSizer->Add(button, 0, wxALL, 5); boxSizer->Add(textOut, 1, wxALL, 5);
this->SetSizer(boxSizer); this->Layout(); this->Centre(wxBOTH); }
void OnButton(wxCommandEvent &WXUNUSED(event)) { this->textOut->SetValue(this->textIn->GetValue()); }
};
class TestApp : public wxApp
{
public: TestFrame *frame; virtual bool OnInit()
{
frame = new TestFrame();
frame->Show(true);
return true;
}
};
class GuiTest : public testing::Test
{
protected:
TestApp *app;
virtual void SetUp()
{
char appname[] = "wxUnitTest.exe";
int argc = 1;
char *argv[1] = { appname };
app = new TestApp();
wxApp::SetInstance(app);
wxEntryStart(argc, argv);
app->OnInit();
}
virtual void TearDown()
{
//wxTheApp->OnRun();
app->OnExit();
wxEntryCleanup();
}
};
TEST_F(GuiTest, CopyTest)
{
wxUIActionSimulator acts;
app->frame->textIn->SetFocus();
wxYield();
acts.Text("Text");
wxYield();
app->frame->button->SetFocus();
wxYield();
acts.Char(WXK_RETURN);
wxYield();
ASSERT_EQ("Text", app->frame->textOut->GetValue());
}
// Main.cpp
#include "stdafx.h"
#include "Main.h"
#include "tst_guitest.h"
#include "wx/app.h"
int main(int argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
解决方案
推荐阅读
- java - 在java中使用&和|将字节转换为整数
- java - Oracle - 如何在不格式化的情况下将 Java 项目中的时间戳作为字符串插入
- c - 数据结构(C):大砖小房子,还是小砖大房子?
- r - 在 Linux 中查找软件包的二进制文件
- docker - docker stack deploy的自定义入口点?
- rest - NetSuite SuiteTalk REST Web 服务 - 登录尝试无效
- robotics - Webots - 并排打开多个窗口
- xml - 恢复并解释隐藏在属性数据中的子父级互连(而不是在 XML 中表示为结构)[XSLT 1.0]
- javascript - 从子设置父状态(对象)会导致不断重新渲染
- python - 阴影后调用内置函数