Beter code for better tests

Post on 21-Jan-2018

413 views 0 download

Transcript of Beter code for better tests

@gil_zilberfeld@gil_zilberfeld

Better codefor better tests

@gil_zilberfeld

Hello!

I AM GIL ZILBERFELD

www.gilzilberfeld.com

www.everydayunittesting.com

@gil_zilberfeld

@gil_zilberfeld

First things first

◉ You need an IDE and a test framework

◉ You need the Gilded Rose code

◉ Make sure it compiles and you can write tests

that run

@gil_zilberfeld

Agenda

◉ Testing legacy code

◉ Making legacy code testable

◉ Testable code with TDD

@gil_zilberfeld@gil_zilberfeld

What makes code testable?

How would you ask a programmer to make it so?

@gil_zilberfeld

Exercise: The Gilded Rose

◉ Check out the code

◉ Create a test plan just from the code

◉ Do not share with other teams

@gil_zilberfeld

Exercise: Write tests

◉ According to your test plan

◉ Without changing the code

◉ What is hard?

◉ What is easy?

@gil_zilberfeld

Exercise: Test plan from requirements

◉ Add test cases according to the requirements

◉ Compare to the other test plan

◉ How is it different?

◉ How much impact does having the code and

requirement have?

@gil_zilberfeld@gil_zilberfeld

How does legacy code happen?

@gil_zilberfeld

The origin of legacy code

◉ Decisions taken too early

◉ Architecture is hard to change

◉ Centered around frameworks (and databases)

◉ Business logic is spread around

@gil_zilberfeld

Problems with legacy code

◉ Hard to find things

◉ Focus on technical aspects

◉ Not built for testing

@gil_zilberfeld@gil_zilberfeld

Refactoring for testability

@gil_zilberfeld

Better understanding

◉ Organize classes and files

◉ Rename classes

◉ Rename methods

◉ Rename files

@gil_zilberfeld

Better accessibility

◉ Change accessibility

◉ Introduce Field

◉ Add accessors

◉ Expose interfaces

◉ Introduce interface

◉ Add overload

@gil_zilberfeld

Reduce dependencies

◉ Move method

◉ Extract class

◉ Extract method

◉ Introduce Parameter

@gil_zilberfeld

Shared state issues

◉ Databases

◉ Files

◉ In-memory state

◉ Setup

◉ Clean up and error handling

@gil_zilberfeld

Exercise: Code modifications

◉ Small changes

◉ Make code clear and easy to test

◉ Make tests pass (obviously)

@gil_zilberfeld@gil_zilberfeld

Test driven development

To boldly go where no code has been before

@gil_zilberfeld

The TDD Cycle

@gil_zilberfeld

The three rules of TDD

◉ You may not write production code until you have

written a failing test

◉ You may not write more than one unit test than is

sufficient to fail, and not compiling is failing

◉ You may not write more production code than is

sufficient to pass the current test

@gil_zilberfeld

Let’s build a lightsaber!

@gil_zilberfeld

Exercise: Lightsaber

◉ Build a test plan for a light saber

@gil_zilberfeld@gil_zilberfeld

TDD Lightsaber

@gil_zilberfeld

Exercise

◉ Write test-first

◉ Better in pairs

◉ What is easy

◉ What is hard

◉ How is that different than the other exercises

@gil_zilberfeld

BENEFITS

Same tools

Better coverage

Less debugging

Better design

Think before your

code!

You can also split your content

ISSUES

Requires discipline

Works better for

greenfield projects

Takes time to get to great

results

The light side vs the dark side

@gil_zilberfeld

Summary

◉ Code has a lot if impact on how tests look like

◉ Understand the code

◉ Demand testability

◉ Collaborate for better code and better tests

@gil_zilberfeld@gil_zilberfeld

Resources

@gil_zilberfeld

Resources

◉ Code katas– CodeKatas.Org

◉ Cyber Dojo- Cyber-Dojo.org

@gil_zilberfeld

Thanks!

ANY QUESTIONS?

You can find me at:

@gil_zilberfeld

http://www.GilZilberfeld.com

http://www.EverydayUnitTesting.com

http://www.fastee.im