Test Driven Developer
You got a TEST for that?
Home
|
Syndication
|
Sign In
Sunday, November 20, 2011
NUnit Tests for WinForms Application
Lately I have been doing work on WinForms applications and have been looking at ways to test them. We use unit tests to test at the method level, and [ideally] our code is written to be able to separate the logic code from the UI code. However, not *all* applications are written this way (unfortunately). Today I am in a situation where I must be able to test an application at the UI level. I need to bring up forms, type in text boxes, poke buttons, and inspect results in labels, etc.
The answer is NUnitForms! This fabulous test framework is an open source project that extends the functionality of NUnit, and allows us to test applications at the UI level. We don't consider this to be a unit test or even integration test mechanism, but rather a functional test mechanism. Rather than having testers manually go through screen after screen in the application just to see if it all works, we'll write some NUnit code that will automate it all for us.
So How do I do it?
SourceForge bits:
http://sourceforge.net/projects/nunitforms/files/nunitforms/
First, download the NUnitForms source code and add it to your solution. Then, write an NUnit test class, add a reference to the NUnitForms project and make your test class inherit from the NUnitFormsTest class. Add a reference to your application classes, and create a test. In the test, open your form with Form.Show() (so that it is visible). Then use a ButtonTester, a LabelTester, TextBoxTester, etc. to find each of the controls by name, and do the appropriate action to them. Then, after all of your actions are completed, call Application.DoEvents() to make sure to let everything have time to execute. You can then grab those text boxes, labels, etc. and Assert that the values are correct. That's it! Build it up step by step, and you have a fully automated end-to-end test!
If your forms are simple enough, you might even be able to use the Recorder application that comes with the codebase to record your keystroke and mouse actions as a C# code test. If your app has some startup code to accomplish, refactor it so that it's in simple methods, and include a hidden form in the app that your Recorder can open to accomplish the startup before opening other forms.
I have written and included sample code in a Visual Studio 2010 project in the zip file attached. This is a really great way to automate acceptance, functional, and end-to-end tests for a WinForms application project.
Sample code
Here
(5.5MB)
Automation
|
TDD
|
Testing
Sunday, November 20, 2011 4:40:51 PM (Pacific Standard Time, UTC-08:00)
Comments [0]
|
Trackback
Comments are closed.
© Copyright 2012, John E. Boal
Calendar
<
February 2012
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
Total Posts: 53
This Year: 0
This Month: 0
This Week: 0
Comments: 22
Archives
December, 2011 (1)
November, 2011 (1)
September, 2011 (1)
August, 2011 (1)
May, 2011 (1)
April, 2011 (1)
March, 2011 (0)
February, 2011 (1)
November, 2010 (1)
September, 2010 (1)
June, 2010 (1)
April, 2010 (1)
March, 2010 (1)
February, 2010 (1)
September, 2009 (1)
August, 2009 (1)
June, 2009 (1)
May, 2009 (1)
April, 2009 (1)
February, 2009 (1)
January, 2009 (2)
December, 2008 (2)
November, 2008 (2)
October, 2008 (2)
September, 2008 (4)
August, 2008 (3)
July, 2008 (5)
June, 2008 (4)
May, 2008 (1)
April, 2008 (2)
March, 2008 (4)
February, 2008 (2)
January, 2008 (1)
On this page
categories
ABN
Acceptance Criteria
ATDD
Automation
Bugs
C#
Continuous Integration
Mocks
MSTest
Python
Refactoring
Selenium
SQL
TDD
Testing
Tools
Unit Tests
Video
WatiN
Links
Home
Test Driven Development, Defined (Wikipedia)
Test Driven Design
Test-Driven.com - Agile development tools
NUnit
Book: Test-Driven Development in Microsoft .NET
CodeProject - Advanced Unit Testing: Unit Test Patterns
John Boal's Personal Blog
John Boal's Agile Development Blog
Blogroll
OPML
Lazy Coder
Scott Koon's Blog
#2782
Ade Miller's Tech Blog
Agile Development
Mitch Lacey's Agile Development Blog
Espresso Fueled Agile Development
Mike Puleio's Blog
Geek Noise
Noise de Peter Provost
Sneal's Blog
Shawn Neal's Blog
Search
About
© Copyright 2012, John E. Boal
Sign In