Top Banner
What we talk about When we talk about Testing Noel Rappin, Table XI
51

What we talk about when we talk about testing, or beyond red, green, refactor

Jul 19, 2015

Download

Technology

noelrap
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: What we talk about when we talk about testing, or beyond red, green, refactor

What we talk aboutWhen we talk about

TestingNoel Rappin, Table XI

Page 2: What we talk about when we talk about testing, or beyond red, green, refactor

Or... Beyond

RedGreen

Refactor

Page 3: What we talk about when we talk about testing, or beyond red, green, refactor

Do you

test?

Page 4: What we talk about when we talk about testing, or beyond red, green, refactor

Do you feel

guiltyabout not testing

more

Page 5: What we talk about when we talk about testing, or beyond red, green, refactor

Why?

Page 6: What we talk about when we talk about testing, or beyond red, green, refactor

No

truetester

Page 7: What we talk about when we talk about testing, or beyond red, green, refactor

TDDis

hard

Page 8: What we talk about when we talk about testing, or beyond red, green, refactor

There arespecific skills

Page 9: What we talk about when we talk about testing, or beyond red, green, refactor

How Does TDD Work?

Page 10: What we talk about when we talk about testing, or beyond red, green, refactor

RedGreen

Refactor

Page 11: What we talk about when we talk about testing, or beyond red, green, refactor

But...Red, green, refactor is a better

slogan than an instruction manual

Page 12: What we talk about when we talk about testing, or beyond red, green, refactor

What do I test next?How do I split my

problem into testable components?

Page 13: What we talk about when we talk about testing, or beyond red, green, refactor

If you TDD in blocks that are too big TDD is

a real pain

Page 14: What we talk about when we talk about testing, or beyond red, green, refactor

Sample problem

Page 15: What we talk about when we talk about testing, or beyond red, green, refactor

Find all the unique sequences of digits 1 to 9, such as [1, 1, 2, 3, 8]

• The sum is 15

• At least one digit appears exactly twice

• No digit appears more than twice

• Order is irrelevant

• There are 38 such sequences

Page 16: What we talk about when we talk about testing, or beyond red, green, refactor

How do you test this?

Page 17: What we talk about when we talk about testing, or beyond red, green, refactor

Probably a bad idea:it "solves the problem" do expect(sequence.solutions.length).to eq(38)end

Page 18: What we talk about when we talk about testing, or beyond red, green, refactor

This is a little betterit "knows a valid sequence" do expect(Sequence.new(1, 1, 2, 3, 8)).to be_validend

Page 19: What we talk about when we talk about testing, or beyond red, green, refactor

And then we would make that test pass...

Page 20: What we talk about when we talk about testing, or beyond red, green, refactor

Wait,How would we make

that test pass?

Page 21: What we talk about when we talk about testing, or beyond red, green, refactor

class Sequence def init(*items) @items = items end

def valid? true endend

Page 22: What we talk about when we talk about testing, or beyond red, green, refactor

This is

OK

Page 23: What we talk about when we talk about testing, or beyond red, green, refactor

Because it is

notpermanent

Page 24: What we talk about when we talk about testing, or beyond red, green, refactor

Okay, now what

Page 25: What we talk about when we talk about testing, or beyond red, green, refactor

One option: break the testit "knows a valid sequence" do expect(Sequence.new(1, 1, 2, 3, 9)).not_to be_validend

Page 26: What we talk about when we talk about testing, or beyond red, green, refactor

Normally a good option, but

problematic here

Page 27: What we talk about when we talk about testing, or beyond red, green, refactor

Option 2, go smallerit "can tell if the sum is 15" do expect(Sequence.new(7, 8).to be_correct_sum)end

Page 28: What we talk about when we talk about testing, or beyond red, green, refactor

Why is that better?

Page 29: What we talk about when we talk about testing, or beyond red, green, refactor

Because it's small enough that I can write it without

thinking too hard

Page 30: What we talk about when we talk about testing, or beyond red, green, refactor

Also, I can negate it easilyit "can tell if the sum is not 15" do expect(Sequence.new(7, 8, 1).not_to be_correct_sum)end

Page 31: What we talk about when we talk about testing, or beyond red, green, refactor

TDD should reduce cognitive load

Page 32: What we talk about when we talk about testing, or beyond red, green, refactor

So from here, I can do• has a digit pair

• does not have a digit trio

• is unique

Page 33: What we talk about when we talk about testing, or beyond red, green, refactor

Start with anend-to-end test

Page 34: What we talk about when we talk about testing, or beyond red, green, refactor

Find an piece that can be encapsulated

Page 35: What we talk about when we talk about testing, or beyond red, green, refactor

Test a happy-path solution

Page 36: What we talk about when we talk about testing, or beyond red, green, refactor

Try to break it

Page 37: What we talk about when we talk about testing, or beyond red, green, refactor

Back to the problem

Page 38: What we talk about when we talk about testing, or beyond red, green, refactor

There's one more piece

Page 39: What we talk about when we talk about testing, or beyond red, green, refactor

I need to loop over some universe of

possible sequences.

Page 40: What we talk about when we talk about testing, or beyond red, green, refactor

This is hard to TDD

Page 41: What we talk about when we talk about testing, or beyond red, green, refactor

What is the code supposed to accomplish?

What testable assertions can I make?

Page 42: What we talk about when we talk about testing, or beyond red, green, refactor

We could test all 9^15 combinations, I

guess(205 trillion)

Page 43: What we talk about when we talk about testing, or beyond red, green, refactor

But, for example[1, 1, 1]

eliminates all longer sequences

Page 44: What we talk about when we talk about testing, or beyond red, green, refactor

Testable assertion: what sequence

should we test next

Page 45: What we talk about when we talk about testing, or beyond red, green, refactor

• () => (1)• (1) => (1, 1)• (1, 1) => (1, 1, 1)• (1, 1, 1) => (1, 1, 2)• (1, 1, 2, 2, 3, 3, 4) => (1, 1, 2, 2, 3, 4)• (1, 9, 5) => (2)• (9, 6) => nil

Page 46: What we talk about when we talk about testing, or beyond red, green, refactor

If you do it that way, it only checks 9825

sequencs

Page 47: What we talk about when we talk about testing, or beyond red, green, refactor

Where did design come in?

Page 48: What we talk about when we talk about testing, or beyond red, green, refactor

I design my functionality as I

write the end-to-end test

Page 49: What we talk about when we talk about testing, or beyond red, green, refactor

I design my internal API as I write the happy-path test

Page 50: What we talk about when we talk about testing, or beyond red, green, refactor

I refactor to keep my design clean

Page 51: What we talk about when we talk about testing, or beyond red, green, refactor

Noel RappinTable XI@noelrap

http://www.noelrappin.com/trddhttp://pragprog.com/book/nrtest2/rails-4-test-

prescriptions