Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Structured unit testing using SPEK (mobilization 2019)

Structured unit testing using SPEK (mobilization 2019)

Martin Petrulak

October 26, 2019
Tweet

More Decks by Martin Petrulak

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. Martin Petrulak mobilization 2019 How to create the test •

    Extend : Spek{…} abstract class • Nesting lambdas • test{…} - assertions • group{…} - create hierarchy
  4. 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
  5. Martin Petrulak mobilization 2019 Fixtures • before/afterGroup{…}. - only current

    scope • before/afterEachTest{…} - all current & nested scopes
  6. 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 - ???
  7. Martin Petrulak mobilization 2019 Specification style • group{…} -> describe{…}

    or context{…} • test{…} -> it{…} • before/afterEachTest{…} -> before/afterEach{…} • before/afterGroup{…} -> before/after{…}
  8. 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
  9. 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
  10. Martin Petrulak mobilization 2019 Gherkin • group{…} -> Feature{…} ,

    Scenario{…} • test{…} -> Given{…}, When{…} And{…}, Then{…}
  11. 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
  12. 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
  13. 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
  14. 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