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

Spekulations on Testing

Spekulations on Testing

Brief overview of Spek and its benefits
https://www.youtube.com/watch?v=hq2dNB0rOB4

Acb8c985ddf8a03c218569e471cc7322?s=128

John Shelley

October 11, 2016
Tweet

Transcript

  1. Spekulations on Testing Introduction to Spek

  2. John Shelley @jpshells

  3. Tests are specifications

  4. @Test public void testCalculator() { SampleCalculator calculator = new SampleCalculator();

    Int value = calculator.sum(2, 4); assertEquals(6, value); } Tests
  5. • Under what conditions is sum calculated? • What exactly

    is it doing? • What is the expected outcome? It could be better
  6. Speks class SimpleSpec: Spek({ describe("a calculator") { val calculator =

    SampleCalculator() . . . . . . . . . } })
  7. Speks class SimpleSpec: Spek({ describe("a calculator") { val calculator =

    SampleCalculator() it("should return the result of adding the first number to the second number") { val sum = calculator.sum(2, 4) assertEquals(6, sum) } . . . } })
  8. Speks class SimpleSpec: Spek({ describe("a calculator") { val calculator =

    SampleCalculator() it("should return the result of adding the first number to the second number") { val sum = calculator.sum(2, 4) assertEquals(6, sum) } it("should return the result of subtracting the second number from the first number") { val subtract = calculator.subtract(4, 2) assertEquals(2, subtract) } } })
  9. Spek • Built with by Jetbrains • Built with Kotlin

    • Runs with JUnit 4 (+ 5) • Specification Framework • Can test Java or Kotlin • Open Source • IntelliJ Plugin (Android Studio)
  10. class FixtureSpec: Spek({ describe("a group") { beforeEachTest { ... }

    context("a nested group") { beforeEachTest { ... } ... Fixtures
  11. Fixtures ... context("a nested group") { beforeEachTest { ... }

    it ("should work") { ... } } it("do something") { … } afterEachTest { ... } } })
  12. class SimpleCalculatorSpec: SubjectSpek <Calculator> ({ subject { Calculator() } (1)

    it("should return the result of adding the first number to the second number") { assertEquals(6, subject.sum(2, 4)) (2) } it("should return the result of subtracting the second number from the first number") { assertEquals(2, subject.subtract(4, 2)) (2) } }) Subjects
  13. (1) Tell Spek how to instantiate the subject, in this

    case a Calculator. This will be invoked for every test scope, which means each test scope will have a unique instance. (2) Use subject to access the instance of the subject. Subjects
  14. Kotlin Night in San Francisco

  15. • Dependency on Kotlin, which means they’re not always in

    sync. Caveats
  16. • Dependency on Kotlin, which means they’re not always in

    sync. • Studio Plugin isn’t 100% working, can be a bit flaky Caveats
  17. • Dependency on Kotlin, which means they’re not always in

    sync. • Studio Plugin isn’t 100% working, can be a bit flaky • Mockito! ◦ Null issues due to Kotlin’s nullability protection ▪ Use Mockito-Kotlin (https://github.com/nhaarman/mockito-kotlin) • Depends on Mockito 2.1.0 ◦ 2.1.0 was just released last month... Caveats
  18. Questions? https://jetbrains.github.io/spek/ http://jetbrains.github.io/spek/docs/ https://realm.io/news/kau-laura-kogler-kotlin-testing/ https://realm.io/news/kau-rob-fletcher-kotlin-testing/

  19. Happy Spekking