[php vigo][talk] unit testing sucks ( and it's your fault )

87
Unit Testing Sucks … and it is your our fault

Upload: php-vigo

Post on 16-Feb-2017

225 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: [Php vigo][talk] unit testing sucks ( and it's your fault )

Unit Testing Sucks… and it is your our fault

Page 2: [Php vigo][talk] unit testing sucks ( and it's your fault )
Page 3: [Php vigo][talk] unit testing sucks ( and it's your fault )
Page 4: [Php vigo][talk] unit testing sucks ( and it's your fault )

Testing

“Testing sucks!”- Everybody

Page 5: [Php vigo][talk] unit testing sucks ( and it's your fault )

Testing

Page 6: [Php vigo][talk] unit testing sucks ( and it's your fault )

Testing

Page 7: [Php vigo][talk] unit testing sucks ( and it's your fault )

Testing

Testing don’t suck: tests that suck, suck.

Page 8: [Php vigo][talk] unit testing sucks ( and it's your fault )

We care about clean code

Page 9: [Php vigo][talk] unit testing sucks ( and it's your fault )

... but we forgot something

Page 10: [Php vigo][talk] unit testing sucks ( and it's your fault )

Writing better tests

Tips and tricks for writing better tests

Page 11: [Php vigo][talk] unit testing sucks ( and it's your fault )

Subtle Differences

#1: Test codeis differentfrom othercode

Page 12: [Php vigo][talk] unit testing sucks ( and it's your fault )

4 rules of Simple Design

Passes its tests

Minimizes duplication

Maximizes clarity

Has no superfluous parts

Page 13: [Php vigo][talk] unit testing sucks ( and it's your fault )

Rules for tests?

Trustworthy

Express the intent

Flexible

Page 14: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#2: Choose better test names

Page 15: [Php vigo][talk] unit testing sucks ( and it's your fault )

What are we testing here?

Page 16: [Php vigo][talk] unit testing sucks ( and it's your fault )

Choose good names

I don’t want to read the whole test case to know what is trying to test.

Please.

Pretty please.

Problems

Page 17: [Php vigo][talk] unit testing sucks ( and it's your fault )

What are we testing here?

Page 18: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#3: Avoid logic inside tests

Page 19: [Php vigo][talk] unit testing sucks ( and it's your fault )

Avoid logic inside tests

Page 20: [Php vigo][talk] unit testing sucks ( and it's your fault )

Avoid logic inside tests

Conditionals: You don’t know which path the test will take

Loops: you could be sharing state between tests

If tests have logic, who test the tests?

Problems

Page 21: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#4: Highlight the important stuff

Page 22: [Php vigo][talk] unit testing sucks ( and it's your fault )

Be concise

Too much information in your tests make them hard to read

Think about what details could you leave out

Assert messages help us highlight behaviour

Problems

Page 23: [Php vigo][talk] unit testing sucks ( and it's your fault )

Assertions

Use the name of the test for explaining the feature that you are testing.

Page 24: [Php vigo][talk] unit testing sucks ( and it's your fault )

Assertions

Use the name of the test for explaining the feature that you are testing.

Then use assert messages to clarify what's the example under test and why is that tested.

Page 25: [Php vigo][talk] unit testing sucks ( and it's your fault )

Highlight the important

Page 26: [Php vigo][talk] unit testing sucks ( and it's your fault )

Highlight the important

Page 27: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#5: Builders to the rescue

Page 28: [Php vigo][talk] unit testing sucks ( and it's your fault )

Building same objects

Page 29: [Php vigo][talk] unit testing sucks ( and it's your fault )

Building same objects

We depend on the stability of the constructor / setter methods. If they change, we are screwed.

We are forced to pass all the parameters, even if they are not important for the test.

Problems

Page 30: [Php vigo][talk] unit testing sucks ( and it's your fault )

Building same objects

Page 31: [Php vigo][talk] unit testing sucks ( and it's your fault )

Building same objects

Page 32: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#6: Building your own DSL

Page 33: [Php vigo][talk] unit testing sucks ( and it's your fault )

No semantic

Page 34: [Php vigo][talk] unit testing sucks ( and it's your fault )

Single level of abstraction

Single Levelof Abstraction Principle

Page 35: [Php vigo][talk] unit testing sucks ( and it's your fault )

Single level of abstraction

Page 36: [Php vigo][talk] unit testing sucks ( and it's your fault )

Creating your DSL

Page 37: [Php vigo][talk] unit testing sucks ( and it's your fault )

Creating your DSL

Page 38: [Php vigo][talk] unit testing sucks ( and it's your fault )

Creating your DSL

Page 39: [Php vigo][talk] unit testing sucks ( and it's your fault )

Creating your DSL

Page 40: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#7: Don’t hide the important stuff

Page 41: [Php vigo][talk] unit testing sucks ( and it's your fault )

Rules for tests

Trustworthy

Express the intent

Flexible

Page 42: [Php vigo][talk] unit testing sucks ( and it's your fault )

Rules for tests

Trustworthy

Express the intent

Flexible

Page 43: [Php vigo][talk] unit testing sucks ( and it's your fault )

Duplication

Page 44: [Php vigo][talk] unit testing sucks ( and it's your fault )

Expressing the intent

"The refactoring technique ‘Extract Method’ is not about saving lines of code to make it shorter, but about make the code more readable"

Page 45: [Php vigo][talk] unit testing sucks ( and it's your fault )

Typing isn’t the bottleneck

"Programming is not about typing... it’s about thinking"

- Rich Hickey

Page 46: [Php vigo][talk] unit testing sucks ( and it's your fault )

Usual suspects

Set Up

Data Providers

Page 47: [Php vigo][talk] unit testing sucks ( and it's your fault )

Techniques

Page 48: [Php vigo][talk] unit testing sucks ( and it's your fault )

Usual suspects

Set Up

Data Providers

Page 49: [Php vigo][talk] unit testing sucks ( and it's your fault )

Problems with Set Up

Move important code out of sight

Now you have to remember that the code was there

Since code in setUp is generic for all test cases, you lose context on why is that code there

Problems

Page 50: [Php vigo][talk] unit testing sucks ( and it's your fault )

Data Providers

Page 51: [Php vigo][talk] unit testing sucks ( and it's your fault )

Explicit Set Up ™

Page 52: [Php vigo][talk] unit testing sucks ( and it's your fault )

Usual suspects

Set Up

Data Providers

Page 53: [Php vigo][talk] unit testing sucks ( and it's your fault )

Problems with Providers

Tend to make tests more complex

The most meaningful parts of the test (input & output) are now outside the test

Usually end up more complex than you expected

Problems

Page 54: [Php vigo][talk] unit testing sucks ( and it's your fault )

Data Providers

Page 55: [Php vigo][talk] unit testing sucks ( and it's your fault )

Solutions

Use Explicit Set Up ™ for the important stuff

Don’t mix different test cases in one provider

Avoid logic inside providers

Page 56: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#8: Mocking, the right way

Page 57: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Page 58: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Page 59: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Test doubles are objects that we create to help us test our system

Page 60: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Dummy

Fake

Stub

Spy

Mock

Page 61: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Dummy

Fake

Stub

Spy

Mock

Page 62: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Dummy

Page 63: [Php vigo][talk] unit testing sucks ( and it's your fault )

Dummy

Page 64: [Php vigo][talk] unit testing sucks ( and it's your fault )

Dummy

Use the real object unless its instantiation has side effects or is expensive somehow

Page 65: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Stub

Page 66: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Mock

Page 67: [Php vigo][talk] unit testing sucks ( and it's your fault )

Components

Value

objectEntity Service

Page 68: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Avoid test doubles for Value Objects

Page 69: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Avoid test doubles for Value Objects

Think twice before using test doubles for Entities

Page 70: [Php vigo][talk] unit testing sucks ( and it's your fault )

Command Query Separation

Command

Query

Page 71: [Php vigo][talk] unit testing sucks ( and it's your fault )

Command

Commands change the state of a system but do not return a value

Page 72: [Php vigo][talk] unit testing sucks ( and it's your fault )

Query

Queries return a value and don’t change the observable state of the system

Free of side effects

Page 73: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Use stubs to force a query to return a specific value so you guide the execution path of the test

Page 74: [Php vigo][talk] unit testing sucks ( and it's your fault )

Test Doubles

Use stubs to force a query to return a specific value so you guide the execution path of the test

Use mocks to assert that a command to another object has been sent

Page 75: [Php vigo][talk] unit testing sucks ( and it's your fault )

Without Test Doubles

isthis stillunit testing?

Page 76: [Php vigo][talk] unit testing sucks ( and it's your fault )

Without Test Doubles

What’s aunit?

Page 77: [Php vigo][talk] unit testing sucks ( and it's your fault )

Unit Test

Unit test

Page 78: [Php vigo][talk] unit testing sucks ( and it's your fault )

Isolated Test

Isolated test

Page 79: [Php vigo][talk] unit testing sucks ( and it's your fault )

Decoupling

Don’t modify your tests when refactoring.

It would couple the test to the current implementation

Page 80: [Php vigo][talk] unit testing sucks ( and it's your fault )

Tips and Tricks

#9: How much testing is enough?

Page 81: [Php vigo][talk] unit testing sucks ( and it's your fault )

How much to test

"Write tests until fear is transformed into boredom"

Page 82: [Php vigo][talk] unit testing sucks ( and it's your fault )

How much to test

Confidence

Communication

Success!

Page 83: [Php vigo][talk] unit testing sucks ( and it's your fault )

How much to test

Confidence & communication are the goals

High Code coverage is not a goal: is a side effect

Page 84: [Php vigo][talk] unit testing sucks ( and it's your fault )

#10: Think. Don’t blindly follow rules (like these)

Tips and Tricks

Page 85: [Php vigo][talk] unit testing sucks ( and it's your fault )

Everything is a trade-off

Page 86: [Php vigo][talk] unit testing sucks ( and it's your fault )

Conclusions

Testing makes you go faster.

Throw some love to your tests.

If you find yourself going slower, identify why.

TDD makes all of this much easier.

Page 87: [Php vigo][talk] unit testing sucks ( and it's your fault )

Jose Armesto @fiunchinho