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

Clean Tests - Scala UA

Clean Tests - Scala UA

TDD isn’t news, and most everyone agrees that writing tests (before, during or after implementation) is valuable. Hardly anyone agrees on what good, clean tests look like, though; if your tests are a live specification of your codebase, don’t they deserve the same care and attention as your production code?
This talk focuses on how to keep your tests readable, simple and maintainable. Specifically we will discuss how the “given-when-then” pattern affects the way you factor your code, and showcase the remarkable differences between a sloppy specification and a well-factored one.

Noam Almog

April 07, 2017
Tweet

More Decks by Noam Almog

Other Decks in Programming

Transcript

  1. Can a single test Stand on its own? Yes No

    Show TOO MUCH Hide TOO MUCH
  2. public void testCancel_notDoneInterrupt() throws Exception { Future<?> future = newFutureInstance();

    future.cancel(true); try { future.get(); fail("Expected CancellationException"); } catch (CancellationException e) { assertNotNull(e.getCause()); } } 1. description 2. given 3. when 4. then Test structure
  3. public void testCancel_notDoneInterrupt() throws Exception { … } Test description

    "throws an exception reducing a canceled future" "Sends an email to the server with the site-id"
  4. try { future.get(); fail("Expected CancellationException"); } Test action WixBiRecordingStudio.record {

    logEvent() ←-- WHEN } andThenPlay { recording => recording.play must contain(exactly( aBiEventWithEntries("bar" -> "baz", "method" -> "foo"))) }
  5. Test Expectations person must beATeenager person must beAmericanTeenager person.age must

    be_>=(13) person.age must be_<=(18) person.location must_=== "USA"
  6. takeaways Test behavior, not code The Test should stand on

    its own USE ONLY VARIABLES YOU DEFINED yes rarely Never given when then Describe what, not how Start today, DON’T WAIT