GUI testing with DUnit
I don’t how I didn’t know this until now but it turns out there’s a whole section of DUnit designed to help you test GUI elements. What follows is small write up about how to do it.
I started with a dead simple app. Type your text into the edit box, click the button and the text shown in a label’s caption:
So I started with a TGUITestCase descendant:
type MainFormTests = class(TGUITestCase) protected procedure SetUp; override; procedure TearDown; override; published procedure Hookup; end;
DUnit runs the SetUp and TearDown methods before and after each test method. This is different than NUnit and other unit testing frameworks which call SetUp and TearDown once per test suite execution. So if in the SetUp we start the application and in the TearDown we close it then we will be starting and stopping the app for every test. This is undesirable in this type of testing since we will be changing state in application. Additionally it might slow the tests down to have to perform the application startup and shutdown over and over again.
To deal with this problem we’ll use a class from the TestExtensions unit called TTestSetup. TTestSetup is a decorator for a TTestCase. This means that you implement behavior before and after tests run … like setup and teardown code that should only run once. To get this done you need two things: 1) a subclass of TTestSetup in which you’ll do your one time setup and teardown and 2) do a little different test registration. Here’s the TTestSetup class:
uses TestFramework, TestExtensions, GUITesting, fMain; type MainFormSetup = class(TTestSetup) private FMainForm : TForm1; protected procedure SetUp; override; procedure TearDown; override; end;
You can see here the main form reference for our test app and the SetUp and TearDown that will happen once. Next let’s see the TGUITestCase and the implementation code:
MainFormTests = class(TGUITestCase) protected procedure TearDown; override; published procedure Hookup; procedure TestChangeEditText; procedure TestButtonClickWithText; procedure TestButtonClickWithNoText; procedure TestButtonClickWithOnlyWhiteSpace; end; implementation { MainFormSetUp } procedure MainFormSetUp.SetUp; begin FMainForm := TForm1.Create(Application); FMainForm.Show; inherited; end; procedure MainFormSetUp.TearDown; begin FMainForm.Release; FMainForm.Free; inherited end; { MainFormTests } procedure MainFormTests.Hookup; begin Check(True); end; procedure MainFormTests.TearDown; begin GUI := nil; inherited; end; procedure MainFormTests.TestButtonClickWithNoText; var lbl: TLabel; LabelText: string; begin RunGUITestSequence(LabelText, lbl, ''); CheckEquals(LabelText, lbl.Caption); end; procedure MainFormTests.TestButtonClickWithOnlyWhiteSpace; var lbl : TLabel; LabelText : string; begin RunGUITestSequence(LabelText, lbl, ' '); CheckEquals(LabelText, lbl.Caption); end; procedure MainFormTests.RunGUITestSequence(var LabelText: string; var lbl: TLabel; const TestValue : string); var Edit: TEdit; begin lbl := FindControl('Label1') as TLabel; LabelText := lbl.Caption; Edit := FindControl('Edit1') as TEdit; Edit.Text := TestValue; Click('Button1'); end; procedure MainFormTests.TestChangeEditText; var Edit: TEdit; begin Edit := FindControl('Edit1') as TEdit; Edit.Text := 'I like pizza'; CheckEquals('I like pizza', Edit.Text); end; procedure MainFormTests.TestButtonClickWithText; var lbl: TLabel; begin Click('Button1'); lbl := FindControl('Label1') as TLabel; CheckEquals('I like pizza', lbl.Caption); end; initialization RegisterTest(MainFormSetUp.Create(MainFormTests.Suite));
With the test registration above, the DUnit test tree looks a little different:
Now for the fun part.
You take these tests and recreate my test application. Comment below about how it went and what questions, if any, you had.


Have u ever played with OpenCtf ?
http://www.mikejustin.com/open.html#item_261
Great example, Thank you
I wasn’t aware of DUnit with GUI testing.
I did not notice this feature DUnit.
thanks.
So did anyone try the challenge?
VERY cool example! We’ve been always telling people that automated GUI testing is difficult/impossible…
Thanks,
Pawel
Just tried OpenCtf, lot of testning for free and easy to use. Thanks for the tip Ahmet.
“This is different than NUnit and other unit testing frameworks which call SetUp and TearDown once per test suite execution.”
Sorry, but you’re way off base here. SetUp and TearDown are always called once per test method, in NUnit as well as in DUnit (along with csUnit, and I’m pretty sure JUnit, and probably every other xUnit-like framework out there).
Are you thinking of NUnit’s FixtureSetUp and FixtureTearDown? Those are more or less equivalent to what you can do with a single TTestSetup in DUnit.
Thanks Joe. I’ve updated the article accordingly.
Jody,
Thanks for the post! I finally got around to trying this and it was an…interesting experience. Here’s how it went:
1) The test unit didn’t compile. The uses clause is missing Forms and StdCtrls, and RunGUITestSequence isn’t included in the object declaration.
2) I can’t see the point of the “Hookup” test.
3) In MainFormSetUp.TearDown you’re calling both Release and Free. Release just posts a message that eventually frees the form, so it’s unnecessary.
4) In MainFormTests.TearDown you have “GUI := nil”, but you never assign it in the first place. I’m assuming that’s left over junk from before you used TTestSetup.
5) TestChangeEditText fails if the edit doesn’t exist, which I guess is good from a TDD standpoint, but the CheckEquals call is just verifying that the VCL doesn’t have bugs, which seems like a strange thing to do.
Ok, finally got to compiling and running tests:
6) Created an empty fMain form and ran it. Hookup passed, the other 4 did not.
7) Dropped a button, edit, and label on the form and used default properties for everything. TestButtonClickWithText failed, the other 4 passed.
9) I remove the event handler entirely, and change Label1’s caption to “I like pizza”. All 5 tests pass.
So I think I managed to recreate your test application, but only if you were lying about what it did.
I’m also curious about the separation of TestChangeEditText and TestButtonClickWithText. If the application did what you described, TestButtonClickWithText would only work if it was run immediately after TestButtonClickWithText. DUnit doesn’t randomize the test order, so that isn’t a concern, but it also means the test can’t be run on its own.
Hi Craig,
Thanks for trying out the challenge and posting your experience!
Here’s the implementation code for the form:
To answer your questions:
The Hookup test: This is a habit I developed. The first test I write is Hookup. I use it to be sure my new test suite is properly registered and working the way I expect. You can blame Ron Jeffries for this habit
GUI := nil: The GUI property of TGUITestCase is assigned in the SetUp. The default behavior sets GUI to the first form that’s created in the test. That’s why you don’t see an assignment of GUI in my test case.
The TestChangeEditText test: This was really a boot-strapping test. I wrote it to discover if I could interact with the form elements. I probably should have deleted this test after writing the succeeding tests.
Regarding item number nine in your list: Guess I should have added a test that specifies that the label caption should be blank on startup
Regarding your last paragraph: You’re right. Those two tests should be combined into one test.
Thanks again for posting your experience!
BTW, I love Beyond Compare.
Thanks for a very helpful article!
Just an other observation of me. I think it may be useful for others.
I wanted to apply D-Unit in testing MDI-child forms but I couldn’t manage to do so with
procedure MainFormSetUp.SetUp;
begin
FMainForm := TMDIForm.Create(Application);
FMainForm.Show;
FMDIChild := TMDIChild.Create(FMainForm);
inherited;
end;
an exception was thrown as a result. Then I tried:
procedure MainFormSetUp.SetUp;
begin
Application.CreateForm(TMDIForm, FMainForm);
FMainForm.Show;
FMDIChild := TMDIChild.Create(FMainForm);
inherited;
end;
This had worked!
Where is the download for DUNIT 2?
If one of the forms I am testing may display a modal dialog box created by a call to MessageDlg, is there a way to programmatically press a button on the modal dialog box so that the unit test does not require the tester to manually click the modal dialog box?
Hi,
Thanks a lot for sharing this informative information about DUNIT. I was looking about this from long time and your post really helps me a great deal. Keep it up.
I greatly appreciate all the info I’ve read here. I will spread the word about your blog to other people. Cheers.
This is a really interesting post. Thanks for sharing this.
Don’t have cash to buy some real estate? Don’t worry, because that is achievable to take the home loans to solve such kind of problems. Therefore get a consolidation loan to buy all you need.
Very interesting and informative site! Good job done by you guys, Thanks
Great post, you are working great on this blog, i just want to say you that just keep it up.
Wow! Thanks for the great informative post.
I think the above article is informative for all concerned people. For me this Information is really really useful.
Although post is nice but I am not agree with all the points. Any way best as compare to others…
Very interesting information.
One should read your articles for getting more and more info on GUI testing.
Very nice post.
I recommend every one should read your articles for getting more and more info on GUI testing.
Although post is nice but I am not agree with all your points.
But keep writing, it will give you more grip and expertise in you skills.
Yeah off course, it is nice to see all this.
Your blog is really excellent. It inspires the readers who has that great desire to lead a better and happier life. Thanks for sharing this information and hope to read more from you.
Thanks, I like this post it is very good and informative. I am sure that this post will be very helpful.
Your blog seems to be so delicious, I’ve known so much since i read it every day
Where is the download for DUNIT 2?
I recommend every one should read your articles
Thats a very clear code GUNIT object for creating interfaces. I think you should create cool interface for many programs and show it to us.
informative
nice, thnx really helpful
Great post, what you said is really helpful to me. I can’t agree with you anymore. I have been talking with my friend about, he though it is really interesting as well. Keep up with your good work, I would come back to you.
is there a way to programmatically press a button on the modal dialog box so that the unit test does not require the tester to manually click the modal dialog box?
That issue referring to this topic is smashing! Thence scholars not have to finish the thesis writing or just thesis proposal by their own, they would get your help.
I opine that this is workable to go to this site, just because only here scholars should see the good enough stuff referring to this good post. Thus, the thesis service would use this for student dissertation completing.
To create the gapless thesis methodology can be a synonym of a cemetery for some people. But there are some other to forestall the unsuccess. Thence, I will recommend to use the assistance of the buy dissertation service, when people are willing their idea close to this topic be perfectly composed.
You have great composing skillfulness and you thought seems to be amazing. I think that hard working students will like to complete the essays of such quality and the assignment writing service can do it for them.
When people are not sure what to opt for, term papers or american history essay, they can ask you, because you know the way to create the well done knowledge related to this topic.
Hello, I’m using Delphi 7, and would use the D-Unit, but I am not able to install it, because I sought help at several sites but the installation is not very clear, could you help me please.
Hugs.
Some people say check information of our costs right before ordering custom written essays in the custom essays writing firm. And some people must also get the superb texts referring to this good post over here.
Thank you so much for this. They are truly remarkable. What a Creativity!
Brilliant! Very useful information for me.
Thanks for posting this much valuable content.It is a great example and helpful to many of us.
Brilliant! Very useful information for me.
What is DUnit?
What is DUnit?
Nice one..,thank u for this one
you really have avery nice blog,it’s the first time to be here but it won’t be the last untill then keep blogging.goodluck!
Sorry, but you’re way off base here. SetUp and TearDown are always called once per test method, in NUnit as well as in DUnit (along with csUnit, and I’m pretty sure JUnit, and probably every other xUnit-like framework out there).
Yes, I’ve tried to use this one and it’s kind of amazing.
Very nice Site number one topic Thanks you..
Thanks for sharing. i really appreciate it that you shared with us such a informative post..
[url=http://www.linksoflondonuk.com/Bracelets/"]charm bracelets[/url] is best jewellery.I like so,do you like.
Every female in the world is willing to be unique, but doesn’t know the simple way to do that. But millions of people find the free ringtones or just cellphone ringtones to become unique.
I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.
admire what you have done here.
Thanks for the post! I finally got around to trying thi
Thanks for information you’ve given.. I will back for more..
Woh.., nice article, thank you for sharing this. This article helps to many people. And also i like to tell about the EssayAcademia. EssayAcademia provides well written custom papers on a wide variety of subjects.
I do not know what to say. This blog is fantastic and You know so much about this subject. So much so that you made me want to learn more about it. Your blog is my stepping stone, and thanks for the heads up on this subject.
Brilliant! Very useful information for me.
Thanks for taking the time to share this, I feel strongly about it and love reading more on this topic.
That is an awfully astounding column you’ve posted.
Thanks a lot for that a fantastically amazing post!
New properties and methods have been added to the PrintJob class to give you better control of the way content is printed from an AIR application, including the choice of printer, paper size, a
nd number of copies. New properties also give you more information about the printer, such as printable area, whether the printer will print in color, and whether the print job is currently active.
Io amo molto i programmi di presentazione come Powerpoint o Keynote. Per progettare una buona presentazione bisogna tenere conto di una serie di semplici regole e molto buon senso. http://www.tiffanycore.com
Thanks for report.
it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity!
Good website. I like the all pages and all comments. Thanks for all!!! Regards!!!
Good website and happy messages. I like it. Thank you master!
Hi WebMaster! Real good website! Good work!!!! Thank You!
An amazing machine, you deserve to 1. These fantastic shoes can be a brilliant and attractive. Enjoy and of itself.
An amazing machine, you deserve to 1. These fantastic shoes can be a brilliant and attractive. Enjoy and of itself.
I appreciate yout report.
very helpful guide and clearly explained, thanks
Nice stuff. Thanx.
Nice stuff. Thanx.
This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. Did you acquired lots of links and I see lots of trackbacks??
I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new
stuff here than anybody else!
Its amazing, looking at the time and effort you put into your blog and detailed information you provide.It’s very beautiful!!!
I can go with the example stated by you but truly speaking at the ends its quality that really matters.Thank you!
I really liked your article. Keep up the good work.
The code is quite helpful. I would like to say thank you so much.
keep it up man!
Th4t be an epic da shizzi4 post, th4nkie 4it & in da futures we’ll be seeing more of it
We7ll I8be dat9 ogr6e speekie da speekie, gratz & than4x
heb7e sh8at be th34nkie 4it on da posting left & righ8ty
I posted your piece of writing 2 my myspace profile.
Good post! I am also going to write a blog post about this… thanks
If you are in the corner and have no money to go out from that point, you will require to take the personal loans. Just because that would help you emphatically. I get commercial loan every year and feel great because of it.
Admiring the time and effort you put into your blog and detailed information you offer!
I think the above article is informative for all concerned people. For me this Information is really really useful.
I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them.
Great illustration, you should do YouTube videos
thanks a lot for sharing this post.
@Joe White
great post. keep it up.
I do not know what to say. This blog is fantastic
Wonderful post about “GUI testing with DUnit”.
I do not know what to say. This blog is fantastic
By learning these technologies, you open up so much more possibilities than if you narrow yourself to a select few set of components.
Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information
I admit, I have not been on this webpage in a long time… however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.
Great stuff as usual…
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
a lot for sharing this informative information about DUNIT. I was looking about this from long time and your post really helps me a great deal. Keep it up.
Very nice.
Glass Brooklyn
Very nice.
@Jody Dawkins
Nice site.
I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them.
Its great resource. i was finding that type inf and now i get it.thanks for this…
thanks a lot for sharing this one. nice post!
Awesome post.
this is actually what i was looking for.
thank you for the post.
keep going on.
thanks a lot for sharing this one. nice post!
GUI stands for Graphical User Interface. Thanks for posting this very informative blog. Looking forward for your next post.
yes,we’ll use a class from the TestExtensions unit called TTestSetup.
konstruktiv eigene Ideen und Anregungen einzubringen. Wir sind der Meinung, dass eine command nationale Herangehensweise dem internationalen Charakter des Internets und habitation vielfältigen thomas sabo
Herausforderungen und Entwicklungen
Speak quite righthttp://www.c2cjersey.com
Nice post! thank you for sharing!
WHAT DOES DUNIT STANDS FOR?
Clean,simple and it really works
Its seems like technical stuff
Men revalue your topic! To buy custom essay papers or custom papers about this topic will be broad source of information!