180 likes | 535 Views
Test Driven Development and NUnit Testing. -Adam Main. What is Test Driven Development?. 2 Rules Write new code only when a test fails Eliminate duplication Failing to compile is failing a test. Basic Process. 1. Write a test that fails 2. Write code to pass the test
E N D
Test Driven Development and NUnit Testing -Adam Main
What is Test Driven Development? • 2 Rules • Write new code only when a test fails • Eliminate duplication • 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 • Concise and simple • Less manual testing • Know specs and requirements • Higher percent code coverage • Highly testable code • Coupling and cohesion
Disadvantages • High number of compiles • Wastes time • Less thought • Very little design • 15-20 minutes • Language • Hardware
What is NUnit? • Based off of xUnit • JUnit -Java • Basic testing framework • .NET environment • Uses metadata • Console and GUI testing apps
Why NUnit? • Simple Interface • Constant Feedback • XML results • Configurable • Subsets • Packages • Solutions
How do you use NUnit? • Metadata attribute tags • Assert library calls • GUI or console application
What are Attribute tags? • .NET • [Attribute] • Metadata • “Compilable comments”
Attribute Tags • [TestFixture] • [Test] • [TestFixtureSetUp] • [TestFixtureTearDown]
More Attribute Tags • [ExpectedException(typeof(ExceptionType))] • [Category("Category Name")] • [Explicit] • [Ignore(“reason”)]
What is the Assert Library • Created by NUnit team • Static functions • Assert class
Assert Functions • 3 Basic Overloads -Assert.IsFalse( bool condition); -Assert.IsFalse( bool condition, string message ); -Assert.IsFalse( bool condition, string message, object[] parms ); • One Assert per test • Short circuit • Simple 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 • Load VS projects/solutions • Save selected tests • .nunit
Demo • Create a simple • Use TDD • Use NUnit
Sources • [1] NUnit - Documentation. Retrieved March 28, 2005, from http://nunit.org/documentation.html • [2] George, Boby and Laurie Williams. An Initial Investigation of Test Driven Development in Industry.Retrieved March 30, 2005, from http://collaboration.csc.ncsu.edu/laurie/Papers/TDDpaperv8.pdf • [3] Ganssle, Jack. XP Deconstructed. Dec 10, 2003. http://www.embedded.com/showArticle.jhtml?articleID=16700086 • [4] Provost, Peter. Test Driven Development in .NET. http://www.codeproject.com/dotnet/tdd_in_dotnet.asp • [5] Introduction to Test Drive Development (TDD). Retreived April 1, 2005, from http://www.agiledata.org/essays/tdd.html • [6] Mugridge, Rick. Test Driven Development and the Scientific Method. http://www.agiledevelopmentconference.com/2003/files/P6Paper.pdf