Test Driven Development
Test Driven Development
ppt
30-05-07
Content
When use tests? Why automate testing? We have specifications? What is Test Driven Development? How to write tests? Unit Testing TestDriven.Net Nunit
http://www.khbo.be/~peuteman/TDD.html
2/44
30-05-07
Contradictory to
Don't touch it, it might break Fragile system
4/44
30-05-07
Manual testing
inserting output statements
To follow flow
Do we reach this statement?
To verify calculations
What is the current value of this variable
5/44
Manual testing..
Running the application in debugger
Insert breakpoint at start of suspicious code Step through code Watch evolution of local variables
30-05-07
Manual testing..
With manual testing, testing only done with specific values
Those values causing the current error
If testing is manual
insert some extra work now to write the tests
You do not get it for free
Automated testing
Automated tests..
run as many times as needed
Without complaining
30-05-07
Benefits
Benefits of automated tests
We test the base functionality of our application The tests are repeatable Our co-workers can verify how we tested Serves partly as "documentation"
Show several ways of how to call a method
3. We have specifications?
Specifications are a very good source to write tests Strategy:
Select a post condition
Tests do not start from pre conditions and class invariants
Turn into a test If we are not able to turn the specification into a test
Then the specification is unclear
Rewrite the post condition
30-05-07
Repeat cycle
12/44
30-05-07
13/44
30-05-07
15/44
30-05-07
XP most used
XP most used principles:
Simple Design Pair Programming Test Driven Development Design Improvement
Extreme Programming
Simplicity is the key
Structure your software system such that it can handle all current issues - and only those - in an elegant way
Do not try to anticipate future elements
Example: do not partition a class of persons into subclasses of men and women, if such a separation is not needed right now
18/44
30-05-07
Extreme Programming
Pair programming
All code is produced by pairs of two programmers working on the same machine
The best way to pair program is to just sit side by side in front of the monitor
Slide the key board and mouse back and forth
One person types and thinks tactically about the method being created
The other person thinks strategically about how that method fits into the class
19/44
Extreme Programming
Refactor mercilessly, Design Improvement
Restructure your software system as soon as it no longer handles all current issues in the most elegant way
Split a class into two or more classes if it turns out to define groups of different objects
Turn a class of persons into a hierarchy involving an abstract class of persons and concrete subclasses of men and women
10
30-05-07
21/44
22/44
11
30-05-07
Pragmatic Unit Testing in C# with NUnit, 2nd Ed. Andy Hunt and Dave Thomas
23/44
These tests are written in accordance to the specification These tests focus on the end-user These tests typically lead to 80% coverage
24/44
12
30-05-07
These tests are written in accordance to the implementation These tests focus on the developer With these tests we will focus on the 20% code not covered by black box tests
25/44
26/44
13
30-05-07
Make sure your tests are non destructive Make your tests repeatable
Do not change persistent state so other tests can't run, app behaviour change All state added during the test should be discarded for further tests Implement tearDown Everyone who changes the code can run the tests Do one thing And do it good
28/44
14
30-05-07
6. Unit Testing
What is unit testing?
Programmer tests Tests on small parts, units Tests below the user interface layer
Interface based (Not GUI) Lead to component decoupling
29/44
NUnit
.Net languages
Unit
Simple to use
Command Line NUnit GUI Visual Studio Add-In
TestDriven.Net csUnit
30/44
15
30-05-07
7. TestDriven.Net
Allows Ad Hoc Testing of code Uses internally Nunit Includes Cover Explorer
Line coverage
Simple to measure
Weakest form of coverage
8. NUnit
How does NUnit work? Features of NUnit Generating Unit tests: Pex
32/44
16
30-05-07
Account
33/44
34/44
17
30-05-07
Attributes [ ]
The test-code is specially annotated using custom attributes From nunit.framework.dll
Fixtures [ TestFixture ]
Marks a class as containing tests Restrictions: public class + default constructor Constructor without side effects
is called multiple times
Assertions
Our code contains assertions To demonstrate the correct working of the application
Configuration files
Different settings for test as settings in production
36/44
18
30-05-07
Features of NUnit
Attributes Assertions
37/44
38/44
19
30-05-07
Extra classes:
StringAssert CollectionAssert FileAssert
39/44
Features of NUnit:
Equality Asserts (value) Identity Asserts (reference) Comparison Asserts
Assert Class
Assert.Are[Not]Equal( int expected, int actual ); Assert.Are[Not]Equal( object expected, object actual ); Assert.AreSame( object expected, object actual ); Assert.Contains( object anObject, IList collection ); Assert.Greater[OrEqual]( int arg1, int arg2 ); Assert.Greater[OrEqual]( IComparable arg1, IComparable arg2 ); Assert.Less[OrEqual]( int arg1, int arg2 ); Assert.Less[OrEqual]( IComparable arg1, IComparable arg2 );
Type Asserts
Condition tests
Assert.Is[Not]InstanceOfType( Type expected, object actual ); Assert.Is[Not]AssignableFrom( Type expected, object actual ); Assert.Is{True,False}( bool condition ); Assert.Is[Not]Null( object anObject ); Assert.IsNaN( double aDouble ); Assert.Is[Not]Empty( string aString ); Assert.Is[Not]Empty( ICollection collection );
Utility Methods
Assert.Fail(); Assert.Ignore();
40/44
20
30-05-07
Features of NUnit:
StringAssert CollectionAssert
extra classes
StringAssert.Contains( string expected, string actual ); StringAssert.{Starts,Ends}With( string expected, string actual ); StringAssert.AreEqualIgnoringCase( string expected, string actual ); CollectionAssert.AllItemsAreInstancesOfType( Collection collection, Type expectedType ); CollectionAssert.AllItemsAreNotNull( Collection collection ); CollectionAssert.AllItemsAreUnique( Collection collection ); CollectionAssert.Are[Not]Equal( Collection expected, Collection actual ); CollectionAssert.Are[Not]Equivalent( Collection expected, Collection actual); CollectionAssert.[DoesNot]Contain[s]( Collection expected, object actual ); CollectionAssert.Is[Not]SubsetOf( Collection subset, Collection superset ); CollectionAssert.Is[Not]Empty( Collection collection ); FileAssert.Are[Not]Equal( Stream expected, Stream actual ); FileAssert.Are[Not]Equal( FileInfo expected, FileInfo actual ); FileAssert.Are[Not]Equal( string expected, string actual );
FileAssert
41/44
http://research.microsoft.com/pex/
42/44
21
30-05-07
43/44
Remember
Program testing can be a very effective way to show the presence of bugs, but is hopelessly inadequate for showing their absence.
Edsger W. Dijkstra EWD 340: The humble programmer published in Commun. ACM 15 (1972)
44/44
22