18
Test Driven Test Driven Development and NUnit Development and NUnit Testing Testing -Adam Main -Adam Main

Test Driven Development and NUnit Testing -Adam Main

Embed Size (px)

Citation preview

Page 1: Test Driven Development and NUnit Testing -Adam Main

Test Driven Development Test Driven Development and NUnit Testingand NUnit Testing

-Adam Main-Adam Main

Page 2: Test Driven Development and NUnit Testing -Adam Main

What is Test Driven What is Test Driven Development?Development?

2 Rules2 Rules• Write new code only when a test failsWrite new code only when a test fails• Eliminate duplicationEliminate duplication

Failing to compile is failing a testFailing to compile is failing a test

Page 3: Test Driven Development and NUnit Testing -Adam Main

Basic ProcessBasic Process

1. Write a test that fails1. Write a test that fails

2. Write code to pass the test2. Write code to pass the test

3. Refactor the code3. Refactor the code

4. If the program isn’t finished, goto 4. If the program isn’t finished, goto 11

Page 4: Test Driven Development and NUnit Testing -Adam Main

AdvantagesAdvantages

Concise and simpleConcise and simple Less manual testingLess manual testing Know specs and requirementsKnow specs and requirements Higher percent code coverageHigher percent code coverage Highly testable codeHighly testable code Coupling and cohesionCoupling and cohesion

Page 5: Test Driven Development and NUnit Testing -Adam Main

DisadvantagesDisadvantages

High number of compilesHigh number of compiles• Wastes timeWastes time• Less thoughtLess thought

Very little designVery little design• 15-20 minutes15-20 minutes• LanguageLanguage• HardwareHardware

Page 6: Test Driven Development and NUnit Testing -Adam Main

What is NUnit?What is NUnit?

Based off of xUnitBased off of xUnit• JUnit -JavaJUnit -Java

Basic testing frameworkBasic testing framework .NET environment.NET environment Uses metadataUses metadata Console and GUI testing appsConsole and GUI testing apps

Page 7: Test Driven Development and NUnit Testing -Adam Main

Why NUnit?Why NUnit?

Simple InterfaceSimple Interface Constant FeedbackConstant Feedback XML resultsXML results ConfigurableConfigurable

• SubsetsSubsets• PackagesPackages• SolutionsSolutions

Page 8: Test Driven Development and NUnit Testing -Adam Main

How do you use NUnit?How do you use NUnit?

Metadata attribute tagsMetadata attribute tags Assert library callsAssert library calls GUI or console applicationGUI or console application

Page 9: Test Driven Development and NUnit Testing -Adam Main

What are Attribute tags?What are Attribute tags?

.NET.NET [Attribute][Attribute] MetadataMetadata ““Compilable comments”Compilable comments”

Page 10: Test Driven Development and NUnit Testing -Adam Main

Attribute TagsAttribute Tags

[TestFixture] [TestFixture] [Test][Test] [TestFixtureSetUp][TestFixtureSetUp] [TestFixtureTearDown][TestFixtureTearDown]

Page 11: Test Driven Development and NUnit Testing -Adam Main

More Attribute TagsMore Attribute Tags

[ExpectedException(typeof(Exceptio[ExpectedException(typeof(ExceptionType))] nType))]

[Category("Category Name")] [Category("Category Name")] [Explicit] [Explicit] [Ignore(“reason”)][Ignore(“reason”)]

Page 12: Test Driven Development and NUnit Testing -Adam Main

What is the Assert LibraryWhat is the Assert Library

Created by NUnit teamCreated by NUnit team Static functionsStatic functions Assert classAssert class

Page 13: Test Driven Development and NUnit Testing -Adam Main

Assert FunctionsAssert Functions

3 Basic Overloads3 Basic Overloads -Assert.IsFalse( bool condition);

-Assert.IsFalse( bool condition, string message );

-Assert.IsFalse( bool condition, string message,

object[] parms ); One Assert per testOne Assert per test

• Short circuitShort circuit• Simple and concise testsSimple and concise tests

Page 14: Test Driven Development and NUnit Testing -Adam Main

Assert FunctionsAssert Functions

AreEqual(int actual, int expected)AreEqual(int actual, int expected) AreEqual(double actual, double expected)AreEqual(double actual, double expected) AreEqual(float actual, float expected)AreEqual(float actual, float expected) AreEqual(decimal actual, decimal expected)AreEqual(decimal actual, decimal expected) AreEqual(object actual, object expected)AreEqual(object actual, object expected) AreSame (object actual, object expected)AreSame (object actual, object expected)

Page 15: Test Driven Development and NUnit Testing -Adam Main

Assert FunctionsAssert Functions

IsTrue( bool condition) IsTrue( bool condition) IsFalse( bool condition)IsFalse( bool condition) IsNull( object obj)IsNull( object obj) IsNotNull( object obj)IsNotNull( object obj) Fail()Fail() Ignore()Ignore()

Page 16: Test Driven Development and NUnit Testing -Adam Main

ProjectsProjects

Load VS projects/solutionsLoad VS projects/solutions Save selected testsSave selected tests .nunit.nunit

Page 17: Test Driven Development and NUnit Testing -Adam Main

DemoDemo

Create a simple Create a simple Use TDDUse TDD Use NUnitUse NUnit

Page 18: Test Driven Development and NUnit Testing -Adam Main

SourcesSources

[1] NUnit - Documentation. [1] NUnit - Documentation. Retrieved March 28, 2005, from Retrieved March 28, 2005, from http://nunit.org/documentation.htmlhttp://nunit.org/documentation.html

[2] George, Boby and Laurie Williams. [2] George, Boby and Laurie Williams. An Initial Investigation of Test An Initial Investigation of Test Driven Development in IndustryDriven Development in Industry.. Retrieved March 30, 2005, from Retrieved March 30, 2005, from http://collaboration.csc.ncsu.edu/laurie/Papers/TDDpaperv8.pdfhttp://collaboration.csc.ncsu.edu/laurie/Papers/TDDpaperv8.pdf

[3] Ganssle, Jack. [3] Ganssle, Jack. XP DeconstructedXP Deconstructed. Dec 10, 2003. . Dec 10, 2003. http://www.embedded.com/showArticle.jhtml?articleID=16700086http://www.embedded.com/showArticle.jhtml?articleID=16700086

[4] Provost, Peter. [4] Provost, Peter. Test Driven Development in .NETTest Driven Development in .NET. . http://www.codeproject.com/dotnet/tdd_in_dotnet.asphttp://www.codeproject.com/dotnet/tdd_in_dotnet.asp

[5] [5] Introduction to Test Drive Development (TDD).Introduction to Test Drive Development (TDD). Retreived April Retreived April 1, 2005, from http://www.agiledata.org/essays/tdd.html1, 2005, from http://www.agiledata.org/essays/tdd.html

[6] Mugridge, Rick. [6] Mugridge, Rick. Test Driven Development and the Scientific Test Driven Development and the Scientific MethodMethod. . http://www.agiledevelopmentconference.com/2003/files/P6Paper.pdhttp://www.agiledevelopmentconference.com/2003/files/P6Paper.pdff