When I first started using unit tests, I ran into two problems. Firstly, he could test private methods and fields, and secondly, he lagged in keeping unit tests up to date when rapid development took place. Therefore, I took the approach below for my unit tests.
#if UNITTEST using NUnit.Framework; #endif public class MyBlackMagic { private int DoMagic() { return 1; } #if UNITTEST [TestFixture] public class MyBlackMagicUnitTest { [TestFixtureSetUp] public void Init() { log4net.Config.BasicConfigurator.Configure(); } [Test] public void DoMagicTest() { Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name); Assert.IsTrue(DoMagic() == 1, "You are not a real magician!"); } } #endif }
I find that the approach overcomes my two problems, and thatβs the click of a pre-compiler to make sure all unit tests are compiled.
Now my problem is that I'm moving on to a new project, which is about using separate assemblies for unit tests. Before I dive in and begin to state the merits of the inner class approach, as shown above, I would like to know if anyone thinks he has flaws?
Edit:
Just add a couple of points around some of the shortcomings mentioned:
- A single test code will never affect production code because the pre-compiler UNITTEST flag is disabled
- Unit test code does not make the main code less clear, because it is placed at the bottom of each class and wrapped in a Visual Studio scope directive.
- I believe that the inner unit test class means that the main class is actually simpler because there are no additional methods or properties that should be exposed only for testing. There will always be cases when you want to check some internal state of a class as part of a unit test sooner or later ...
c # unit-testing nunit
sipwiz
source share