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

John Shelley

October 11, 2016
Tweet

More Decks by John Shelley

Other Decks in Technology

Transcript

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

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

    is it doing? • What is the expected outcome? It could be better
  3. 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) } . . . } })
  4. 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) } } })
  5. 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)
  6. class FixtureSpec: Spek({ describe("a group") { beforeEachTest { ... }

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

    it ("should work") { ... } } it("do something") { … } afterEachTest { ... } } })
  8. 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
  9. (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
  10. • Dependency on Kotlin, which means they’re not always in

    sync. • Studio Plugin isn’t 100% working, can be a bit flaky Caveats
  11. • 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