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

Documenting Go Code With Beautiful Tests - GopherCon UK 2018

Documenting Go Code With Beautiful Tests - GopherCon UK 2018

Whether you believe in TDD or not, you must agree that it's unwise to build production code with no tests. They verify your application, but also act as a perfect documentation for the development team. I would like to show a couple of tips and tricks of how you can write pretty and effective tests.

Paweł Słomka

August 02, 2018
Tweet

More Decks by Paweł Słomka

Other Decks in Technology

Transcript

  1. @pawel_slomka ABOUT ME • Blogger at mycodesmells.com • Go developer

    at Ingrid • Co-organizer of GoWroc meetup @pawel_slomka
  2. @pawel_slomka ABOUT ME • Blogger at mycodesmells.com • Go developer

    at Ingrid • Co-organizer of GoWroc meetup • Runner @pawel_slomka
  3. @pawel_slomka ABOUT ME • Blogger at mycodesmells.com • Go developer

    at Ingrid • Co-organizer of GoWroc meetup • Runner • Millenial @pawel_slomka
  4. @pawel_slomka ABOUT ME • Blogger at mycodesmells.com • Go developer

    at Ingrid • Co-organizer of GoWroc meetup • Runner • Millenial @pawel_slomka
  5. @pawel_slomka ABOUT ME • Blogger at mycodesmells.com • Go developer

    at Ingrid • Co-organizer of GoWroc meetup • Runner • Millenial Alll GIFs by giphy.com @pawel_slomka
  6. @pawel_slomka WHY WE WRITE THEM 1 • Making sure code

    works 2 • Improve maintainability (will work in the future)
  7. @pawel_slomka WHY WE WRITE THEM 1 • Making sure code

    works 2 • Improve maintainability (will work in the future) 3 • Document usage
  8. @pawel_slomka WHY WE WRITE THEM NOW 1 2 3 FUTURE

    YOU FUTURE THEM 1 • Making sure code works 2 • Improve maintainability (will work in the future) 3 • Document usage
  9. @pawel_slomka WHY WE WRITE THEM NOW 1 2 3 FUTURE

    YOU FUTURE THEM 1 • Making sure code works 2 • Improve maintainability (will work in the future) 3 • Document usage
  10. @pawel_slomka LIFE CYCLE OF
 TEST CODE REWRITE ALL WTF IS

    THAT? FORGET PASSING? WRITE (first time)
  11. @pawel_slomka TDD DEAD So much effort! I want my code

    to work on the first take Not enough time (just enough to make code work)
  12. @pawel_slomka SIDE TRACK Who needs to start their day with

    a cup of coffee? What happens when there’s no coffee in the office in the morning?
  13. @pawel_slomka SIDE TRACK Who needs to start their day with

    a cup of coffee? What happens when there’s no coffee in the office in the morning?
  14. @pawel_slomka SIDE TRACK Who needs to start their day with

    a cup of coffee? What happens when there’s no coffee in the office in the morning?
  15. @pawel_slomka SIDE TRACK Who needs to start their day with

    a cup of coffee? What happens when there’s no coffee in the office in the morning? Go DROP some production database, works even better
  16. @pawel_slomka TDD ALIVE Helps you write testable code Makes you

    think about code design You are safe around TDD fanatics (they’re hidden everywhere!)
  17. @pawel_slomka TDD ALIVE Helps you write testable code Makes you

    think about code design You are safe around TDD fanatics (they’re hidden everywhere!) Keep you focused, just write a test and take a break
  18. @pawel_slomka WHY WE 
 DON’T WRITE THEM We are lazy

    (I checked, it works) Writing tests is hard (especially the first ones)
  19. @pawel_slomka WHY WE 
 DON’T WRITE THEM We are lazy

    (I checked, it works) Writing tests is hard (especially the first ones) The client didn’t pay for them (really?)
  20. @pawel_slomka • Readable output • Just enough to debug what

    went wrong BEAUTIFUL TESTS TEST REPORTS TEST BODY
  21. @pawel_slomka • Readable output • Just enough to debug what

    went wrong • Feedback loop is fast BEAUTIFUL TESTS TEST REPORTS TEST BODY
  22. @pawel_slomka • Readable output • Just enough to debug what

    went wrong • Feedback loop is fast BEAUTIFUL TESTS TEST REPORTS TEST BODY • Easy to read
  23. @pawel_slomka • Readable output • Just enough to debug what

    went wrong • Feedback loop is fast BEAUTIFUL TESTS TEST REPORTS TEST BODY • Easy to read • Easy to extend
  24. @pawel_slomka DON’T USE “VERBOSE” • With, it shows logs from

    all test cases… • Without verbose, you see logs only from failing tests:
  25. @pawel_slomka CHECK FOR 
 RACE CONDITIONS • Always check if

    you have race conditions • Sample, risky code:
  26. @pawel_slomka CHECK FOR 
 RACE CONDITIONS • …but tests can

    help here, too: • Without flag you might not notice anything…
  27. @pawel_slomka USE FLAGS 
 FOR “SLOW” TESTS • If some

    of your tests are slow: • …hide them behind a flag (supported by go test)
  28. @pawel_slomka AVOID CACHING RESULTS OF INTEGRATION TESTS • Go caches

    test results if code doesn’t change: • This won’t work for integration tests - you want to run them even if the test code didn’t change:
  29. @pawel_slomka WRITING ASSERTIONS • Use a library • https://github.com/stretchr/testify •

    https://github.com/matryer/is • Build your own, add context:
  30. @pawel_slomka USE “GOLDEN FILES” • Testing file generation • Don’t

    put expected output in test file • Create an actual file with expected output • Much easier to see what you want to see • Works as a documentation, too!
  31. @pawel_slomka CHECK FUNCTIONS • My favourite way of writing tests

    • Easy to read, easy to extend • For a function: • Create a check function type:
  32. @pawel_slomka CHECK FUNCTIONS • Create factories for checks for errors,

    values, etc: • Trick - shorthand function to use in table tests:
  33. @pawel_slomka CALL TO ACTION • Write tests to save your

    sleep • Write tests to save your free time
  34. @pawel_slomka CALL TO ACTION • Write tests to save your

    sleep • Write tests to save your free time • Write tests for those coming after you
  35. @pawel_slomka CALL TO ACTION • Write tests to save your

    sleep • Write tests to save your free time • Write tests for those coming after you