Unit testing

Preview:

DESCRIPTION

Everyone says you should test your designs, but expects you to know what that testing should be. Here we briefly look at the kinds of tests (software) engineers might encounter and what the terms being used actually mean. Finally we settle on Unit Testing as a good place to begin the testing process.

Citation preview

Unit TestingUnit Testing

Save yourself and your project

Jeslie Chermak (jeslie@earthlink.net)

Save yourself and your project

Jeslie Chermak (jeslie@earthlink.net)

What is “testing”?What is “testing”?(We’re going to focus on software)• Testing is the process of validating that

software meets its design goals, behaves as expected, and can be duplicated: http://en.wikipedia.org/wiki/Software_testing

• Note: Most of what we’ll discuss applies equally to any other type of testing as well.

(We’re going to focus on software)• Testing is the process of validating that

software meets its design goals, behaves as expected, and can be duplicated: http://en.wikipedia.org/wiki/Software_testing

• Note: Most of what we’ll discuss applies equally to any other type of testing as well.

Testing “colors”Testing “colors”There really are only three:http://en.wikipedia.org/wiki/Software_testing - The_box_approach

• Black box –based on no knowledge of the actual inner workings (e.g., using just the spec. or user’s guide).

• White box – based on detailed knowledge of the actual implementation.

• Gray box – everything in between black and white.

There really are only three:http://en.wikipedia.org/wiki/Software_testing - The_box_approach

• Black box –based on no knowledge of the actual inner workings (e.g., using just the spec. or user’s guide).

• White box – based on detailed knowledge of the actual implementation.

• Gray box – everything in between black and white.

Testing “levels”Testing “levels”

Again, basically three levels:http://en.wikipedia.org/wiki/Software_testing - Testing_levels

•System test – entire “product” behaves•Integration test – one piece interacts properly with another piece•Unit test – one piece behaves correctly in isolation

Again, basically three levels:http://en.wikipedia.org/wiki/Software_testing - Testing_levels

•System test – entire “product” behaves•Integration test – one piece interacts properly with another piece•Unit test – one piece behaves correctly in isolation

Testing ObjectivesTesting ObjectivesAgain, basically three levels:• Acceptance tests – confirm that things

work acceptably• Regression tests – confirm that prior

problems remain resolved• Functional tests – confirm that things

behave as expected

Again, basically three levels:• Acceptance tests – confirm that things

work acceptably• Regression tests – confirm that prior

problems remain resolved• Functional tests – confirm that things

behave as expected

Other Testing GoalsOther Testing Goals• Security tests• Performance tests• Stability tests• Load tests tests• Usability tests• I18N/L10N tests• Destructive tests

• Security tests• Performance tests• Stability tests• Load tests tests• Usability tests• I18N/L10N tests• Destructive tests

Where Should I Start?Where Should I Start?“Begin at the beginning, …”

King, “Alice in Wonderland”

• Are you building something new?• Are you extending something old?“Where ever you go, there you are”

Buckaroo Bonzai, “Buckaroo Bonzai”

• In the beginning, you test basics: you build white box, functional, unit tests.

“Begin at the beginning, …”King, “Alice in Wonderland”

• Are you building something new?• Are you extending something old?“Where ever you go, there you are”

Buckaroo Bonzai, “Buckaroo Bonzai”

• In the beginning, you test basics: you build white box, functional, unit tests.

Basic PrinciplesBasic Principles

“Testing can only show the presence of bugs, not their absence.”

E. W. Dijkstra

•Agile methodologies integrate testing as requisite part of the methodology•KISS

“Testing can only show the presence of bugs, not their absence.”

E. W. Dijkstra

•Agile methodologies integrate testing as requisite part of the methodology•KISS

Some ObservationsSome Observations

You can not test what you …• don’t have a definition for;• can not control;• can not measure;• can not access.

You can not test what you …• don’t have a definition for;• can not control;• can not measure;• can not access.

Is this correct?Is this correct?package com.jcc.training.generics;

import java.util.ArrayList;import java.util.List;

public class Stack implements java.io.Serializable { // marker interface private final List stack = new ArrayList(); public void push(final Integer value) { this.stack.add(value); // NULL

allowed! } private Integer top() { if (this.stack.isEmpty()) throw new IllegalStateException(); return (Integer) this.stack.get(this.stack.size() - 1); } public Integer pop() { final Integer value = this.top(); this.stack.remove(this.stack.size() + 1); return value; }}

package com.jcc.training.generics;

import java.util.ArrayList;import java.util.List;

public class Stack implements java.io.Serializable { // marker interface private final List stack = new ArrayList(); public void push(final Integer value) { this.stack.add(value); // NULL

allowed! } private Integer top() { if (this.stack.isEmpty()) throw new IllegalStateException(); return (Integer) this.stack.get(this.stack.size() - 1); } public Integer pop() { final Integer value = this.top(); this.stack.remove(this.stack.size() + 1); return value; }}

More ObservationsMore Observations

• Use a testing framework.• Make it a part of your IDE/process.• Test often, especially after a

“trivial” change.• Make tests for old code before

you change it.

• Use a testing framework.• Make it a part of your IDE/process.• Test often, especially after a

“trivial” change.• Make tests for old code before

you change it.

Going Further …Going Further …

• Wikipedia book: http://en.wikipedia.org/wiki/Book:Software_testing

• Real world consequences: http://en.wikipedia.org/wiki/List_of_software_bugs

• xUnit: http://en.wikipedia.org/wiki/XUnit

• Google “software testing”

• Wikipedia book: http://en.wikipedia.org/wiki/Book:Software_testing

• Real world consequences: http://en.wikipedia.org/wiki/List_of_software_bugs

• xUnit: http://en.wikipedia.org/wiki/XUnit

• Google “software testing”

Closing thought …Closing thought …

“Beware of bugs in the above code; I have

only proved it correct, not tried it.”

Donald Knuth

“Beware of bugs in the above code; I have

only proved it correct, not tried it.”

Donald Knuth