Test Driven Development and NUnit Testing -Adam Main
What is Test Driven Development? b 2 Rules Write new code only when a test failsWrite new code only when a test fails Eliminate duplicationEliminate duplication b Failing to compile is failing a test
Basic Process 1. Write a test that fails 2. Write code to pass the test 3. Refactor the code 4. If the program isn’t finished, goto 1
Advantages b Concise and simple b Less manual testing b Know specs and requirements b Higher percent code coverage b Highly testable code b Coupling and cohesion
Disadvantages b High number of compiles Wastes timeWastes time Less thoughtLess thought b Very little design minutes15-20 minutes LanguageLanguage HardwareHardware
What is NUnit? b Based off of xUnit JUnit -JavaJUnit -Java b Basic testing framework b.NET environment b Uses metadata b Console and GUI testing apps
Why NUnit? b Simple Interface b Constant Feedback b XML results b Configurable SubsetsSubsets PackagesPackages SolutionsSolutions
How do you use NUnit? b Metadata attribute tags b Assert library calls b GUI or console application
What are Attribute tags? b.NET b [Attribute] b Metadata b “Compilable comments”
Attribute Tags b [TestFixture] b [Test] b [TestFixtureSetUp] b [TestFixtureTearDown]
More Attribute Tags b [ExpectedException(typeof(ExceptionType))] b [Category("Category Name")] b [Explicit] b [Ignore(“reason”)]
What is the Assert Library b Created by NUnit team b Static functions b Assert class
Assert Functions b 3 Basic Overloads -Assert.IsFalse( bool condition); -Assert.IsFalse( bool condition, string message ); -Assert.IsFalse( bool condition, string message, object[] parms ); b One Assert per test Short circuitShort circuit Simple and concise testsSimple and concise tests
Assert Functions AreEqual(int actual, int expected) AreEqual(double actual, double expected) AreEqual(float actual, float expected) AreEqual(decimal actual, decimal expected) AreEqual(object actual, object expected) AreSame (object actual, object expected)
Assert Functions IsTrue( bool condition) IsFalse( bool condition) IsNull( object obj) IsNotNull( object obj) Fail() Ignore()
Projects b Load VS projects/solutions b Save selected tests b.nunit
Demo b Create a simple b Use TDD b Use NUnit
Sources b [1] NUnit - Documentation. Retrieved March 28, 2005, from b [2] George, Boby and Laurie Williams. An Initial Investigation of Test Driven Development in Industry. Retrieved March 30, 2005, from b [3] Ganssle, Jack. XP Deconstructed. Dec 10, b [4] Provost, Peter. Test Driven Development in.NET. b [5] Introduction to Test Drive Development (TDD). Retreived April 1, 2005, from b [6] Mugridge, Rick. Test Driven Development and the Scientific Method.