Pragmatic Testing For developers More bang for your
Pragmatic Testing For developers More bang for your bucks
Agenda 1. Pragmatic testing strategy 2. Test Driven Development 3. Pattern(s) 4. Power tools 5. Resources Page 2 / Copyright © 2016 by Readify Limited
Pragmatic testing strategy Page 3 / Copyright © 2016 by Readify Limited
Should we even write unit tests? “Code without tests is bad code”, “The fact remains: good design is testable, and design that isn't testable is bad. “ - Michael Feathers “TDD is dead”, “Test-induced design damage” – DHH “I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence” – Kent Beck Page 4 / Copyright © 2016 by Readify Limited
Reach a level of confidence › Code coverage › Test pyramid › Do you need the same level of testing for a pacemaker and an exe creating release notes? Page 5 / Copyright © 2016 by Readify Limited
Test Driven Development Page 6 / Copyright © 2016 by Readify Limited
What is a unit test? “Unit of work” or “use case” rather than a function. A test is not a unit test if: › It talks to a database › It communicates across the network › It touches the file system › You have to do special things to your environment (such as editing configuration files) to run it Page 7 / Copyright © 2016 by Readify Limited
What is a good unit test? › Fast, tests that take too long to run end up not being run. › Isolated, independent of other tests. › Repeatable, repeat test over and over again and get the same result every time › Self-validating, as test get further from what they test, it is harder to determine what a test failure means. › Timely, being written at the correct time in the process. Which means before you actually write the production code (TDD) › Automated Page 8 / Copyright © 2016 by Readify Limited
How to write a unit test › Test the behaviour rather than the implementation. › Treat your test code as production code. › Follow the Arrange, Act, Assert pattern › Red / Green / Refactor › Ice breaker test and Devil's Advocate › Make you test fail to avoid false positive › Testing for equality? Json. Convert is your friend. › Assert one thing Page 9 / Copyright © 2016 by Readify Limited
Pattern(s) Page 10 / Copyright © 2016 by Readify Limited
Test data builder › Reaction to Object. Mother anti-pattern › Fluent builder › new Basket. Builder(). With. Large. Discount(). With. Special. Offer(). Build() Page 11 / Copyright © 2016 by Readify Limited
Power tools Page 12 / Copyright © 2016 by Readify Limited
x. Unit. net › Easy parameterized tests › Exception assertions › Feels C#ish (ctor, IDisposable) › Build server integration › Extensibility › Requires less typing on your keyboard › Microsoft is now using x. Unit for most of their open source projects. Page 13 / Copyright © 2016 by Readify Limited
Ecosystem › FAKE: pull a Nu. Get package, write 10 lines of code and you’ve a build system! › NSubstitute › Fluent Assertions: very useful for equality, collections, exceptions › BDDfy › Auto. Fixture › Pact. Net › JMeter Page 14 / Copyright © 2016 by Readify Limited
Resources Page 15 / Copyright © 2016 by Readify Limited
Books and blogs › “The Art of Unit Testing, Second Edition: with examples in C#” - Roy Osherove › “Working Effectively with Legacy Code” - Michael Feathers › Introduction to Unit Testing - Erik Dietrich › Classic TDD or "London School"? Page 16 / Copyright © 2016 by Readify Limited
Talks › TDD, where did it all go wrong - Ian Cooper › Integrated Tests Are A Scam - J. B. Rainsberger › Holistic testing - Jimmy Bogard › Microtesting: How we set fire to the testing pyramid while maintaining confidence – Rob Moore and Matt Davies › Using Formal Methods to Eliminate Exploitable Bugs - Kathleen Fisher Page 17 / Copyright © 2016 by Readify Limited
Pluralsight › Outside-In Test-Driven Development - Mark Seemann › Advanced Unit Testing - Mark Seemann Page 18 / Copyright © 2016 by Readify Limited
- Slides: 18