Download ppt - Unit testing

Transcript
Page 1: Unit testing

Unit TestingUnit Testing

Save yourself and your project

Jeslie Chermak ([email protected])

Save yourself and your project

Jeslie Chermak ([email protected])

Page 2: Unit testing

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.

Page 3: Unit testing

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.

Page 4: Unit testing

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

Page 5: Unit testing

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

Page 6: Unit testing

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

Page 7: Unit testing

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.

Page 8: Unit testing

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

Page 9: Unit testing

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.

Page 10: Unit testing

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; }}

Page 11: Unit testing

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.

Page 12: Unit testing

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”

Page 13: Unit 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