UNIT TESTING IN DRUPAL HOWDY! I am Mateu I am here because I love - - PowerPoint PPT Presentation

unit testing in drupal howdy
SMART_READER_LITE
LIVE PREVIEW

UNIT TESTING IN DRUPAL HOWDY! I am Mateu I am here because I love - - PowerPoint PPT Presentation

UNIT TESTING IN DRUPAL HOWDY! I am Mateu I am here because I love quality code. You can find me at @e0ipso HELLO! My name is Christian and I assert true. You can find me at @penyaskito 1. WHAT TO TEST Types of tests and when to


slide-1
SLIDE 1

UNIT TESTING IN DRUPAL

slide-2
SLIDE 2

HOWDY!

I am Mateu

I am here because I love quality code. You can find me at @e0ipso

slide-3
SLIDE 3

HELLO!

My name is Christian

… and I assert true. You can find me at @penyaskito

slide-4
SLIDE 4

1. WHAT TO TEST

Types of tests and when to choose

  • ne or the other
slide-5
SLIDE 5

Types of testing within Drupal

Kernel Test Unit Test Simple Test JavaScript Test

slide-6
SLIDE 6

Unit test

  • Testing a class

Kernel test

  • API tests
  • Integration tests

Simple test

  • Testing output (HTML)

JavaScript Tests

  • Testing behavior

When to choose one or the other?

slide-7
SLIDE 7

When to choose one or the other?

Unit Kernel Simple Complexity High Middle Low Speed High Middle Low Stability* Low Middle High Reliability High High Middle

* During the dev process

slide-8
SLIDE 8

Units of code Take a method in a class that produces an output and/or changes the state.

What is unit testing?

Testing Feed that method with several sets of inputs, and assert that the output is what you expect and the state is what you expect.

slide-9
SLIDE 9

Testing procedural code

› Cannot be unit tested. › It can be tested using SimpleTest. › It poisons OO code when called from a class. Use as little procedural code as possible!

slide-10
SLIDE 10

What about hooks?

› Declare a service and include the logic there › Inject dependencies to your service › Test your service › The hook only calls your service

slide-11
SLIDE 11

DEPENDENCY INJECTION

Do not use new in your code. Pass in all the objects to your class constructor.

slide-12
SLIDE 12

Procedural hook DO NOT INCLUDE ANY LOGIC!

slide-13
SLIDE 13

Service declaration INJECTS YOUR DEPENDENCIES

slide-14
SLIDE 14

Testable Service THIS CLASS HAS YOUR LOGIC!

slide-15
SLIDE 15

Inject your dependencies to replace them

During testing the injected

  • bjects can be replaced by

simplified substitutes called stubs. We’ll see how in a moment.

slide-16
SLIDE 16

Testing pyramid wants more unit tests

According to Mike Cohn End to end tests are less reliable and more brittle.

slide-17
SLIDE 17

“If you get a failure in a high level test, not just do you have a bug in your functional code, you also have a missing unit test” — Martin Fowler

[1]

slide-18
SLIDE 18

2. HOW TO TEST

Common techniques and tools for Unit and Kernel testing in Drupal

slide-19
SLIDE 19

“To know oneself, one should assert oneself.” — Albert Camus

[2]

slide-20
SLIDE 20

assert methods True/false, equals, same, null, array ops...

slide-21
SLIDE 21

MOCKING OBJECTS

We care about interactions

slide-22
SLIDE 22

MOCKING WITH PHPUNIT vs Prophecy

slide-23
SLIDE 23

Data Providers

Refactor your tests :-)

slide-24
SLIDE 24

Trick: Testing code that deals with procedural code

Wrap procedural code in a service Inject that service TEST NOW

CONTRIBUTE THAT!

slide-25
SLIDE 25

KERNEL TESTING

Lies in the middle of Unit and Simple testing

slide-26
SLIDE 26

3. WHEN TO RUN YOUR TESTS

What good are tests if you don’t run them

slide-27
SLIDE 27

Running tests consistently

DEVELOP TEST MERGE

slide-28
SLIDE 28

Running tests consistently

DEVELOP MERGE TEST

TDD IS NOW EASY IN DRUPAL!

slide-29
SLIDE 29

Have a CI tool? RUN YOUR TESTS THERE Pre commit Lint your code. Pre push Run your tests. Pull request Check coverage

slide-30
SLIDE 30

CI WITH DRUPAL.ORG TESTBOTS

slide-31
SLIDE 31

CI WITH TRAVIS

[3]

slide-32
SLIDE 32

CI WITH JENKINS (actually, D.O)

slide-33
SLIDE 33

Procedural hook DO NOT INCLUDE ANY LOGIC!

slide-34
SLIDE 34

4. CONCLUSIONS

Test things. Care about dependency

  • injection. Mock your dependencies.

Run your tests.

slide-35
SLIDE 35
slide-36
SLIDE 36