Unit testing
This presentation is the property of its rightful owner.
Sponsored Links
1 / 27

Unit testing PowerPoint PPT Presentation


  • 141 Views
  • Uploaded on
  • Presentation posted in: General

Unit testing. Lecture Outline. Introduction to Unit Testing Development principles Unit Test Frameworks Unit tests for legacy code Resources. Introduction to Unit Testing. Overview: Why write unit tests? Why not? What are bugs? What unit tests won't do for you.

Download Presentation

Unit testing

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Unit testing

Unit testing


Lecture outline

Lecture Outline

  • Introduction to Unit Testing

  • Development principles

  • Unit Test Frameworks

  • Unit tests for legacy code

  • Resources


Introduction to unit testing

Introduction to Unit Testing

  • Overview: Why write unit tests? Why not?

  • What are bugs?

  • What unit tests won't do for you


Introduction to unit testing1

Introduction to Unit Testing

Overview

Why write Unit Tests?

  • Clean, bug-free, documented code

  • Quality software: less complaints

  • Coding simplicity: less stress

    Why not?

    Not enough time!


Introduction to unit testing2

Introduction to Unit Testing

Overview

  • The less tests, the more bugs

  • The more bugs, the more complaints

  • The more complaints, the more stress

  • The more stress, the less tests we write


Introduction to unit testing3

Introduction to Unit Testing

Overview

Can we write

bug-free code?

?

?

?

?

?

?

?


Unit testing

Introduction to Unit Testing

Overview

What is a bug?

  • An unwanted feature (memory leak, feature creep etc.)

  • A "broken" feature

  • A misunderstood feature (by the client or the coder)

    "It's not a bug, it’s a feature!"


Introduction to unit testing4

Introduction to Unit Testing

Overview

What Unit Tests don’t do:

Performance issues

Integration errors

User interface issues


Introduction to unit testing5

Introduction to Unit Testing

Statistics

According to some very reliable sources, programmers tend to spend over 80% of their working time looking for causes of bugs in existing code, 15% fixing these bugs, and 5% designing and developing new code.

(source: N.Connault's astute observations, 2008).


Introduction to unit testing6

Introduction to Unit Testing

Collateral Damage

Code Library 1

Code Library 2

Dependencies

Identify Bug

Fix bug

Test library 1

Patch library 1

NEW HIDDEN

BUG !!!

Result:

Waste of time


Introduction to unit testing7

Introduction to Unit Testing

Solution

Code Library 1

Code Library 2

Dependencies

Identify Bug

Fix bug

Test alllibraries

Patch libraries

When all tests pass...

Result: Better code


Unit testing

Coding principles

  • Coding principles

  • that apply to unit testing

  • ...


If you don t know why your modification fixed a bug

If you don't know why

your modification fixed a bug,

Coding principles

Ignorance is the enemy

  • you're almost guaranteed

  • to be the creator of at least one new bug


Coding principles

Coding principles

Holistic development

Development and testing

are not separate tasks


Coding principles1

Coding principles

Automation

The goal of programming is to reduce the amount of repetitive work and the number of simple decisions humans need to make.

Testing code is repetitive, and it is usually simple. Tests should be automated.


Coding principles2

Coding principles

Isolation

Each unit test must test a single unit of code.

A unit of code is usually a simple function

or a simple method.

Example of an overloaded function:

solveWorldHunger(money)


Coding principles3

Coding principles

Independence

One unit test

must never affect another.

The order in which they are run

must be absolutely inconsequential


Coding principles4

Coding principles

Test-driven development

When you need new functionality,

first write a failing unit test,

then implement the new code.


Coding principles5

Coding principles

Reliability

Never introduce bugs

in your unit tests.

Write them with the same rigour

as with your regular code.


Coding principles6

Coding principles

Mock objects

  • Write mock objects for:

  • Objects whose behaviour is non-deterministic (news feed)‏

  • Objects that are complex/difficult to set up

  • Objects that are slow

  • Objects that have or are a User Interface

  • Objects whose behaviour is hard to trigger

  • Objects that haven't yet been implemented


Coding principles7

Coding principles

Golden Rule

NEVER, NEVER, NEVER

commit code

if any unit test fails

!!!


Coding principles8

Coding principles

Thoroughness

Test boundary conditions as much as is possible and reasonable, including heavy load conditions.


Coding principles9

Coding principles

Prioritise

Don't write tests for code

that will obviously never fail


Questions and answers

Questions and answers

?+!


Unit test frameworks

Unit test Frameworks

Advantages

  • Automate the generation of unit testing code

  • Simplify the entire process

  • Provide advanced testing features (mock objects, fancy reports, expectations etc…)


Unit tests in legacy code

Too big a job to “catch up” all at once

Create unit tests to verify bug reports

Use unit tests to fix bugs

Refactor code when needed

Commit bug fix when all unit tests pass

Unit Tests in legacy code


Resources

Andrew Hunt and David Thomas. Pragmatic Unit Testing in Java with JUnit. The Pragmatic Programmers, LLC, Raleigh, NC, and Dallas, TX, 2004.

Venkat Subramaniam and Andy Hunt. Practices of an Agile Developer working in the Real World, The Pragmatic Bookshelf, 2006.

http://blog.daveastels.com/2005/07/05/a-new-look-at-test-driven-development

http://www.ibm.com/developerworks/java/library/j-cq09187/index.html

http://video.google.com/videoplay?docid=8135690990081075324

Resources


  • Login