1. Download and install nunit (the latest win version is NUnit-2.6.0.12051.msi) http://www.nunit.org/index.php? P = download
2. Download and install visual nunit 2010 plug-in http://visualstudiogallery.msdn.microsoft.com/c8164c71-0836-4471-80ce-633383031099 for
Note: you can find and open the plug-in through View> other window in vs (shortcut: Ctrl + F7)
3. Create a test project unittestapp (for example, a simple console application) and introduce nunit. framework class library (path of the file after installation by default: C: \ Program Files \ nunit 2.6 \ bin \ framework \ nunit. framewor. DLL)
4. Add the calculator class to implement a simple addition and acquiring Singleton Method
Calculatornamespace UnitTestApp{ public class Calculator { public int Add(int a, int b) { return a + b; } private static readonly object objSync = new object(); private static Calculator instance = null; public static Calculator GetInstance() { if (instance == null) { lock (objSync) { if (instance == null) { instance = new Calculator(); } } } return instance; } }}
5. added the nunittest class for unit testing.
UnitTestusing NUnit.Framework;namespace UnitTestApp{ [TestFixture] public class NUnitTest { [Test] public void AddTest() { var calc = new Calculator(); var result = calc.Add(1, 1); Assert.AreEqual(2, result); } [Test] public void AddTestFailure() { var calc = new Calculator(); var result = calc.Add(1, 1); Assert.AreEqual(10, result); } [Test] public void SingtonTest1() { var calc = Calculator.GetInstance(); Assert.IsNull(calc); } [Test] public void SingtonTest2() { var calc1 = Calculator.GetInstance(); var calc2 = Calculator.GetInstance(); Assert.IsTrue(object.Equals(calc1, calc2)); } }}
Compile the console project, click the "run" button of the visual nunit plug-in, and everything is quiet.
Note: In the above Code, nunit. Framework is referenced and testfixture is used to mark the class used for testing. test is used to represent a specific test case. We can see that the most important thing in unit testing is assertions. The rest is completely handed over to the Framework automation. Test results can be seen in a timely manner through the plug-in:
Download Demo: unittestapp
Reference: http://www.cnblogs.com/leoo2sk/archive/2010/01/13/pragmatic-unit-testing-with-nunit.html
Http://www.cnblogs.com/dudu/archive/2011/07/26/unit_testing_constructor_dependency_injection.html
Integrate nunit for unit testing in vs2010