Unit-Testing: Presented by Benny Pasternak November 2005
Unit-Testing: Presented by Benny Pasternak November 2005
Presented by
Benny Pasternak
November 2005
Introduction
– Definition
– Why bother?
– eXtreme Unit Testing
Unit Test Patterns
Some Best practices
Testing Frameworks
Conclusion
2
Definitions
3
Why even bother?
4
Why even bother? (continued)
pass/fail patterns
collection management patterns
data driven patterns
performance patterns
simulation patterns
multithreading patterns
stress test patterns
presentation layer patterns
process patterns
9
Pass/Fail Patterns
Simple-Test Pattern
Code-Path Pattern
Parameter-Range Pattern
10
Simple Test-Pattern
11
Code-Path Pattern
13
Data Driven Test Patterns
Simple-Test-Data Pattern
Data-Transformation-Test Pattern
14
Simple-Test-Data Pattern
16
Data Transaction Patterns
Simple-Data-I/O Pattern
Constraint Data Pattern
The Rollback Pattern
17
Simple-Data-I/O Pattern
18
Constraint Data Pattern
20
Collection Management Patterns
Collection-Order Pattern
Enumeration Pattern
Collection-Constraint Pattern
Collection-Indexing Pattern
21
Collection-Order Pattern
24
Collection-Indexing Pattern
Performance-Test Pattern
26
Performance-Test Pattern
Mock-Object Pattern
Service-Simulation Pattern
Bit-Error-Simulation Pattern
Component-Simulation Pattern
28
Mock-Object Pattern
30
Component-Simulation Pattern
31
Multithreading Patterns
Signaled Pattern
Deadlock-Resolution Pattern
32
Signaled Pattern
33
Deadlock-Resolution Pattern
34
Stress-Test Patterns
Bulk-Data-Stress-Test Pattern
Resource-Stress-Test Pattern
Loading-Test Pattern
35
Bulk-Data-Stress-Test Pattern
36
Resource-Stress-Test Pattern
37
Loading-Test Pattern
38
Presentation Layer Patterns
39
Process Patterns
Process-Sequence Pattern
Process-State Pattern
Process-Rule Pattern
40
Pattern Summary
41
Some Best Practices
42
Naming standards for unit tests
Example:
43
Test coverage and testing angles
Example:
44
When should a test be changed or removed?
Example:
Void Sum_Negative1stNumberCalculatesCorrectly()
{
Int sumResult = sum(-1,1,2);
Assert.AreEqual(2,sumResult);
}
Example:
DO:
– Express what should have happened and what did not happen
“Foo should have thrown an exception”
“Fodd didn not throw any exception”
Foo should have returned a new ID”
“Foo did not open the connection before returning it”
DON’T:
Provide empty or meaningless messages
Provide messages that repeat the name of the test case
Provide messages that simply state the test inputs
48
Avoid multiple asserts in a single unit test
49
Mock Objects Usage
For example
The unit test should test the class logic without having to configure or
rely on the availability of the logger class or email class
So we replace the logger class with a “fake” one which can also mimic
various scenarios of failures which are hard to recreate in real life
50
Making tests withstand design and interface
changes – remove code duplication
51
Unit Testing Frameworks
52
NUnit - Features
53
NUnit – Attribute Listing
– [TestFixture]
– [TestFixtureSetUp]
– [TestFixtureTearDown]
– [Test]
– [SetUp]
– [TearDown]
– [ExpectedException(typeof(Exception))]
– [Ignore(“message”)]
54
NUnit – Attributes
55
NUnit – Attributes
56
NUnit - Example
Class:
namespace bank
{
public class Account
{
private float balance;
public void Deposit(float amount)
{
balance+=amount;
}
public void Withdraw(float amount)
{
balance-=amount;
}
public void TransferFunds(Account destination, float amount)
{
}
public float Balance
{
get{ return balance;}
}
}
}
57
NUnit - Example
Test:
namespace bank
{
using NUnit.Framework;
[TestFixture]
public class AccountTest
{
[Test]
public void TransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);
}
}
}
58
NUnit – Screen Shots
59
NUnit – Screen Shots
60
NUnit – Screen Shots
61
NUnit – Screen Shots
62
Summary
http://www.extremeprogramming.org/
Advanced Unit Test, Part V - Unit Test
Patterns by Mark Clifton -
http://www.codeproject.com/gen/design/autp
5.asp#Introduction0
Best Practices -
http://weblogs.asp.net/rosherove/category/98
34.aspx?Show=All
www.junit.org
64