100% found this document useful (1 vote)
135 views64 pages

Unit-Testing: Presented by Benny Pasternak November 2005

The document discusses various patterns for unit testing software code. It begins with definitions of unit testing and motivations for unit testing like reducing bugs. It then covers principles of extreme unit testing like testing first before writing code. The bulk of the document describes different categories of unit test patterns like pass/fail patterns, data patterns, collection patterns, simulation patterns, and stress test patterns. It provides examples for each type of pattern to test specific aspects of code.

Uploaded by

Ashihs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
135 views64 pages

Unit-Testing: Presented by Benny Pasternak November 2005

The document discusses various patterns for unit testing software code. It begins with definitions of unit testing and motivations for unit testing like reducing bugs. It then covers principles of extreme unit testing like testing first before writing code. The bulk of the document describes different categories of unit test patterns like pass/fail patterns, data patterns, collection patterns, simulation patterns, and stress test patterns. It provides examples for each type of pattern to test specific aspects of code.

Uploaded by

Ashihs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 64

Unit-Testing

Presented by
Benny Pasternak
November 2005

“Program testing can be used to show the


presence of bugs, but never to show their
absence!” - Edsger Dijkstra, [1972]
Agenda

 Introduction
– Definition
– Why bother?
– eXtreme Unit Testing
 Unit Test Patterns
 Some Best practices
 Testing Frameworks
 Conclusion

2
Definitions

 testing of individual hardware or


software units or groups of related units
[IEEE 90]
 a method of testing the correctness of a
particular module of source code [Wiki]

3
Why even bother?

 Tests Reduce Bugs in New Features


 Tests Reduce Bugs in Existing
Features
 Tests Are Good Documentation
 Tests Reduce the Cost of Change
 Tests Improve Design
 Tests Allow Refactoring

4
Why even bother? (continued)

 Tests Constrain Features


 Tests Defend Against Other
Programmers
 Testing Is Fun
 Testing Forces You to Slow Down and
Think
 Testing Makes Development Faster
 Tests Reduce Fear
5
eXtreme Unit Testing - Principles

 Use a unit test framework


 All classes should be tested
 Create tests first - Code is added only
when a tests breaks
 Unit tests are released into the code
repository along with the code they test.
(code without one may not be released)
 Upon discovering a missing unit test it
6 must be created at that time
eXtreme Unit Testing - Benefits

 Enable collective code ownership


 Guard your functionality from being
accidently harmed
 Requiring all code to pass all tests
ensures all functionality always works.
 Enable refactoring
 Enable frequent integration
 Tests before code – solidifies
requirements
7
Unit Test Patterns – Why?

 All tools have their supporters and


protestors
 As any tool it might be shelved one
day as “yet another programming tool”
 To change this fate, it has to be
embraced by both community and tool
developers
 In order to do so, it must be formalized
into a real engineering discipline rather
than an ad hoc approach
8
Unit Testing Patterns

 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

First line of defense to guarantee good code

 Simple-Test Pattern
 Code-Path Pattern
 Parameter-Range Pattern

10
Simple Test-Pattern

 Pass/Fail results tell us that the code under test


will work/trap an error given the same input
(condition) as in the unit test
 No real confidence that the code will work correctly
or trap errors with other set of conditions

11
Code-Path Pattern

 Emphasizes on conditions that test the code


paths with in the unit rather than conditions
that test for pass/fail
 Results are compared to expected output of
given code path
 Caveat: How do you test code-paths if tests
are written first?
12
Parameter-Range Pattern

 Code-Path pattern with more than a single


parameter test

13
Data Driven Test Patterns

Patterns which enable testing units with a


broad range of input, output pairs

 Simple-Test-Data Pattern
 Data-Transformation-Test Pattern

14
Simple-Test-Data Pattern

 Reduces complexity of Parameter-Range unit by


separating test data from the test.
 Test data is generated and modified independent
of the test
 Results are supplied with the data set. Variances
in the result are not permitted
 Candidates for this pattern: Checksum
Calculations, mathematical algorithims, etc…
15
Data-Transformation-Test Pattern

 Works with data in which a qualitive measure of


the result must be performed.
 Typically applied to transformation algorithms such
as lossy compression

16
Data Transaction Patterns

Patterns embracing issues of data persistence


and communication

 Simple-Data-I/O Pattern
 Constraint Data Pattern
 The Rollback Pattern

17
Simple-Data-I/O Pattern

 Verifies the read/write functions of the service

18
Constraint Data Pattern

 Adds robustness to Simple-Data-I/O pattern by


testing more aspects pf the service and any rules
that the service may incorporate
 Unit test verifies the service implementation itself,
whether a DB schema, web service, etc…
19
Rollback Pattern

 Verifies rollback correctness


 Most transactional unit tests should incorporate
ability to rollback dataset to known state, in order
to undo test side effects

20
Collection Management Patterns

Used to verify that the code is using the correct


collection

 Collection-Order Pattern
 Enumeration Pattern
 Collection-Constraint Pattern
 Collection-Indexing Pattern

21
Collection-Order Pattern

 Verifies expected results when given an


unordered list
 The test validates that the result is as expected:
unordered, ordered or same sequence as input
 Provides implementer with information on how the
container manages the collections
22
Enumeration Pattern

 Verifies issues of enumeration or collection


traversal
 Important test when connections are non-linear.
i.e. collection tree nodes
 Edge conditions (past first or last item) are also
important to test
23
Collection-Constraint Pattern

 Verifies that the container handles constraint


violations: null values and duplicate keys
 Typically applies to key-value pair collections

24
Collection-Indexing Pattern

 Verifies and documents indexing methods that the


collection must support – by index and/or by key
 Verifies that update and delete transactions that
utiilize indexing are working properly and are
protected against missing indexes
25
Performance Patterns

Used to test non functional requirements as


performance and resource usage

 Performance-Test Pattern

26
Performance-Test Pattern

 Types of performance that can be measured:


Memory usage (physical, cache, virtual)
Resource (handle) utilization
Disk utilization (physical, cache)
Algorithm Performance (insertion, retrieval)
27
Simulation Patterns

Used to verify that the code is using the correct


collection

 Mock-Object Pattern
 Service-Simulation Pattern
 Bit-Error-Simulation Pattern
 Component-Simulation Pattern

28
Mock-Object Pattern

 Classes need to be abstracted, objects must be


constructed in factories, facades and bridges need to
be used to support abstraction
 Alternatively, AOP practices can be used to establish a
pointcut
29
Service-Simulation Pattern

 Test simulates connection and I/O method of a


service
 Useful when developing large applications in
which functional pieces are yet to be implemented

30
Component-Simulation Pattern

 Mock object simulates a component failure, such


as network cable, hub or other device
 After a suitable time, the mock object can do a
variety of things:
– Thrown an exception
– Returns incomplete of completely missing data
– Return a “timeout” error

31
Multithreading Patterns

In order to perform many threading tests


correctly, the unit tester must itself execute
tests as separate threads.

 Signaled Pattern
 Deadlock-Resolution Pattern

32
Signaled Pattern

 This test verifies that a worker thread eventually


signals the main thread or another thread

33
Deadlock-Resolution Pattern

 Verifies that dead locks are resolved

34
Stress-Test Patterns

Verify unit’s performance under stress

 Bulk-Data-Stress-Test Pattern
 Resource-Stress-Test Pattern
 Loading-Test Pattern

35
Bulk-Data-Stress-Test Pattern

 Designed to validate performance of data


manipulation when working with large data sets
 Will reveal inefficencies in insertion, access
 Typically corrected by reviewing indexing, constrains,
reexamining if code should be client or server side

36
Resource-Stress-Test Pattern

 Depends on the features of the operating system


(may be served by using mock objects)
 If not supported by OS, mock objects must be used to
simulate the response of the operating system under
a low resource condition

37
Loading-Test Pattern

 Measures behavior of the code when another


machine, application, or thread is loading the
“system” (i.e. high CPU usage or network traffic)
 Ideally, a unit test simulating high volume of
network traffic would create a thread to inject
packets onto the network

38
Presentation Layer Patterns

1. Verify that information is getting to the user


right at the presentation layer itself
2. The internal workings of the application are
correctly setting presentation layer state.

 View-State Test Pattern


 Model-State Test Pattern

39
Process Patterns

A process is just a different type of unit.


Validate state transitions and business rules

 Process-Sequence Pattern
 Process-State Pattern
 Process-Rule Pattern

40
Pattern Summary

 Unit Test patterns cover broad aspects


of development; not just functional
 May promote unit testing to become a
more formal engineering discipline
 Helps identify the kind of unit tests to
write, and its usefulness.
 Allows developer to choose how
detailed the unit tests need to be

41
Some Best Practices

1. Naming standards for unit tests


2. Test coverage and testing angles
3. When should a unit test be removed or changed?
4. Tests should reflect required reality
5. What should assert messages say?
6. Avoid multiple asserts in a single unit test
7. Mock Objects Usage
8. Making tests withstand design and interface
changes – remove code duplication

42
Naming standards for unit tests

 Test name should express a specific requirement


 Test name should include the expected input or state and the
expected result output or state
 Test name should include name of tested method or class

Example:

Given method: Public int Sum(params int[] values)


with requirement to ignore numbers > 1000 in the summing
Process

Then test name should be Sum_NumberIgnoredIfBiggerThan1000

43
Test coverage and testing angles

Q: How can one check if a unit test has good coverage


over the tested code?
A: Try removing a line or a constraint check.

Example:

Public int Sum (int x,int y, bool allowNegatives)


{
(!true)
if (!allowNegatives)
{
if (x<0 || y<0) throw exception;
}
return x+y;
}

44
When should a test be changed or removed?

 Generally, a passing test should never be removed. They make


sure that code changes don’t break working code.
 A passing test should only be changed to make it more
readable.
 When failing tests don’t pass, it usually means there are
conflicting requirements:

Example:

[ExpectedException(typeof(Exception),Negatives not allowed)]


Void Sum_Negative1stNumberThrowsException()
{
Sum (-1,1,2);
}

New features allows negative numbers.


45
When should a test be changed or removed?

New developer writes the following test:

Void Sum_Negative1stNumberCalculatesCorrectly()
{
Int sumResult = sum(-1,1,2);
Assert.AreEqual(2,sumResult);
}

Earlier test fails due to a requirement change – it’s no longer valid

Two course of actions:


1. Delete the failing test after verifying that it’s not valid
2. Change the old test:
1. Either testing the new requirement
2. Or to test the older requirement under new settings
46
Tests should reflect required reality

Example:

Int Sum(int a,int b) – returns sum of a & b

What’s wrong with the following test?

Public void Sum_AddsOneAndTwo()


{
int result = Sum(1,2);
Assert.AreEqual(4, result, “bad sum”);
}

 Common mistake is to confuse the “Fail first” requirement with


“Fail by testing something illogical”
 A failing test should prove that there is something wrong with
the production code and not the unit test code
47
What should assert messages say?

 Assert message in a test is one of the most important things.


 Tells us what we expected to happen but didn’t, and what happened
instead
 Good assert message helps us track bugs and understand unit tests
more easily

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

Consider the following test:


Void Sum_AnyParamBiggerThan1000IsNotSummed()
{
Assert.AreEqual(3, Sum(1001,1,2);
Assert.AreEqual(3, Sum(1,1001,2);
Assert.AreEqual(3, Sum(1,2,1001);
}

Disadvantages of multiple asserts in one test case:


 If the first assert fails, the test execution stops for this test case.
 Favor failure of multiple tests over a failure of one test with multiple
asserts
 Affect future coders to add assertions to test rather then introducing a
new one

49
Mock Objects Usage

Q: When should mock objects be used?


A: Mock objects are used when one needs to replace or remove
dependencies from code under test

For example

class LoginManager manages user logins with the following responsibility:


When login fails, class reports to a logger class or email class

 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

 Re-design is a major pitfall developers writing unit tests might face.


 A sudden design change – removal of a default constructor, new
parameters added, etc… - might break many of the unit tests that have
already been written.
 All hell breaks loose and the developer starts fixing hundreds of tests.

Guidelines to prevent the scenario described above:

 Encapsulate object creation code


 Encapsulate complex or lengthy object initialization code
 Apply these helper methods only when duplicate code across the tests
is detected

51
Unit Testing Frameworks

 JUnit - First Unit Testing Framework


developed by Erich Gamma and Kent Beck
 Since then Unit Testing Frameworks have
been developed for a broad range of
computer languages.
 List of xUnit frameworks can be found at:
http://www.testingfaqs.org/t-unit.html
http://www.xprogramming.com/software.htm

52
NUnit - Features

 Test code is annotated using custom


Attributes
 Test code contains Assertions
 Supports Configuration Files
 Tests organized as Multiple Assemblies

53
NUnit – Attribute Listing

– [TestFixture]
– [TestFixtureSetUp]
– [TestFixtureTearDown]
– [Test]
– [SetUp]
– [TearDown]
– [ExpectedException(typeof(Exception))]
– [Ignore(“message”)]

54
NUnit – Attributes

 [TestFixture] - This attribute marks a class that contains


tests, and, optionally, setup or teardown methods
 [Test] - The Test attribute marks a specific method inside a
class that has already been marked as a TestFixture, as a
test method
 [TestFixtureSetUp] - Used to indicate a setup method that
will be ran once; before all other tests. This is the first
method that is called before the tests are started
 [TestFixtureTearDown] - Used to indicate a tear down
method that will be ran once; after all other tests have run.
This is the last method that is called after all the tests have
finished.

55
NUnit – Attributes

 [SetUp] - Used to indicate a setup method should be ran


before each of the tests
 [TearDown] - Used to indicate a tear down method should
be ran after each of the tests are ran
 [ExpectedException(typeof(Exception))] –
– When you want an exception to be thrown
– Will only pass if exception type was throw
 [Ignore(“Not ready for primetime”)] - Used when a test is
not ready, or you don’t want the test to be ran

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

 The concept of Unit Testing has been around


for many years
 New methodologies in particular XP, have
turned unit testing into a cardinal foundation
of software development.
 Writing good & effective Unit Tests is hard!
 This is where supporting integrated tools and
suggested guidelines enter the picture.
 The ultimate goal is tools that generate unit
tests automatically
63
Refrences

 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

You might also like