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

Five approaches to testing

Five approaches to testing

There are many different reasons to test.

Unit testing allows us to plug into and execute our code without the application running.

It is an incredible tool to be able to handle. It can really speed things up.

See bundled github project for exercises and examples:
https://github.com/ddikman/five-ways-to-test

David Dikman

March 04, 2020
Tweet

More Decks by David Dikman

Other Decks in Programming

Transcript

  1. Overview - Why we do unit testing - The five

    approaches - Test Driven Development - Learning - Coverage - After-the-fact - Just don’t - Conclusion
  2. Why we do testing 01 To enable refactoring No tests

    > Fear of change > Exponential code rot 02 To prove functionality Test > Coverage > Increased confidence it works Unit (and other automated) tests. Find faults faster, speeds up 03 To document Test = This is how it should work
  3. Test driven development Red-Green-Refactor cycle 1. Write one single tiny

    test > run > fails 2. Write minimal code to make it pass > run > green 3. Improve (refactor) code to make it cleaner > run > green Suitable when: I know what I need to do. I know how to do it. I want to do it right
  4. Learning Similar to TDD. 1. Write a test to run

    some code 2. Debug or print outputs 3. Explore and learn 4. Rewrite with TDD (if needed) Works well when you know what you need to do but not quite how to do it. Backdoor to running some specific code. Also good for debugging.
  5. Coverage Get confidence to change. 1. Someone’s written some code

    2. You want to change it 3. Add coverage with tests 4. Change it If all tests still pass, you haven’t broken anything!
  6. After-the-fact Okay, so, some times, we write code first and

    tests afterwards. 1. You write some code 2. Your CI tells you your coverage has dropped 3. You add tests Why do this? Two reasons: 1. Do you really need all that code? 2. To be able to safely refactor
  7. Same process as with getting coverage! Only difference is, you’re

    not sure if your code actually works or not. So make sure you write test cases, not to test the code but to test the behaviour.
  8. Just don’t Then there are the times we just shouldn’t.

    Or, at least not with unit tests. Some of these are better for humans, UI test frameworks or integration tests. - Visual (texts, images, themes) - Data (default values, configuration) - SQL and API calls - UI interactions and animations
  9. Conclusion There are many different reasons to test. Unit testing

    allows us to plug into and execute our code without the application running. It is an incredible tool to be able to handle. It can really speed things up.