test-oriented languages: is it time for a new era?

48
Test-Oriented Languages: is it time for a new era? Ben Stopford : The Royal Bank of Scotland Friday, 25 March 2011

Upload: ben-stopford

Post on 27-May-2015

2.220 views

Category:

Documents


1 download

DESCRIPTION

Where is TDD going next? Mock Methods not Objects?There are many good reasons for using TDD in it’s ‘mockist’ form and you tend to build better software as as result. However there’s still a lot of overhead involved in creating mock and stub objects and passing them around. It makes the approach less accessible, more elitist and harder to integrate into a project if all the devs aren’t indoctrinated with the approach.The slides below throw around some ideas for how we might do things differently. At it’s core is the concept of mocking methods rather than objects along with a compilation process that forces isolation.

TRANSCRIPT

Page 1: Test-Oriented Languages: Is it time for a new era?

Test-Oriented Languages: is it time for a new era?Ben Stopford : The Royal Bank of Scotland

Friday, 25 March 2011

Page 2: Test-Oriented Languages: Is it time for a new era?

Mocking is fundamental part of Test Driven Development

Friday, 25 March 2011

Page 3: Test-Oriented Languages: Is it time for a new era?

But how much further can we go in current imperative languages?

Friday, 25 March 2011

Page 4: Test-Oriented Languages: Is it time for a new era?

What might we improve if we were not inhibited by current mainstream compilers?

Friday, 25 March 2011

Page 5: Test-Oriented Languages: Is it time for a new era?

What might we do with a blank slate?

Friday, 25 March 2011

Page 6: Test-Oriented Languages: Is it time for a new era?

Quilt

Friday, 25 March 2011

Page 7: Test-Oriented Languages: Is it time for a new era?

Stub methods not classes

Compiler significantly reduces the amount of test code that must be written to provide isolation

How is Quilt Different

Friday, 25 March 2011

Page 8: Test-Oriented Languages: Is it time for a new era?

Taking step back...

Friday, 25 March 2011

Page 9: Test-Oriented Languages: Is it time for a new era?

What are the motivations for mock driven development?

Friday, 25 March 2011

Page 10: Test-Oriented Languages: Is it time for a new era?

State based tests inevitably overlap

!"#$%&'($%

!"#$%&'($%

Friday, 25 March 2011

Page 11: Test-Oriented Languages: Is it time for a new era?

This makes it hard to diagnose the source of a break.

!"#$%&'($%

!"#$%&'($%

Friday, 25 March 2011

Page 12: Test-Oriented Languages: Is it time for a new era?

The solution is to use mock objects to provide substitute interactions that we can assert from our test !"#$%&'($%

!"#$%&'($%

Friday, 25 March 2011

Page 13: Test-Oriented Languages: Is it time for a new era?

Current Challenges

Friday, 25 March 2011

Page 14: Test-Oriented Languages: Is it time for a new era?

Coupling between test and source

Friday, 25 March 2011

Page 15: Test-Oriented Languages: Is it time for a new era?

“I've always been a old fashioned classic TDDer and thus far I don't see any reason to change. I don't see any compelling benefits for mockist TDD, and am concerned about the consequences of coupling tests to implementation.”

Friday, 25 March 2011

Page 16: Test-Oriented Languages: Is it time for a new era?

The JMock Era

Friday, 25 March 2011

Page 17: Test-Oriented Languages: Is it time for a new era?

Mock Interactions

Friday, 25 March 2011

Page 18: Test-Oriented Languages: Is it time for a new era?

All interactions must be explicitly coded in the test

!"#$%&'($%

Friday, 25 March 2011

Page 19: Test-Oriented Languages: Is it time for a new era?

The Mockito Era

Friday, 25 March 2011

Page 20: Test-Oriented Languages: Is it time for a new era?

Only mock interactions you care about

!"#$%&'($%

Friday, 25 March 2011

Page 21: Test-Oriented Languages: Is it time for a new era?

This reduces coupling between test and class

Friday, 25 March 2011

Page 22: Test-Oriented Languages: Is it time for a new era?

Quilt takes this concept further by only requiring methods to be stubbed not whole objects (but more on that later)

Friday, 25 March 2011

Page 23: Test-Oriented Languages: Is it time for a new era?

To do Mock-Driven development you need to test first

The Testing Barrier

Friday, 25 March 2011

Page 24: Test-Oriented Languages: Is it time for a new era?

Should it be like this?

Friday, 25 March 2011

Page 25: Test-Oriented Languages: Is it time for a new era?

In quilt it’s as easy to test after as it is to test first

!"#$%&'($%

Friday, 25 March 2011

Page 26: Test-Oriented Languages: Is it time for a new era?

What Makes Quilt Different?

Friday, 25 March 2011

Page 27: Test-Oriented Languages: Is it time for a new era?

The complier isolates testable units for you.

Where it needs your input it requires that you provide it.

Friday, 25 March 2011

Page 28: Test-Oriented Languages: Is it time for a new era?

So when you run a test the compiler will restrict the execution scope, say to a single class

!"#$%&'($%

Friday, 25 March 2011

Page 29: Test-Oriented Languages: Is it time for a new era?

If that class under test calls other classes the compiler will automatically isolate the interactions

!"#$%&'($%

Friday, 25 March 2011

Page 30: Test-Oriented Languages: Is it time for a new era?

If those interactions return state then the compiler requires that a active stub be declared in the test

!"#$%&'($%

Friday, 25 March 2011

Page 31: Test-Oriented Languages: Is it time for a new era?

So quilt creates seams around the testable unit isolating the functionality under test !"#$%&

Friday, 25 March 2011

Page 32: Test-Oriented Languages: Is it time for a new era?

Methods are Stubbed not Objects

!"#$%&'($%

Friday, 25 March 2011

Page 33: Test-Oriented Languages: Is it time for a new era?

Testing units can be one or many classes: a Patch

!"#$%&#'($)%&"*'%+,&-"%&"#%-./0*1#2%3*11%4.2-#%&"#%5#-1,2,(.$%.4%,%'&677#5%2#&62$%&80#%4.2%&"*'%/#&".5%

91,''#'%,2#%)2.60#5%*$&.%,%:+,&-";%

+,&-"#'%,2#%'#0,2,&#5%78%:<#,/';%

Friday, 25 March 2011

Page 34: Test-Oriented Languages: Is it time for a new era?

There is no need for dependency injection for the purpose of testing

Friday, 25 March 2011

Page 35: Test-Oriented Languages: Is it time for a new era?

The barrier for testing is lowered

Friday, 25 March 2011

Page 36: Test-Oriented Languages: Is it time for a new era?

Looking more closely

Friday, 25 March 2011

Page 37: Test-Oriented Languages: Is it time for a new era?

Compilation Ensures that Stubs are Required only if they Affect the Output of the Test.

Friday, 25 March 2011

Page 38: Test-Oriented Languages: Is it time for a new era?

class ConstructionSite{ Digger digger = new Digger(); Mixer mixer = new CementMixer(); Foreman foreman = new Foreman();

ConstructionSite(){}

ConstructionSite(Digger d, Mixer m, Foreman f){ digger = d; mixer = m; foreman = f; } boolean buildFoundation(Bricks bricks){ Cement cement = mixer.mix(); Foundation foundation = digger.dig(); BrickLayer layer = foreman.getLayer(); if(!cement.isSolid() && bricks.size()> 100){ Posts posts = layer.lay(bricks, cement); foundation.fill(posts); return true; } return false; }}

!"#$%&'(($

Friday, 25 March 2011

Page 39: Test-Oriented Languages: Is it time for a new era?

@TestshouldBuildFoundationsWithLotsOfBricksAndSlowDryingCement(){ Digger digger = mock(Digger.class); CementMixer mixer = mock(CementMixer.class); Foreman foreman = mock(Foreman.class); Cement cement = mock(Cement.class); BrickLayer layer = mock(BrickLayer.class); Foundation foundation = mock(Foundation.class);

when(mixer.mix()).thenReturn(cement); when(digger.dig()).thenReturn(foundation); when(cement.isSolid ()).thenReturn(Boolean.FALSE); when(foreman.getLayer()).thenReturn(layer); ConstructionSite site = new ConstructionSite(digger, mixer, foreman); assertTrue(site.buildFoundation(new Bricks(101)))}

!"#$!#%&$'()*)+$

Friday, 25 March 2011

Page 40: Test-Oriented Languages: Is it time for a new era?

In all this mock/stub setup only two parts are actually pertinent to the test:

@TestshouldBuildFoundationsWithLotsOfBricksAndSlowDryingCement(){ Digger digger = mock(Digger.class); CementMixer mixer = mock(CementMixer.class); Foreman foreman = mock(Foreman.class); Cement cement = mock(Cement.class); BrickLayer layer = mock(BrickLayer.class); Foundation foundation = mock(Foundation.class);

when(mixer.mix()).thenReturn(cement); when(digger.dig()).thenReturn(foundation); when(cement.isSolid ()).thenReturn(Boolean.FALSE); when(foreman.getLayer()).thenReturn(layer); ConstructionSite site = new ConstructionSite(digger, mixer, foreman); assertTrue(site.buildFoundation(new Bricks(101)))}

Friday, 25 March 2011

Page 41: Test-Oriented Languages: Is it time for a new era?

shouldBuildFoundationsWithLotsOfBricksAndSlowDryingCement(){

Seam: cement.isSolid() returns false; bricks.size returns 100;

AssertTrue: new ConstructionSite().buildFoundation(..);}}

!"#$!#%&$'()*+&,$

Friday, 25 March 2011

Page 42: Test-Oriented Languages: Is it time for a new era?

No need to set up stub objects

shouldBuildFoundationsWithLotsOfBricksAndSlowDryingCement(){

Seam: cement.isSolid() returns false; bricks.size returns 100;

AssertTrue: new ConstructionSite().buildFoundation(..);}}

Friday, 25 March 2011

Page 43: Test-Oriented Languages: Is it time for a new era?

Stub Methods not Objects

shouldBuildFoundationsWithLotsOfBricksAndSlowDryingCement(){

Seam: cement.isSolid() returns false; bricks.size returns 100;

AssertTrue: new ConstructionSite().buildFoundation(..);}}

Friday, 25 March 2011

Page 44: Test-Oriented Languages: Is it time for a new era?

Avoiding mock object chains

input.do().do().do() returns “foo”;

Friday, 25 March 2011

Page 45: Test-Oriented Languages: Is it time for a new era?

Static analysis lies at the heart of the Quilt compiler

The compiler tracks the state used in the class under test.

A bottom up analysis technique allows the compiler to exclude cross seam calls that cannot affect the test output.

Friday, 25 March 2011

Page 46: Test-Oriented Languages: Is it time for a new era?

The unit under test should be more than one class

!"#$%&#'($)%&"*'%+,&-"%&"#%-./0*1#2%3*11%4.2-#%&"#%5#-1,2,(.$%.4%,%'&677#5%2#&62$%&80#%4.2%&"*'%/#&".5%

91,''#'%,2#%)2.60#5%*$&.%,%:+,&-";%

+,&-"#'%,2#%'#0,2,&#5%78%:<#,/';%

Friday, 25 March 2011

Page 47: Test-Oriented Languages: Is it time for a new era?

Quilt Test: Running multiple patches together

!"#$%&'()%&

Friday, 25 March 2011

Page 48: Test-Oriented Languages: Is it time for a new era?

Quilt: A new approach to testing

Compiler stubs as much as possible

Compiler forces the isolation of the code under test

Methods are mocked not objects

There is no need to create mock or stub objects and inject them into the test

Developer needs to write the absolute minimum amount of test code

Can even test an existing class

Friday, 25 March 2011