This Document provides an introduction to Unit Testing with emphasis on Test Driven Development (TDD). Here I included the NUnit and csUnit as examples For X-unit test Frame works. I will include VSTS Unit test frame work in my forth coming posts .
Unit Testing
Unit testing aims to ensure that each individual part of a program works correct in isolation. Unit testing should assure correct behaviour of the given unit before it is integrated with other units.
Test Units
Which kind of program units are tested?
What is the smallest testing unit?
Program units for which there exists at least one test case
Possible Units are :
Single commands and expressions
Procedures, functions and methods
Types (classes, structs)
Packages (namespaces, assemblies)
In an object-oriented program, the test units are the individual, public operations in each class
Unit Test Concepts
It is useful to have a common understanding of the concepts behind Unit testing
· Assert
· An activation of a boolean function which represents an atomic test execution
· Test
· A single test that may contain a number of asserts
· Test case
· An aggregation of tests - Tests of an entire class or method
· Test suite
· An aggregation of test cases
Tests, Test cases and Test suites may be modeled as Composites
Unit Test Recommendation
Test case independence
Each test should examine a single program aspect - in isolation from other aspects
Test simplicity
Test code should be simple
Testing of test code is simply too much
Test coverage
Each operation in a class should have at least one associated test
Typically a number of tests per operations
Regression
For each modification - at a reasonable grain size - re-execute all test cases .
Test of Object-oriented programs
What are the basic challenges of testing object-oriented programs?
· Bottom-up OOP development
· Makes it natural to test the classes bottom-up
· Bottom-up testing of classes relieves the need for stubs
· Information hiding
· Makes it difficult/impossible to directly observe the effect of operations on an object
· Dynamic binding and polymorphism
· May contribute with a lot additional independent paths
NUnit for C#
NUnit is a unit testing framework for C#
· NUnit characteristics
· Pieces of test programs are marked by means of particular attributes
· Nunit provides a large amount of different assertions
· The NUnit tools are able to execute the marked test in one or more assemblies (dll files)
· Nunit tools
· A Console Runner
· A GUI Runner
Attributes
Attributes offer a mechanism that allows the programmer to extend the programming language in simple ways.
Attributes can be accessed by the compiler and - at run-time - by the interpreter
| |||
· The attribute Obsolete is defined by a predefined class ObsoleteAttribute
· A subclass of System.Attribute
· Positional attribute parameters correspond to constructor parameters
· Named attribute parameters specify assignment to properties
| NUnit Attributes · [TestFixture] · Marks a class that contains tests · [Test] · Marks a method in test class · [SetUp] · Marks a method which is executed before each test method. At most one such method · [TearDown] · Marks a method which is executed after each test method. At most one such method · [TestFixtureSetUp] · Marks a method which is executed once before execution of any test method. At most one such method · [TestFixtureTearDown] · Marks a method which is executed once after all tests are completed. At most one such method · [ExpectedException(ExceptionType)] · Marks a test which is expected to lead to a given Exceptiontype · [Category("name")] · Categorizes a given test fixture or test · [Ignore] · Marks a test method or a test class which (temporarily) should not be executed
NUnit Assertions · Assert.AreEqual(expected, actual) · 18 overloads. With/without message, tolerance, object params · Assert.AreNotEqual(expected, actual) · 18 overloads. With/without: message · Assert.AreSame(expected, actual) · 3 overloads. With/without: message, object params · Assert.NotSame(expected, actual) · 3 overloads. With/without: message, object params · Assert.Contains(object, anIList) · 3 overloads. With/without: message, object params · Assert.Greater(arg1, arg2) · 18 overloads. With/without: message, object params · Assert.Less(arg1, arg2) · 18 overloads. With/without: message, object params · Assert.IsInstanceOfType(Type, object) · 3 overloads. With/without: message, object params · Assert.IsNotInstanceOfType(Type, object) · 3 overloads. With/without: message, object params · Assert.IsAssignableFrom(Type, object) · 3 overloads. With/without: message, object params · Assert.IsNotAssignableFrom(Type, object) · 3 overloads. With/without: message, object params · Assert.IsTrue(aBool) · 3 overloads. With/without: message, object params · Assert.IsFalse(aBool) · 3 overloads. With/without: message, object params · Assert.IsNaN(aDouble) · 3 overloads. With/without: message, object params · Assert.IsEmpty(aString) · 3 overloads. With/without: message, object params · Assert.IsEmpty(anICollection) · 3 overloads. With/without: message, object params · Assert.IsNotEmpty(aString) · 3 overloads. With/without: message, object params · Assert.IsNotEmpty(anICollection) · 3 overloads. With/without: message, object params · Assert.Fail() · 3 overloads. With/without: message, object params · StringAssert.Contains(expectedString, actualString) · 3 overloads. With/without: message, object params · StringAssert.StartsWith(expectedString, actualString) · 3 overloads. With/without: message, object params · StringAssert.EndsWith(expectedString, actualString) · 3 overloads. With/without: message, object params · StringAssert.AreEqualIgnoringCase(expectedString, actualString) · 3 overloads. With/without: message, object params |
A Failure and a Pass Case of a sample Test run done by me using N -Unit are Shown bellow:
1,http://baccoubonneville.com/blogs/index.php/dotnet/2006/01/10/unit-testing-in-net
2,http://www.c2.com/cgi/wiki?UnitTest
3,http://research.microsoft.com/pex/
4,http://www.codedisplay.com/Code/9720.aspx#lnum234
5,http://www2.sys-con.com/itsg/virtualcd/dotnet/archives/0112/hurtt/index.html
Happy Reading & Stay Tuned



No comments:
Post a Comment