SPEK

 SPEK

50efb6713bc19f8b4c77c6beb86537c7?s=128

Martin Petrulak

September 24, 2019
Tweet

Transcript

  1. Structured unit testing using Spek Martin Petrulak @martin_petrulak

  2. Martin Petrulak .droidconGreece 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
  3. Martin Petrulak .droidconGreece 2019 JUnit - Readability

  4. Martin Petrulak .droidconGreece 2019 Readability

  5. Martin Petrulak .droidconGreece 2019 JUnit - Reusability

  6. Martin Petrulak .droidconGreece 2019 Reusability

  7. Martin Petrulak .droidconGreece 2019 Parametrised tests

  8. Martin Petrulak .droidconGreece 2019 Parametrised tests

  9. Martin Petrulak .droidconGreece 2019 Parametrised tests

  10. None
  11. Martin Petrulak .droidconGreece 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
  12. Martin Petrulak .droidconGreece 2019 Setup - Project

  13. Martin Petrulak .droidconGreece 2019 Setup - Module

  14. Martin Petrulak .droidconGreece 2019 How to create the test •

    Extend : Spek{…} abstract class • Nesting lambdas • test{…} - assertions • group{…} - create hierarchy
  15. Martin Petrulak .droidconGreece 2019 How to create the test

  16. Martin Petrulak .droidconGreece 2019 Execution order

  17. Martin Petrulak .droidconGreece 2019 Execution order

  18. Martin Petrulak .droidconGreece 2019 Execution order • Discovery • Top

    down build test three • Execute code in groups • Top down execution of • Tests • Registered fixtures for scopes
  19. Martin Petrulak .droidconGreece 2019 Fixtures • before/afterGroup{…}. - only current

    scope • before/afterEachTest{…} - all current & nested scopes
  20. Martin Petrulak .droidconGreece 2019 Fixtures

  21. Martin Petrulak .droidconGreece 2019 Fixtures

  22. Martin Petrulak .droidconGreece 2019 Properties

  23. Martin Petrulak .droidconGreece 2019 Properties

  24. Martin Petrulak .droidconGreece 2019 Properties

  25. Martin Petrulak .droidconGreece 2019 Memoized delegate • Caching mode •

    TEST - new instance per test • EACH_GROUP - new instance per group • SCOPE - same instance among the scope(group) • INHERIT - ???
  26. Martin Petrulak .droidconGreece 2019 Parametrised tests

  27. Martin Petrulak .droidconGreece 2019 How to run tests • Terminal

    • Plugin (recommenced)
  28. Martin Petrulak .droidconGreece 2019 How to run tests

  29. Martin Petrulak .droidconGreece 2019 Specification style • group{…} -> describe{…}

    or context{…} • test{…} -> it{…} • before/afterEachTest{…} -> before/afterEach{…} • before/afterGroup{…} -> before/after{…}
  30. Martin Petrulak .droidconGreece 2019 Specification style

  31. Martin Petrulak .droidconGreece 2019 Specification style

  32. Martin Petrulak .droidconGreece 2019 Specification style

  33. Martin Petrulak .droidconGreece 2019 Skipping tests

  34. Martin Petrulak .droidconGreece 2019 Skipping tests

  35. Martin Petrulak .droidconGreece 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
  36. Martin Petrulak .droidconGreece 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
  37. Martin Petrulak .droidconGreece 2019 Gherkin • group{…} -> Feature{…} ,

    Scenario{…} • test{…} -> Given{…}, When{…} And{…}, Then{…}
  38. Martin Petrulak .droidconGreece 2019 Gherkin

  39. Martin Petrulak .droidconGreece 2019 Gherkin

  40. Martin Petrulak .droidconGreece 2019 Gherkin

  41. Martin Petrulak .droidconGreece 2019 Gherkin

  42. Martin Petrulak .droidconGreece 2019 Gherkin

  43. Martin Petrulak .droidconGreece 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
  44. Martin Petrulak .droidconGreece 2019 Gherkin advantages • Looks sexy :)

    • Easy to understand by QA
  45. Martin Petrulak .droidconGreece 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
  46. Martin Petrulak .droidconGreece 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
  47. Martin Petrulak .droidconGreece 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
  48. Martin Petrulak .droidconGreece 2019 –me “ Thank you very much

    ! Question time :) ”