首页 > 解决方案 > 我如何编写用于测试 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();  
}

标签: c++unit-testingwxwidgetsgoogletest

解决方案


推荐阅读