xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

89

Transcript of xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Page 1: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.
Page 2: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

xUnit Test Patterns

Page 3: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

(Some)xUnit Test Patterns

(in practice)by Adam Czepil

Page 4: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Who needs these?

What kind of problems xUnit test patterns may help to solve? Who will benefit the most?

Page 5: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Goals of Test Automation

Code Smells

Principles of Test Automation

Behavior Smells

Projects Smells

Test Patterns

Page 6: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Is it a fish?

Page 7: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Or is it a mammal?

Page 8: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Revenons a nos moutons!

Page 9: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What is Defect Localization and why developers in good projects (almost) don’t use debugger?

Debugging is an art

Page 10: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

The problem

Page 11: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

The problem

We run the test (possibly manual test)

Page 12: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

The problem

We run the test (possibly manual test)The test fails

Page 13: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

The problem

We run the test (possibly manual test)The test failsWe have to figure out why it fails?

Page 14: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

The problem

We run the test (possibly manual test)The test failsWe have to figure out why it fails?

The problem is that this can be very time consuming...

Page 15: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Fortunatelly we have extra-skilled developers ...

... who can find the problem very quickly ;-)

Page 16: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What we expect from very good tests is that

Page 17: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What we expect from very good tests is thatif the tests fail they provide information

Page 18: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What we expect from very good tests is thatif the tests fail they provide information detailed enoughso we can find the source of the failure quickly.

Page 19: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Defect Localization

ability to very quickly find the actual cause of test failure

Page 20: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Defect Localization is

Page 21: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Defect Localization is

one of the major goals of test automation!

Page 22: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Defect Localization is

Page 23: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Defect Localization is

one of the major advantages of unit tests over customer (functional) tests!

Page 24: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What to do to have good Defect Localization?

Page 25: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Frequent Debugging

Page 26: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

When we deal with Frequent Debugging?

Page 27: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

When we deal with Frequent Debugging?

When manual debugging is required to determine the cause of most test failures.

Page 28: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

When we deal with Frequent Debugging?

When manual debugging is required to determine the cause of most test failures.

This is also true when instead of debugging we use extensive logging during testing.

Page 29: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Frequent Debugging...

Page 30: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Frequent Debugging...

reduces productivity!

Page 31: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Frequent Debugging...

reduces productivity! andmakes development schedules much less predictable!

Page 32: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Frequent Debugging...

reduces productivity! andmakes development schedules much less predictable!

because a single manual debugging session could extend the time required to develop the software by half a day or more.

Page 33: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the causes of Frequent Debugging?

Page 34: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the causes of Frequent Debugging?

Frequent Debugging is caused by

Page 35: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the causes of Frequent Debugging?

Frequent Debugging is caused by missing tests or

Page 36: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the causes of Frequent Debugging?

Frequent Debugging is caused by missing tests or lack of Defect Localization in our suite of automated tests.

Page 37: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

We should be able to quickly determine the root cause of test failure based on

Page 38: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

We should be able to quickly determine the root cause of test failure based on Assertion Message

Page 39: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

We should be able to quickly determine the root cause of test failure based on Assertion Message or through the pattern of test failures

Page 40: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

We should be able to quickly determine the root cause of test failure based on Assertion Message or through the pattern of test failures andknowledge about changes being made.

Page 41: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the main causes for no Defect Localization?

Page 42: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the main causes for no Defect Localization?

• not enough detailed unit tests compared to functional tests,

Page 43: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the main causes for no Defect Localization?

• not enough detailed unit tests compared to functional tests,• missing component tests (for a cluster of classes) that would

point out an integration error between classes.

Page 44: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What are the main causes for no Defect Localization?

• not enough detailed unit tests compared to functional tests,• missing component tests (for a cluster of classes) that would

point out an integration error between classes, • Infrequently Run Tests, because it causes that it is difficult

to point out the change that caused the tests to fail.

Page 45: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Very good solution for Frequent Debugging problem is...

Page 46: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Very good solution for Frequent Debugging problem is...

TDD – Test Driven Development

Page 47: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Very good solution for Frequent Debugging problem is...

TDD – Test Driven Development (or at least Test First)

Page 48: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What I want you to remember?

Page 49: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What I want you to remember?

Defect Localization is about finding quickly bug causing the tests to fail.

Page 50: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What I want you to remember?

Defect Localization is about finding quickly bug causing the tests to fail.

Unit tests are usually much better in Defect Localization than customer tests.

Page 51: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What I want you to remember?

Defect Localization is about finding quickly bug causing the tests to fail.

Unit tests are usually much better in Defect Localization than customer tests.

Frequent Debugging usually means that project is either missing tests or has poor Defect Localization.

Page 52: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

What I want you to remember?

Defect Localization is about finding quickly bug causing the tests to fail.

Unit tests are usually much better in Defect Localization than customer tests.

Frequent Debugging usually means that project is either missing tests or has poor Defect Localization.

Use TDD as often as possible

Page 53: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

WARNING! Too much debugging may broke your eyes! ;-)

Page 54: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Questions?

Page 55: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

How NOT to automate manual tests?What are some differences between

manual and automated test-case? How to create automated test

comparing to manual test?

Page 56: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Manual vs. Automated Tests

Page 57: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Manual vs. Automated Tests

Let’s compare some aspects...

Page 58: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Manual vs. Automated Tests

Let’s compare some aspects...-time

Page 59: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Manual vs. Automated Tests

Let’s compare some aspects...-time,-fragility.

Page 60: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Time

Executing manual tests requires A LOT OF time from HUMAN

Page 61: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Time

Executing automated tests requires almost NO time from HUMAN

Page 62: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Since HUMAN time is precious and COMPUTER time is cheap we should care much about the first anddon’t have to worry too much about the latter.

Page 63: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Conclusions

Opposite to manual tests we can create unlimited number of regression test cases.

Page 64: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Fragility

When the test fails/stops in some non-final point HUMAN usually finds a way (work around) to continue its execution.

Page 65: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Fragility

When the test fails in some non-final point test execution STOPS.

Page 66: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

HUMAN, although much slower than COMPUTER, is smarter test executor, especially when the test fails before all test conditions have been checked.

Page 67: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Conclusions

Opposite to manual tests we have to care about assertions order andfind a way of handling not executed assertions.

Page 68: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Verify One Condition per Test

Page 69: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Verify One Condition per Test principle is not the same asOne Assertion Per Test principle

Page 70: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Let’s see an example...

example-refactored.zipExample.zip

example example refactored

Page 71: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

When (not) to use setup (@Before) and teardown (@After) in xUnit?

Page 72: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Verify One Condition per Test helps to give us good Defect Localization to pinpoint the source of the failure.

Page 73: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Another reason we Verify One Condition per Test is to Minimize Test Overlap

Page 74: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Another reason we Verify One Condition per Test is to Minimize Test Overlap, so that we have fewer tests to modify if we later modify the behavior of the SUT.

Page 75: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

So? How NOT to automate manual tests?

Page 76: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

So? How NOT to automate manual tests?

Don’t write long, complex test scenarios

Page 77: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

So? How NOT to automate manual tests?

Don’t write long, complex test scenarios,instead Verify One Condition per Test

Page 78: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

So? How NOT to automate manual tests?

Don’t write long, complex test scenarios,instead Verify One Condition per Test,but not necesserily one assertion per test.

Page 79: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Questions?

Page 80: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Hey! Test code is code too!

Is test code different than “regular” code? How to organize test cases into classes/files/directories? What about test utils/helpers? Where to keep them?

Page 81: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Questions?

Page 82: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

There is a lot of „patterns” described in the book. Here are „maps” of them...

Page 83: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

The Maps

Page 84: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.
Page 85: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.
Page 86: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.
Page 87: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Why it’s worthwile to study xUnit patterns?

Page 88: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Questions?

Page 89: xUnit Test Patterns (Some) xUnit Test Patterns (in practice) by Adam Czepil.

Thank you!Go there: http://xunitpatterns.com/Really! You don’t have to read everything there...