25
HOW TO WRITE TESTABLE CODE S.O.L.I.D.

How To Write a Testable Code

Embed Size (px)

Citation preview

Page 1: How To Write a Testable Code

HOW TO WRITE TESTABLE CODES.O.L.I.D.

Page 2: How To Write a Testable Code

HOW TO WRITE TESTABLE CODE

What we will discuss today…

• What we won’t talk about• Some terminology• Single Responsibility• Open/Closed• Liskov Substitution Principle• Interface Segregation• Dependency Inversion• Review• Questions

Page 3: How To Write a Testable Code

INTRODUCTION

Andrew TrebbleSenior Developer

OPIN Software

Page 4: How To Write a Testable Code

• Why to test• How to write tests• What to test• How to integrate tests into your workflow• How to do Object Oriented Programming

WHAT WE WONT TALK ABOUT

Page 5: How To Write a Testable Code

S.O.L.I.D

WHAT WE WILL TALK ABOUT TODAY

Page 6: How To Write a Testable Code

But before we start…some terminology

Page 7: How To Write a Testable Code

• Class• Instance• Inheritance• Base class• Abstract class• Concrete class• Interface

OBJECT ORIENTED PROGRAMMING TERMS

Page 8: How To Write a Testable Code

Unit Tests• Tests a single class in complete isolation• All inputs are fake

Integration Tests• Test the components together to make sure they play well with each other• Test the interaction of your code with other parts of the system

Functional Tests• Test to make sure the system actually does what it is supposed to do• Run with a full system behind it

TYPES OF TESTS

Page 9: How To Write a Testable Code

What does that mean?• Your objects should do only one thing• It should be the only object in the code base that does that one job

S.O.L.I.D: SINGLE RESPONSIBILITY

Page 10: How To Write a Testable Code

Why?• Reduces code complexity• Ensures that bugs are isolated and easier to find and fix• Reduces the number of tests that you need to write to cover the class

S.O.L.I.D: SINGLE RESPONSIBILITY

Page 11: How To Write a Testable Code

Open? Drupal’s already open isn’t it?• Not that kind of open• Open to extension• Closed to change

S.O.L.I.D: OPEN / CLOSED

Page 12: How To Write a Testable Code

Why go to the trouble• Reduce the number of regressions you experience• Makes the code more mockable• No need to rewrite tests to account for the new functionality

S.O.L.I.D: OPEN / CLOSED

Page 13: How To Write a Testable Code

Oh yeah, of course! Goes without saying… What’s the Liskov Substitution Principle?• First proposed by Barbra Liskov of MIT in the late 80’s• States that in a computer program, if S is a subtype of T, then objects of type T may be replaced with

objects of type S without altering any of the desirable properties of that program• Makes code more resilient to changes in it’s dependencies

S.O.L.I.D: LISKOV SUBSTITUTION PRINCIPLE

Page 14: How To Write a Testable Code

Makes sense. Can you expand on why I need this, please?• Makes your classes independent of the implementations of their dependencies• Makes mocking possible

S.O.L.I.D: LISKOV SUBSTITUTION PRINCIPLE

Page 15: How To Write a Testable Code

I’m getting tired of trying to come up with ways to say “What’s that”?• A class should have as few methods as is feasible to implement its interface• More small interfaces instead of few big interfaces

S.O.L.I.D: INTERFACE SEGREGATION

Page 16: How To Write a Testable Code

Once again, Why?• Smaller interfaces are easier to write tests for.• Smaller interfaces have less reason to change

S.O.L.I.D: INTERFACE SEGREGATION

Page 17: How To Write a Testable Code

Please tell me more• The complement of the Liskov Substitution Principle• Dependencies should be abstract base classes or interfaces. Never a concrete implementation of one

of those• Forces loose coupling

S.O.L.I.D: DEPENDENCY INVERSION

Page 18: How To Write a Testable Code

Go ahead and inform me of the why• Loose coupling helps enforce dependency injection• Makes your classes independent of the exact types of its dependencies• Makes mocking possible through dependency injection

S.O.L.I.D: DEPENDENCY INVERSION

Page 19: How To Write a Testable Code

Dependency Injection• Make your classes dependent on abstract base classes or interfaces• Don’t ever instantiate another object inside of your object• Don’t rely on anything from outside your class unless it was passed in• Pass in all dependencies as instantiated objects• Your hooks will basically become just a bunch of setup that then pass to a object to

do the heavy lifting

REVIEW

Page 20: How To Write a Testable Code

Mocking• Write mock classes that implement the same interface or extend the same base class.• Mock objects do not do anything. They only pretend to.• Mock objects return the data that will put your class through it’s paces. You may need multiple tests

to ensure all code paths are executed and tested.

REVIEW

Page 21: How To Write a Testable Code

• Monthly meetup• First Wednesday of every month• From 6pm to 8pm• New and exciting topics• Expert speakers and presenters from the

community• Meet new people passionate about Drupal• Get support from the local community• FREE pop, beer & pizza

DRUPALYOW

Page 22: How To Write a Testable Code

OPIN Software is the company behind The Open Mic Podcast, which is released bi-weekly and discusses anything & everything Drupal!

We will be hosting a live podcast today at 4pm, in the business track room, and welcome everyone to attend and contribute by discussing what you learned today and what you are hoping for from the community in the future.

To listen to The Open Mic Podcast, please visit:

http://theopenmic.libsyn.com/

THE OPEN MIC PODCAST

Page 23: How To Write a Testable Code

OPIN Software is offering free Drupal 8 training sessions to the attendees of DrupalCamp Ottawa. Sessions will include:

• Drupal 8 Fundamentals• Drupal 8 Module Development• Drupal 8 Theming

If you are interested, please visit the OPIN booth upstairs.

FREE DRUPAL 8 TRAINING

Page 24: How To Write a Testable Code

THANK YOU!

QUESTIONS?

Page 25: How To Write a Testable Code

Go forth and Rock!