Unit Testing

16

Click here to load reader

description

This presentation explains what is unit testing, why unit testing is important and gives a head start on how to implement unit testing in your .NET projects using NUnit test framework.

Transcript of Unit Testing

Page 1: Unit Testing

Unit Testing

Gayani Tennakoon Software Engineer@ 99XTechnology

Page 2: Unit Testing

The partner of choice for ISV's

Plan for Today

• What is Unit Testing• Why Unit Tests• Best Practices for effective Unit Testing• Considerations in Unit Testing• Nunit overview• Writing Unit Tests – Practical Session• Exercise..

Page 3: Unit Testing

The partner of choice for ISV's

What is Unit Testing

Page 4: Unit Testing

The partner of choice for ISV's

Unit Testing

• Developers do unit testing

• Write a code to test a small specific area of functionality

• It helps to prove the functionality

Page 5: Unit Testing

The partner of choice for ISV's

Why Unit Tests

• Better Code• Better Design• Code is easier to maintain later• Confidence when you code• Make your life easy

Page 6: Unit Testing

The partner of choice for ISV's

Best Practices for Effective Unit Testing

• Use Good ToolsTesting Focused Tools –Nunit, MsTest,Test Driven .NET

Supporting Tools -Ms Build, Resharper

• Use a Test List• Get a Mentor• Automate Your Test Execution

Page 7: Unit Testing

The partner of choice for ISV's

Considerations in Unit Testing

What to test

You need to answer

“If the code ran correctly, how would I know?”

You can test• Test at the boundaries• Test every error message• Test different configurations• Run tests that are annoying to setup• Avoid redundant tests

Page 8: Unit Testing

The partner of choice for ISV's

Considerations in Unit Testing

Test Structure

William Wake’s 3-A Pattern

– Arrange

– Act

– Assert Designing For Test

“How am I going to test this ?”

Page 9: Unit Testing

The partner of choice for ISV's

Nunit Overview

• Open source Unit Testing framework • Attributes 

• indicate test classes and methods • modify behavior of classes and methods

• Assertions • test an actual value against expected value

Page 10: Unit Testing

The partner of choice for ISV's

Nunit Overview[TestFixture]

public class TestFixtureLifetime{    [TestFixtureSetUp]    public void BeforeTest()    {

// single set of functions that are performed once prior to executing any of the tests in the text fixture

}

    [TestFixtureTearDown]    public void AfterTest()    {

// single set of functions that are performed once after to executing any of the tests in the text fixture

}

    [Test]    public void Test1()    {

int expectedResult=4;

int actualResult =squareRoot(16);

Assert.AreEqual(expectedResult,actualResult,”Error Message”)

}

   }

Page 11: Unit Testing

The partner of choice for ISV's

Nunit Overview

• Learn more about Nunit

go to http://www.nunit.org

Page 12: Unit Testing

The partner of choice for ISV's

Its Time To Write Some Unit Tests

Page 13: Unit Testing

The partner of choice for ISV's

Exercise

• Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

• Write unit tests for your implementation

• You will be given 15 min.

Page 14: Unit Testing

The partner of choice for ISV's

References

http://bradwilson.typepad.com/presentations/effective-unit-testing.pdf

http://www.testdriven.com http://www.xprogramming.com http://workspaces.gotdotnet.com/tdd

Page 15: Unit Testing

The partner of choice for ISV's

References

• The Pragmatic Programmer

Andy Hunt and Dave Thomas• Test-Driven Development in Microsoft .NET

Jim Newkirk and Alexei Vorontsov• Test-Driven Development, by Example

Kent Beck• Pragmatic Unit Testing in C# with NUnit

Andy Hunt and Dave Thomas• Working Effectively With Legacy Code

Michael Feathers• Refactoring

Martin Fowler• Lessons Learned in Software Testing

Cem Kaner, James Bach, and Brett Pettichord

Page 16: Unit Testing

The partner of choice for ISV's

Thank You !