Darrell Norton's Blog [MVP]

Sponsors

The Lounge

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Unit Testing a Windows Forms or Console Application

Juanma asked me a question about testing exe files, such as WinForms or Console apps, in response to my post How do you setup NUnit tests in Visual Studio.NET projects?  The problem is you cannot add a reference to an executable assembly.

One easy trick is to change the project output type to Class Library.  To do this, open the properties for the WinForm or Console project, and in the dropdown for Output Type, select Class Library.  If you add a WinForm project, you will get an error message.  To fix it, simple add a reference to the System.Windows.Forms assembly in the unit test project.

Then you can add a reference to your WinForm project and test any public methods just like a dll.  When you change the Output Type back to Windows Application and rebuild, you’ll get the “cannot reference an exe” error message again.  You can dereference the windows application again, but it’s easier to not build the unit tests project.  Go to the Tools - Configuration Manager menu and deselect the unit test project(s).  Then the unit tests will not be built, and you won’t get the error.

Alternatively, you can separate out all your business logic into a separate project that is a class library and not have any of these problems.  However, this is not always feasible, and the trick above works well enough.

UPDATE:  Jeff Key says to move just the Main method into a separate WinForms project, and then convert your original project into a Class Library.  Then you won't have any conversion issues!


Posted 03-04-2004 11:17 AM by Darrell Norton

[Advertisement]

Comments

Jeff Key wrote re: Unit Testing a Windows Forms or Console Application
on 03-07-2004 1:32 PM
I'll never understand why you can't ref an exe. Insane. Another alternative is take our your Main method, move it to its own WinForms project, then convert your project to a Class Library project. With this configuration there's no need to change your build configuration and your tests will always build.
Darrell wrote re: Unit Testing a Windows Forms or Console Application
on 03-07-2004 1:46 PM
Nice tip Jeff! That is a very good idea.
qwerty wrote re: Unit Testing a Windows Forms or Console Application
on 03-10-2004 11:18 AM
Good blog!
Darrell wrote re: Unit Testing a Windows Forms or Console Application
on 03-10-2004 12:16 PM
qwerty - if that's your *real* name, thanks.
Darrell Norton's Blog wrote NUnitForms released
on 04-14-2004 5:41 AM
NUnitForms released
Jose Almeida wrote re: Unit Testing a Windows Forms or Console Application
on 05-19-2004 6:58 AM
Not beeing able to reference an exe is a limitation of the IDE only, not of the compilers.
What I've done is simply create a makefile that executes csc adding the .exe assembly as a reference to the NUnit test fixture assembly with /reference: (/r:) just like any other assembly.
Darrell wrote re: Unit Testing a Windows Forms or Console Application
on 05-19-2004 7:29 AM
Jose - unfortunately yes. I found that out later now as I am doing Windows Forms work. Such a sucky implementation.
Dhominator wrote re: Unit Testing a Windows Forms or Console Application
on 10-19-2004 5:16 AM
I've been using the "launcher" project approach for a while... but eventually some functionality creeps into that one as well... sign.

What bothers me [almost] as much as the IDE not being able to ref a .exe is that it's simply a file extension convention AFAIK. Meaning you can just copy the .exe to a .dll in the postbuild event and ref that from unit tests. Good grief :p
Darrell wrote re: Unit Testing a Windows Forms or Console Application
on 10-19-2004 6:27 AM
Yep, VS is a pain sometimes. :)