Structured unit testing using SPEK (Droidcon SF...

Structured unit testing using SPEK (Droidcon SF 2019)

Martin Petrulak

November 25, 2019

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

51 slides
  1. Martin Petrulak Droidcon San Francisco 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 Droidcon San Francisco 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 Droidcon San Francisco 2019 How to create a

    test • Extend : Spek{…} abstract class • Nesting functions • test{…} - assertions • group{…} - create hierarchy
  4. Martin Petrulak Droidcon San Francisco 2019 Execution order • Discovery

    • Top down build test three • Execute code in groups • Top down execution of • Registered fixtures for scopes (setup/teardown case) • Tests (assertions)
  5. Martin Petrulak Droidcon San Francisco 2019 Fixtures • before/afterGroup{…}. -

    only current scope • before/afterEachTest{…} - all current & nested scopes
  6. Martin Petrulak Droidcon San Francisco 2019 Memoized delegate • Caching

    mode • TEST - new instance per test • EACH_GROUP - new instance per group • SCOPE - same instance in current group & subgroups • INHERIT - ???
  7. Martin Petrulak Droidcon San Francisco 2019 How to run tests

    • Terminal • Plugin (recommenced)
  8. Martin Petrulak Droidcon San Francisco 2019 Specification style • group{…}

    -> describe{…} or context{…} • test{…} -> it{…} • before/afterEachTest{…} -> before/afterEach{…} • before/afterGroup{…} -> before/after{…}
  9. Martin Petrulak Droidcon San Francisco 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
  10. Martin Petrulak Droidcon San Francisco 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
  11. Martin Petrulak Droidcon San Francisco 2019 Gherkin • group{…} ->

    Feature{…} , Scenario{…} • test{…} -> Given{…}, When{…} And{…}, Then{…}
  12. Martin Petrulak Droidcon San Francisco 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
  13. Martin Petrulak Droidcon San Francisco 2019 Gherkin advantages • Looks

    sexy :) • Easy to understand by non tech people
  14. Martin Petrulak Droidcon San Francisco 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
  15. Martin Petrulak Droidcon San Francisco 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
  16. Martin Petrulak Droidcon San Francisco 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

More Decks by Martin Petrulak

Other Decks in Technology