26
Dennis Byrne Introduction to Unit Test g Part 1 of 2

Introduction to Unit Testing (Part 1 of 2)

Embed Size (px)

DESCRIPTION

A presentation on introductory unit testing.

Citation preview

Page 1: Introduction to Unit Testing (Part 1 of 2)

Dennis Byrne

Introduction to Unit Test gPart 1 of 2

Page 2: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties

● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions

Page 3: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Encapsulation

Page 4: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Encapsulation

● A unit test is just another class● Do test external behavior

○ Return values○ Interactions with collaborators

● Do not test internal state○ Private methods○ Private variables○ Reflection

Page 5: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties

● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions

Page 6: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic

● Sources of Non-determinism○ Math.random○ java.util.Random○ System clock methods○ Multi-threaded code○ Networks

Page 7: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic

@Test

public void shouldGenerateColorUntestable(){

ColorGenerator bad = new UntestableRandomColorGenerator();

Color nonDeterministic = bad.generate();

// ...

}

Page 8: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic class UntestableRandomColorGenerator implements ColorGenerator {

private final Random _random;

UntestableRandomColorGenerator() {

_random = new Random();

}

public Color generate(){

return Color.color(_random.nextDouble(),

_random.nextDouble(),

_random.nextDouble());

}

}

Page 9: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic

@Test

public void shouldGenerateColorTestable(){

// Dependency Injection

Random random = new Random(42);

ColorGenerator good = new TestableRandomColorGenerator(random);

Color deterministic = good.generate();

// ...

}

Page 10: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic class TestableRandomColorGenerator implements ColorGenerator {

private final Random _random;

TestableRandomColorGenerator() { // prod ctor

this(new Random());

}

TestableRandomColorGenerator(Random random) { // test ctor

_random = random;

}

public Color generate(){

return Color.color(_random.nextDouble(), _random.nextDouble(),

_random.nextDouble());

}

}

Page 11: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic class UntestableCalandar implements Calendar {

// ...

public Set<Event> getFutureEvents() {

long nowInMillis = System.currentTimeMillis();

Set<Event> events = new HashSet<Event>();

for (Event event : events){

if(event.getCreationTime() < nowInMillis){

events.add(event);

}

}

return events;

}

}

Page 12: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic @Test public void testFutureEvents(){

// Dependency Injection

Time time = new Time() {

public long currentTimeMillis() {

return 42; // between past & present

}

};

TestableCalendar calendar = new TestableCalendar(time);

// ...

}

Page 13: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic class TestableCalendar implements Calendar {

private final Time _time;

TestableCalendar(Time time) {

_time = time;

}

public Set<Event> getFutureEvents() {

long nowInMillis = _time.currentTimeMillis();

Set<Event> events = new HashSet<Event>();

// ...

return events;

}

}

Page 14: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Deterministic interface Time {

long currentTimeMillis();

}

class SystemTime implements Time {

public long currentTimeMillis() {

return System.currentTimeMillis();

}

}

Page 15: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties

● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions

Page 16: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Side Effects

MutableShared

Page 17: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Side Effects

● Each unit test is an isolated experiment● Unit test order should be arbitrary● Static singletons● TestNG vs most XUnit test class lifecycle● Unit Testing and Concurrency agree

Page 18: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties

● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions

Page 19: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Simplicity

● KISS … Keep It Simple Stupid● Test code should not reimplement

production● A unit test should only test one class● Test method structure

1 - Build inputs2 - Create the “system under test”3 - Perform assertions

Page 20: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties

● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions

Page 21: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Fast

● Avoid the Network○ Retrieving data from a another online system○ The LixClient interface enabled unit testing

● Go easy on the file system○ Loading large amounts of test data from disk

● Avoid large computations○ Generating combinatorial inputs …○ Time complexity○ Memory usage

Page 22: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions

Page 23: Introduction to Unit Testing (Part 1 of 2)

Unit Test Properties: Assertions @Test

public void ok(){

Calculator calculator = new Calculator();

Number number = calculator.add(new Integer(3), new Integer(7));

Assert.assertEquals(new Integer(10), number); // three + seven = ten

}

@Test

public void better(){

Calculator calculator = new Calculator();

Number number = calculator.add(new Integer(3), new Integer(7));

Assert.assertEquals(number, new Integer(10), "three + seven = ten");

}

Page 24: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions

Page 25: Introduction to Unit Testing (Part 1 of 2)

Favorable Unit Test Properties: Name Conventions

Page 26: Introduction to Unit Testing (Part 1 of 2)

Agenda: Favorable Unit Test Properties● Encapsulation● Deterministic● Free of side effects● Simple● Fast● Assertions● Naming Conventions