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

How to Love Unit Testing

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

How to Love Unit Testing

Unit Testing in Pester
PowerShell Summit 2018

Avatar for Brian Bunke

Brian Bunke

April 12, 2018
Tweet

Other Decks in Technology

Transcript

  1. Pester “Pester is the ubiquitous test and mock framework for

    PowerShell.” https://github.com/pester/Pester @brianbunke
  2. Common Questions Am I doing this right? What does a

    good test even look like? What’s not enough, or too much? @brianbunke
  3. Today’s Goals 1. Feel comfortable reading and writing unit tests

    2. Know some future pitfalls to watch out for @brianbunke
  4. Integration Testing Pain Points • Require infrastructure • Slow •

    Service interruptions • Order of tests matters • Root cause isolation @brianbunke
  5. What is Unit Testing? 1. Making your test suite discrete

    2. Isolating external dependencies from your tests • “Mocking” @brianbunke
  6. Integration Tests -- We’re Still Friends! You should know if

    an API has changed …eventually Limit, don’t eliminate @brianbunke
  7. Unit Test “Drawback” #1: Testable Code Mocking works with functions

    (and modules) Writing tests makes you write more testable code @brianbunke
  8. Unit Test Drawback #2: Parameter Types If unit testing isolates

    third-party dependencies, strongly typing our parameters is an issue @brianbunke
  9. Unit Test Drawback #3: “Tryhard” Mode Strive for “black box”

    over “white box” Tests are meant to help maintain and refactor Accept that bugs will always exist @brianbunke
  10. Limitations of ParameterFilter Mocks • Parameters must be explicitly named

    • Common names may collide with Pester internals • Use $PSBoundParameters instead @brianbunke
  11. Unit Test Drawback #4: Consistency Unit tests are optimistic about

    breaking changes, but we also need to look inward. @brianbunke
  12. How Can You Love Unit Tests? Write as few of

    them as possible. @brianbunke
  13. How Can You Love Unit Tests? •Trust is freeing •Fast

    •Independent •Repeatable @brianbunke
  14. How to Keep Loving Unit Tests • Single responsibility functions

    • Strongly typing parameters from dependencies? • Push everything possible toward the module level • When in doubt, keep it simple • Consider Test-Driven Development (TDD) @brianbunke
  15. PowerShell Summit Resources • Today’s talk • https://github.com/brianbunke/PesterUnitTesting2018 • 2018,

    Glenn Sarti • https://speakerdeck.com/glennsarti • 2017, Chris Hunt • https://github.com/cdhunt/pssummit2017-mocking • 2016, June Blender • https://youtu.be/jvvh9cpD_LM @brianbunke
  16. THANK YOU! Please use the event app or Sched.com to

    submit a session rating! @brianbunke brianbunke.com speakerdeck.com/brianbunke