Last post Dec 23, 2010 11:00 AM by LudovicoVan
Aug 17, 2010 09:04 AM|misuk11|LINK
I have an app that I am testing with NUNit. The project im testing has several helper classes that are created as public static readonly. When I run the NUnit tests, they all fail with the same error
Dec 22, 2010 01:08 AM|m2shash|LINK
I was writing Nunit tests for an APP which had to get some values from application config file.
But when ran Test Cases, they all failed with this error:
System.TypeInitializationException : The type initializer for
'SalesTaxes.Utility.Constants' threw an exception.
Then I tried so many things and found out the way how to tell NUnit to use an application configuration file.
This is easy, just have a configuration file with the same name as your test assembly + ".config" sitting next to your assembly and NUnit automatically picks it up. This is NUnit's built-in behavior and has nothing to do with TestDriven.NET, so it works
for any way you invoke NUnit such as NUnit GUI, NUnit console, or TestDriven.NET.
Eg:- My dll to test was "SalesTaxTest" so the config file has to be named as "SalesTaxTest.dll.config"
The problem is getting your NUnit config file into those directories without having to maintain two copies. The answer is Build Events, specifically post-build events.
Here is the steps I followed to test the App.config file by NUNIT.
1. add an App.config file to your unit test project.
2. Copy everything from your current AssemblyName.dll.config file into the App.config file.The problem is if your project is a class library, Visual Studio won’t
rename the file and copy it to the output directory like it does with a Windows Forms project.
3. Now right-click on the unit test project, select Properties, and select Build Events. Click the ellipses (…) next to Post-build Event Command Line. This will open
up a batch file editor. Click the Macros >> button to see which macros are available to you. It even shows you the values you can expect given your current project.
4. Just Copy thi post-build command and paste it into post-build events .
copy /Y “$(ProjectDir)App.config” “$(TargetDir)$(TargetFileName).config”
Now when you rebuild your project, the App.config file will be renamed to AssemblyName.dll.config and copied to the output directory so that NUnit can access it for configuration information.
Dec 23, 2010 11:00 AM|LudovicoVan|LINK
I agree with the error analysis already provided. As to a "solution", it might rather be worth rethinking your architecture a little bit: in fact, some dependencies are better "injected" than local...