Refactoring Encapsulation and Unit Testing Lesson Two: Encapsulation and Unit Testing.
Unit Testing
Click here to load reader
description
Transcript of Unit Testing
Unit Testing
Gayani Tennakoon Software Engineer@ 99XTechnology
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..
The partner of choice for ISV's
What is 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
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
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
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
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 ?”
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
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”)
}
}
The partner of choice for ISV's
Nunit Overview
• Learn more about Nunit
go to http://www.nunit.org
The partner of choice for ISV's
Its Time To Write Some Unit Tests
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.
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
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
The partner of choice for ISV's
Thank You !