One of the important things missing in Microsofts Visual Studio Express Editions is the option to create Unit Tests. Fortunately there are some free Unit Test Tools, like xUnit.net, ExpressUnit, and lots more. Undoubtedly the best-known Unit Test Tool is NUnit. It’s written in C# and works perfect with current .net Frameworks. Setting up the tests is easy and there are several examples on their webpage, but it’s a bit complicated to get it more convenient. After some hours I faced 2 main questions:
- How could I access my app.config for the TestProject dll?
- How could I debug my tests?
Fortunately both questions could be answered easily.
By default I got the following error message in NUnit when I try to access a connnectionstring from my app.config:
System.Configuration.ConfigurationErrorsException : The configuration section for Logging cannot be found in the configuration source.
Reading some internet resources it seems that NUnit should automatically find the app.config if it’s named correct (TestProject.dll.config). Unfortunately this was not the case…. Then I found a blog entry from Cory Foy, explaining that my .config was at the wrong directory. I had the same project structure as Cory, so I had to place the config in another place. Another useful tip found in the comments of Corys blog: Just open the TestProject.dll using NUnit, and all references are set correct automatically. Excellent!
- Open the .vbproj.user file of my TestProject.
- Add the following lines to the PropertyGroup-Tag:
- Open the solution again and set the TestProject as Startup Project.
- Set the breakpoints to the lines where I want to start debug.
- Press F5 to start the TestProject. Now NUnit will open.
- Select the test you want to debug and click ‘Run’ in NUnit. The code will stop as soon as it reaches a breakpoint and you could debug the code now.
So now both is working, using app.config and debug Tests. The only problem: When NUnit opens from Debugger, the path to the app.config is not set correct. Instead of building a copy-job, I’ve just saved the working NUnit configuration for my project once (simply by opening the Testproject.dll using NUnit and saving the project to my main directory). As soon as Visual Studio starts NUnit, I just open the saved TestProject.Nunit in this instance. Now the app.config is recognized correct and I could debug my code.