Structured unit
testing using Spek
Martin Petrulak
@martin_petrulak
Slide 2
Slide 2 text
Martin Petrulak mobilization 2019
Agenda
• Drawbacks? of JUnit
• Introduction of Spek
• Project set-up
• Core principles
• How to write a test ( 2 distinct styles)
• Specification
• Gherkin
• Wrap up & Questions
Slide 3
Slide 3 text
No content
Slide 4
Slide 4 text
Martin Petrulak mobilization 2019
JUnit - Readability
Slide 5
Slide 5 text
Martin Petrulak mobilization 2019
Readability
Slide 6
Slide 6 text
Martin Petrulak mobilization 2019
JUnit - Reusability
Slide 7
Slide 7 text
Martin Petrulak mobilization 2019
Reusability
Slide 8
Slide 8 text
Martin Petrulak mobilization 2019
Parametrised tests
Slide 9
Slide 9 text
Martin Petrulak mobilization 2019
Parametrised tests
Slide 10
Slide 10 text
Martin Petrulak mobilization 2019
Parametrised tests
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
Martin Petrulak mobilization 2019
Spek
• Unit testing framework
• It’s DSL allows you to write nested tests
• Uses jUnit 5 engine
• JVM / Android / Multiplatform
• JaCoCo, Assertions/Mocking - use whatever you like
• 1st commit 16/12/2012 - Hadi Hariri (talking Kotlin)
• 2 styles how to write tests : Specification & Gherkin
Slide 13
Slide 13 text
Martin Petrulak mobilization 2019
Setup - Project
Slide 14
Slide 14 text
Martin Petrulak mobilization 2019
Setup - Module
Slide 15
Slide 15 text
Martin Petrulak mobilization 2019
How to create the test
• Extend : Spek{…} abstract class
• Nesting lambdas
• test{…} - assertions
• group{…} - create hierarchy
Slide 16
Slide 16 text
Martin Petrulak mobilization 2019
How to create the test
Slide 17
Slide 17 text
Martin Petrulak mobilization 2019
Execution order
Slide 18
Slide 18 text
Martin Petrulak mobilization 2019
Execution order
Slide 19
Slide 19 text
Martin Petrulak mobilization 2019
Execution order
• Discovery
• Top down build test three
• Execute code in groups
• Top down execution of
• Tests
• Registered fixtures for scopes
Slide 20
Slide 20 text
Martin Petrulak mobilization 2019
Fixtures
• before/afterGroup{…}. - only current scope
• before/afterEachTest{…} - all current & nested scopes
Slide 21
Slide 21 text
Martin Petrulak mobilization 2019
Fixtures
Slide 22
Slide 22 text
Martin Petrulak mobilization 2019
Fixtures
Slide 23
Slide 23 text
Martin Petrulak mobilization 2019
Properties
Slide 24
Slide 24 text
Martin Petrulak mobilization 2019
Properties
Slide 25
Slide 25 text
Martin Petrulak mobilization 2019
Properties
Slide 26
Slide 26 text
Martin Petrulak mobilization 2019
Memoized delegate
• Caching mode
• TEST - new instance per test
• EACH_GROUP - new instance per group
• SCOPE - same instance among the scope(group)
• INHERIT - ???
Slide 27
Slide 27 text
Martin Petrulak mobilization 2019
Parametrised tests
Slide 28
Slide 28 text
Martin Petrulak mobilization 2019
How to run tests
• Terminal
• Plugin (recommenced)
Slide 29
Slide 29 text
Martin Petrulak mobilization 2019
How to run tests
Martin Petrulak mobilization 2019
Specification style
Slide 32
Slide 32 text
Martin Petrulak mobilization 2019
Specification style
Slide 33
Slide 33 text
Martin Petrulak mobilization 2019
Specification style
Slide 34
Slide 34 text
Martin Petrulak mobilization 2019
Skipping tests
Slide 35
Slide 35 text
Martin Petrulak mobilization 2019
Skipping tests
Slide 36
Slide 36 text
Martin Petrulak mobilization 2019
Skipping tests
Slide 37
Slide 37 text
Martin Petrulak mobilization 2019
Gherkin
• Written text - executable specification
• Most of the lines start with a special keyword
• Feature, Scenario, Given, When , Then, And
• Keywords are translated to multiple languages
Slide 38
Slide 38 text
Martin Petrulak mobilization 2019
Gherkin - example
Feature: Guess the word
# The first example has two steps
Scenario: Maker starts a game
When the Maker starts a game
Then the Maker waits for a Breaker to join
# The second example has three steps
Scenario: Breaker joins a game
Given the Maker has started a game with the word "silky"
When the Breaker joins the Maker's game
Then the Breaker must guess a word with 5 characters
Martin Petrulak mobilization 2019
Gherkin disadvantages
• Not so flexible regarding nesting
• Default caching mode EACH_SCOPE -> test are not
isolated ->if one test fails, its over
• No build in mechanism for skipping
• Given/When acts as a test in output and counts as a test
Slide 46
Slide 46 text
Martin Petrulak mobilization 2019
Gherkin advantages
• Looks sexy :)
• Easy to understand by QA
Slide 47
Slide 47 text
Martin Petrulak mobilization 2019
Summary
• Nested test are easy to read -> understand -> review
• Reusability made easy (memoized)
• Write parametrised tests as normal Kotlin code
• Not so many options as jUnit 5 to fine tune
• Specification vs Gherkin
• Can exist alongside existing jUnit tests -> give it a shot
Slide 48
Slide 48 text
Martin Petrulak mobilization 2019
Advices / Best practices
• println(…) is your friend
• never interact with property outside of before/after..{…}
• one assertion per test( it{…},Then{…}, And{…} )
• use test{…} only for assertions
Slide 49
Slide 49 text
Martin Petrulak mobilization 2019
Resources
• https://spekframework.org/
• https://github.com/spekframework/spek/
• https://cucumber.io/docs/gherkin/
• https://github.com/Petrulak/android-mvvm-spek
• //TODO slides link
@martin_petrulak
Slide 50
Slide 50 text
Martin Petrulak mobilization 2019
Slide 51
Slide 51 text
Martin Petrulak mobilization 2019
–me
“ Thank you very much ! Question time :) ”