Unit test wxWidgets

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Unit test wxWidgets

Postby Azrael » Wed Sep 13, 2017 1:13 pm

Hi,
I'm trying to test a small project written in c++ with googletest. However, everytime it finds a wxWidgets element it gives me a segmentation fault.
For example :

Code: Select all

void methodToTest(int c){
if( c>10 )
    a=true;
    wxMessageBox("zzz");
else
    a=false;
    wxMessageBox("aaa");
}   


Test:

Code: Select all

TEST( Suite, Test ){
methodToTest(5);
EXPECT_FALSE(a);


The test obviously doesn't pass .. all because of a segmentation fault. With the debugger it indicates the line with wxMessageBox()
Is googletest incompatible with wxWidgets?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1924
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Unit test wxWidgets

Postby ONEEYEMAN » Wed Sep 13, 2017 1:39 pm

Hi,
I don't know anything about googletest, but wxWidgets is tested with the project called cppunit.
Could you post a backtrace at the time of the crash?

Also:
1. What is your wx version?
2. Which OS you are trying to test this on?
3. Did you compile the library yourself?

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 11290
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Unit test wxWidgets

Postby doublemax » Wed Sep 13, 2017 1:45 pm

I think you forgot some braces:

Code: Select all

if( c>10 )
{
    a=true;
    wxMessageBox("zzz");
}
else
{
    a=false;
    wxMessageBox("aaa");
}

Did you initialize wxWidgets?
Use the source, Luke!

User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Re: Unit test wxWidgets

Postby Azrael » Wed Sep 13, 2017 2:52 pm

doublemax wrote:I think you forgot some braces

Yeah, you're right. But that is just a function I wrote to show an example. My method is implemented in another way.
Did you initialize wxWidgets?

I don't know what does that mean. Is this something you need to do inside tests? Maybe I did, I don't know

ONEEYEMAN wrote:Could you post a backtrace at the time of the crash?


https://imgur.com/a/7W2YN

Here is the method and the test including the lines:

https://imgur.com/a/1xKqf

ONEEYEMAN wrote:1. What is your wx version?
2. Which OS you are trying to test this on?
3. Did you compile the library yourself?


1. wx3.1
2.Ubuntu 16.04
3.Yes

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1924
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Unit test wxWidgets

Postby ONEEYEMAN » Wed Sep 13, 2017 2:58 pm

Hi,
Azrael wrote:
doublemax wrote:I think you forgot some braces

Yeah, you're right. But that is just a function I wrote to show an example. My method is implemented in another way.
Did you initialize wxWidgets?

I don't know what does that mean. Is this something you need to do inside tests? Maybe I did, I don't know

Please check the "minimal" sample, in particular MyApp::OnInit() function.
Basically you should create a wxApp object and call its base class.

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 11290
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Unit test wxWidgets

Postby doublemax » Wed Sep 13, 2017 3:04 pm

I don't know what does that mean. Is this something you need to do inside tests? Maybe I did, I don't know
If you have simple code with main() without using the wxWidgets macros to create an application, you need to initialize wxWidgets manually.

The easiest way is to create an instance of wxInitializer:
http://docs.wxwidgets.org/trunk/classwx ... lizer.html
Use the source, Luke!

User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Re: Unit test wxWidgets

Postby Azrael » Thu Sep 14, 2017 8:00 am

I have a wxApp object. To be more precise, i have a class derived from wxApp that overrides OnInit() an OnExit(). I use that in my code. Let's call it MyWxApp.
MywxApp. h

Code: Select all

Class MyWxApp : public wxApp{
void OnInit() override ;
int OnExit() override ;

}


In MyWxApp.cpp I implement OnInit() (I create some objects and an object created from a class derived from wxFrame) and OnExit();

I actually use some macros. Before main I use IMPLEMENT_APP_NO_MAIN(MyWxApp) and in main I call wxEntryStart(argc, argv) And OnInit().
Before, I used the macro IMPLEMENT_APP(MyWxApp) directly inside MyWxApp. cpp but I changed it because there was a problem with main (there were defined more than one) inside the tests when I was trying to create a MyWxApp object.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1924
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Unit test wxWidgets

Postby ONEEYEMAN » Thu Sep 14, 2017 1:25 pm

Hi,
So you test suite is not a GUI?
In this case look at the "console" sample how to properly initialize a non-GUI application.

Also, just curious - you did make the library and you application as "debug" one, right?

Thank you.

User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Re: Unit test wxWidgets

Postby Azrael » Thu Sep 14, 2017 2:00 pm

Hello,
You're right, my test suite is not a GUI. I'll take a look at the console sample (even though my application has a GUI) .

Well, when i compiled the library I did two times. I have a simple version and a debug one.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1924
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Unit test wxWidgets

Postby ONEEYEMAN » Thu Sep 14, 2017 2:17 pm

Hi,
I did see a call to wxMessageBox(). Does this mean you are mixing up GUI and non-GUI framework?

I don't think it will work that way. You can try to use a simple printf() to print the errorneous issue or make a test suite a GUI based.

Thank you.

User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Re: Unit test wxWidgets

Postby Azrael » Sat Sep 16, 2017 1:08 pm

ONEEYEMAN wrote:I don't think it will work that way.

You're right. It won't work that way... I switched to another function for those messages.
ONEEYEMAN wrote:make a test suite a GUI based

How do you do that ?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1924
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Unit test wxWidgets

Postby ONEEYEMAN » Mon Sep 18, 2017 1:49 pm

Hi,
Just look at the minimal sample.
Basically what you need is instantiate wxApp-based object, then create a wxFrame with wxProgressDialog.
Start you test thread (if you testing GUI that won't work) and after each test either successive or failure - send an event to the wxProgressDialog. In the handler - update the progress.
The same thing will work with testing GUI - just start tests after creating wxProgressDialog in the same thread.

Thank you,.


Return to “C++ Development”

Who is online

Users browsing this forum: Google [Bot] and 9 guests