473,704 Members | 2,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unit test for GUI code

Dear All,

I was told that unit test is a powerful tool for progamming. If I am
writing a GUI code, is it possible to still using unit test?

I have a little experience in using unittest++. But I can not work out
a way to use it to test GUI code.

Thanks a lot!

Shuisheng

Feb 22 '07 #1
5 6523
On Feb 22, 11:06 pm, "shuisheng" <shuishen...@ya hoo.comwrote:
Dear All,

I was told that unit test is a powerful tool for progamming. If I am
writing a GUI code, is it possible to still using unit test?

I have a little experience in using unittest++. But I can not work out
a way to use it to test GUI code.

Thanks a lot!

Shuisheng
Do a google for 'TFUI' - its a good technique for your problem.

Aside from that, the main approach is to treat the GUI library like
any other 3rd party library - mainly, don't try and unit test it, unit
test your code that uses it.

By this, I mean: ensure all of your logic (business rules, gui
presentation rules, data retrival and updates, etc) are completed
separated from the gui code. In your gui's event handlers, don't do
anything except delegate to a plain old C++ Class.

By separating your code from the gui code, you can easily test your
code, without having to even link to the gui library.

In software, abstract is usually the way to solve a problem.

Andrew

Feb 22 '07 #2
I was told that unit test is a powerful tool for progamming. If I am
writing a GUI code, is it possible to still using unit test?
I have an approach based on some stuff I read in Michael Feather's
book. It is not as developed as what Andrew posted (for example, I
don't have a clever name for it), but it works for me.

I've been doing MFC programming, so I'll give my example there,
although it's just an example. In MFC, there's a class called CDC,
which is the C (language) device context. It has a bunch of primitive
drawing methods like:

CPoint MoveTo(int x, int y);

Now I'm writing a class C that does a bunch of drawing, so it winds up
calling these. It has a method OnDraw that takes a CDC* and does the
drawing (in turn calling a bunch of other methods). This is the
method I want to test.

So basically, I created a class called DCInterface, which looks like
this:
class DCInterface {
public:
virtual CPoint MoveTo(int x, int y) = 0;
/* etc. */
};

Then, I created a class CDCWrapper that calls the actual one:

class CDCWrapper : public DCInterface {
private:
CDC* pDC;
public:
explicit CDCWrapper(CDC* p) : pDC(p) {}
CPoint MoveTo(int x, int y) { return pDC->MoveTo(x, y); }
/* etc. */

Then I refactor my code for class C to use a DCInterface instead of a
CDC. (The OnDraw method stays, since the MFC framework calls it, but
now it just creates a CDCWrapper and forwards on to the wrapped
version of OnDraw.)

So far, everything is just setup for unit testing.

Now, here's the cool part. I created a TestDC class that implements
all of the functionality by storing a string with messages like
"MoveTo(5, 6) called.\n";

So to do unit tests, I set everything up with my TestDC class, and the
output is a string (instead of drawing something on the screen). I
can then do all my assertions that the appropriate series of draw
operations were called, based on looking at the output string.

This obviously doesn't do everything, but it lets me unit test some
fairly complex graphics routines.

More generally, you can pull out a wrapper that has virtual copies of
the library interface. Create an "real" implementation class that
just delegates to the library, then create a separate "test"
implementation class that stores things to a string (or some other
structure that's easy to use in unit tests).

There's a fair amount of work to create the wrappers in the first
place - I took the approach of "only wrap functions you actually use,"
but that's a one-time cost and then you can reuse it for your unit
testing.

Michael
Feb 23 '07 #3
On Feb 23, 2:11 am, "Michael" <mchlg...@aol.c omwrote:
I was told that unit test is a powerful tool for progamming. If I am
writing a GUI code, is it possible to still using unit test?

I have an approach based on some stuff I read in Michael Feather's
book. It is not as developed as what Andrew posted (for example, I
don't have a clever name for it), but it works for me.

I've been doing MFC programming, so I'll give my example there,
although it's just an example. In MFC, there's a class called CDC,
which is the C (language) device context. It has a bunch of primitive
drawing methods like:

CPoint MoveTo(int x, int y);

Now I'm writing a class C that does a bunch of drawing, so it winds up
calling these. It has a method OnDraw that takes a CDC* and does the
drawing (in turn calling a bunch of other methods). This is the
method I want to test.

So basically, I created a class called DCInterface, which looks like
this:
class DCInterface {
public:
virtual CPoint MoveTo(int x, int y) = 0;
/* etc. */

};

Then, I created a class CDCWrapper that calls the actual one:

class CDCWrapper : public DCInterface {
private:
CDC* pDC;
public:
explicit CDCWrapper(CDC* p) : pDC(p) {}
CPoint MoveTo(int x, int y) { return pDC->MoveTo(x, y); }
/* etc. */

Then I refactor my code for class C to use a DCInterface instead of a
CDC. (The OnDraw method stays, since the MFC framework calls it, but
now it just creates a CDCWrapper and forwards on to the wrapped
version of OnDraw.)

So far, everything is just setup for unit testing.

Now, here's the cool part. I created a TestDC class that implements
all of the functionality by storing a string with messages like
"MoveTo(5, 6) called.\n";

So to do unit tests, I set everything up with my TestDC class, and the
output is a string (instead of drawing something on the screen). I
can then do all my assertions that the appropriate series of draw
operations were called, based on looking at the output string.

This obviously doesn't do everything, but it lets me unit test some
fairly complex graphics routines.

More generally, you can pull out a wrapper that has virtual copies of
the library interface. Create an "real" implementation class that
just delegates to the library, then create a separate "test"
implementation class that stores things to a string (or some other
structure that's easy to use in unit tests).

There's a fair amount of work to create the wrappers in the first
place - I took the approach of "only wrap functions you actually use,"
but that's a one-time cost and then you can reuse it for your unit
testing.

Michael
Its a great book for working with un tested code, and for seeing how
to create 'seams' between areas like GUI libraries and our code.

For those interested in unit testing, a good guide that M Features,
myself and plenty of others use is:

A unit test does NOT :
* Use any file IO
* Connect to a db
* Present anything on screen
* communicate across a network.

The rational for this, is many, but boils down to forcing us to create
a design that is decoupled from these areas, which nearly always
results in a good, clean & highly cohesive design. For example, how
many times have we in the past put logic directly into a dialog
class? Where as with this style of testing, we force ourselves to
separate out the UI aspect from the logic part, resulting in us using
the MVC or MVP or Humble Dialog design patterns, as they allow us to
unit test th elogic without going anywhere near the GUI.

The second major win is speed of test runs.

I can run 1200 unit tests in 42.7 seconds currently (and these are
Java unit tests!) because of following these guidelines. And because
they run soo fast the Team runs ALL tests ALL of the time.
Slow tests mean we run them fewer times, the less we run them, the
longer it is before we find out we broken something. The longer time
before finding out we broke something, the more costly it is to
fix.....

Andrew

Feb 23 '07 #4
On Feb 22, 9:11 pm, "Michael" <mchlg...@aol.c omwrote:
I was told that unit test is a powerful tool for progamming. If I am
writing a GUI code, is it possible to still using unit test?

I have an approach based on some stuff I read in Michael Feather's
book. It is not as developed as what Andrew posted (for example, I
don't have a clever name for it), but it works for me.

I've been doing MFC programming, so I'll give my example there,
although it's just an example. In MFC, there's a class called CDC,
which is the C (language) device context. It has a bunch of primitive
drawing methods like:

CPoint MoveTo(int x, int y);

Now I'm writing a class C that does a bunch of drawing, so it winds up
calling these. It has a method OnDraw that takes a CDC* and does the
drawing (in turn calling a bunch of other methods). This is the
method I want to test.

So basically, I created a class called DCInterface, which looks like
this:
class DCInterface {
public:
virtual CPoint MoveTo(int x, int y) = 0;
/* etc. */

};

Then, I created a class CDCWrapper that calls the actual one:

class CDCWrapper : public DCInterface {
private:
CDC* pDC;
public:
explicit CDCWrapper(CDC* p) : pDC(p) {}
CPoint MoveTo(int x, int y) { return pDC->MoveTo(x, y); }
/* etc. */

Then I refactor my code for class C to use a DCInterface instead of a
CDC. (The OnDraw method stays, since the MFC framework calls it, but
now it just creates a CDCWrapper and forwards on to the wrapped
version of OnDraw.)

So far, everything is just setup for unit testing.

Now, here's the cool part. I created a TestDC class that implements
all of the functionality by storing a string with messages like
"MoveTo(5, 6) called.\n";

So to do unit tests, I set everything up with my TestDC class, and the
output is a string (instead of drawing something on the screen). I
can then do all my assertions that the appropriate series of draw
operations were called, based on looking at the output string.

This obviously doesn't do everything, but it lets me unit test some
fairly complex graphics routines.

More generally, you can pull out a wrapper that has virtual copies of
the library interface. Create an "real" implementation class that
just delegates to the library, then create a separate "test"
implementation class that stores things to a string (or some other
structure that's easy to use in unit tests).

There's a fair amount of work to create the wrappers in the first
place - I took the approach of "only wrap functions you actually use,"
but that's a one-time cost and then you can reuse it for your unit
testing.

Michael
Hi, Michael,

Thanks a lot for your reply. What I understand is that I write a class
to simulate the behavior of the GUI class. The new class uses some
string
information to substitute the graphical and interactive operations. Is
my
understanding right?

Thanks,

Shuisheng
include the Graphic

Feb 23 '07 #5
On Feb 23, 8:36 am, "shuisheng" <shuishen...@ya hoo.comwrote:
Thanks a lot for your reply. What I understand is that I write a class
to simulate the behavior of the GUI class. The new class uses some
string
information to substitute the graphical and interactive operations. Is
my
understanding right?
Yes, you basically write a class that you use only for testing that
makes it easier to test things.

And Andrew is right - if it's hard to do that, it indicates that maybe
you should move some of your logic into a different class (and have
your original class call it), where your new class is specifically
designed for testability.

And finally, the Feather book is truly wonderful. One of the best
I've read in the last year, maybe several years.

Michael

Feb 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1846
by: Shan | last post by:
Is there any tool available for generating unit test code in .NET. This is reverse engineering process, which we have to unit test methods in a class. Tool such as Nunit can test the methods only if we have unit test classes. Is there any tool available which can assist in developing these unit test classes.
5
2500
by: VvanN | last post by:
hi, fellows I'd like to intruduce a new unit test framework for C++ freely available at: http://unit--.sourceforge.net/ It does not need bothering test registration, here is an example // --- begin code ---
1
1153
by: prabhupr | last post by:
Hi All Using nUnit I can write Unit Test on my middle-tier code. Also, its not worth spending time on writing Unit Test on PRIVATE methods (its debatable topic:)) My question was, is there any easy way to identify / validate if nUnit has been written for each PUBLIC method. I'm looking for an easy report that list all methods for which there is no nUnit written
2
1871
by: shuisheng | last post by:
Dear All, I am using visual studipo 2005 (standard version which do not provide unit test tool) to develop some c++ code. I want to do unit test while I am coding. Anybody can suggest me an easy-to-use tool for unit tests? I appreciate your suggestion. Bests,
1
3331
by: batvanio | last post by:
Hi, I am writing unit tests in VS2005 and am having the following problem: I am trying to test a timeout property of one of my methods. This timeout exhibits itself in an exceptioin - i.e. I am sending a command to a hardware device, and if it does not answer in the timeout defined (i.e. in 2 sec), my software fires an exception. Now, I am writing an unit test in which I start a timer and I send the command. Note that I DO expect an...
176
8364
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
1
1798
by: rich_sposato | last post by:
I released version 2.0 of C++ Unit Test Library. You can download it from SourceForget.Net at http://sourceforge.net/projects/cppunittest/ .. I wrote this unit test library because other unit test frameworks always lacked features I considered important. Some only provided output in certain formats, and I had no easy way to choose my own format. Most provided no protection against exceptions. Only a few were useful for unit-testing...
48
2494
by: Ark Khasin | last post by:
Unit testing is an integral component of both "formal" and "agile" models of development. Alas, it involves a significant amount of tedious labor. There are test automation tools out there but from what limited exposure I've had, they are pricey, reasonably buggy, and require compiler/target adaptation. Out of my frustration with two out of two of them came my own. Its instrumentation approach is based solely on profound abuse of the C
6
5686
by: Vyacheslav Maslov | last post by:
Hi all! I have many many many python unit test, which are used for testing some remote web service. The most important issue here is logging of test execution process and result. I strongly need following: 1. start/end timestamp for each test case (most important) 2. immediate report about exceptions (stacktrace) 3. it will be nice to use logging module for output
0
8767
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8687
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9026
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6605
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5931
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4701
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2483
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2080
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.