Slide 1

Slide 1 text

Introducing tests... To a legacy codebase

Slide 2

Slide 2 text

Summary Introducing tests… ● To you ● To a legacy codebase ● To your team ● To the shipping pipeline

Slide 3

Slide 3 text

Introducing tests... To you “Program testing can be used to show the presence of bugs, but never to show their absence.” ~ Edsger Dijkstra

Slide 4

Slide 4 text

Disclaimer The next slide contains code. The code is probably terrible in many ways. As I will see it again I will probably want to delete and rewrite it. Tests allow me to do so. Don’t get attached. THE NEXT SLIDE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Slide 5

Slide 5 text

What is a test ? if (false) { throw(); } Independent program Runs against our code Double entry bookkeeping Applied to programming Introducing tests to you

Slide 6

Slide 6 text

Why test ? https://blog.pdark.de/2012/07/21/software-development-costs-bugfixing/ https://xbsoftware.com/blog/cost-bugs-software-testing/ Introducing tests to you The cost of bugs Assert that the program : ● does not fail ● won’t fail anymore ● won’t fail again Write bug free code Spot bugs as early as possible

Slide 7

Slide 7 text

Who is responsible for writing tests ? Not the C level. Not the project managers. Not the QA. We are.

Slide 8

Slide 8 text

Kinds of tests: Expectations Introducing tests to you https://martinfowler.com/articles/practical-test-pyramid.html Unit Tests Integration Tests End to end Tests

Slide 9

Slide 9 text

Kinds of tests: Reality Introducing tests to you https://watirmelon.blog/testing-pyramids/ Unit Tests Integration Tests End to end Tests Manual tests

Slide 10

Slide 10 text

Introducing tests to you ● What is a test ? ● Why test ? ● Who is responsible for tests ? ● Different kinds of tests ?

Slide 11

Slide 11 text

Introducing tests... To a legacy codebase

Slide 12

Slide 12 text

Where to start ? Build a test harness: ● Do not refactor anything yet ● Call functions and capture the output ● Write tests that assert you will keep the same output ● Make sure they all pass Introducing tests to a legacy codebase

Slide 13

Slide 13 text

New Features / Bugs : TDD Introducing tests to a legacy codebase https://lucetzer.github.io/tdd-and-red-green-cycle/index.html Write a test. If the test already passes, no need to write code Make the suite pass as quick as possible. Refactor the code and the tests, while keeping the suite valid.

Slide 14

Slide 14 text

Start refactoring: Command/Query Separation Introducing tests to a legacy codebase https://martinfowler.com/bliki/CommandQuerySeparation.html Please watch Sandi Metz & Katrina Owen! Queries Commands Example car.getColor() engine.start() When called, output Return values Don’t return anything When called, create Nothing Side effects Tests must Assert return value Assert side effects Assert that other commands are sent Try to separate commands and queries as much as possible, but figure out when you can’t ! (Atomicity, queue.pop() etc.)

Slide 15

Slide 15 text

Disclaimer The next slide contains code. The code is probably terrible in many ways. As I will see it again I will probably want to delete and rewrite it. Tests allow me to do so. Don’t get attached. THE NEXT SLIDE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Slide 16

Slide 16 text

Introducing tests to a legacy codebase The Tell don’t ask Principle : Before https://martinfowler.com/bliki/TellDontAsk.html

Slide 17

Slide 17 text

Introducing tests to a legacy codebase The Tell don’t ask Principle : After https://martinfowler.com/bliki/TellDontAsk.html

Slide 18

Slide 18 text

Introducing tests to a legacy codebase https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design The single responsibility principle: before

Slide 19

Slide 19 text

Introducing tests to a legacy codebase https://scotch.io/bar-talk/s-o-l-i-d-the-first-five-principles-of-object-oriented-design The single responsibility principle: after

Slide 20

Slide 20 text

Finish refactoring By now your tests cover the harness ● Harness tests are passing ● Acceptance tests are passing ● Code is readable ● App works as intended Delete the harness ! Introducing tests to a legacy codebase

Slide 21

Slide 21 text

Introducing tests to a Legacy codebase ● Where to start ? ● TDD ● CQS ● SRP ● TDA https://android.jlelse.eu/solid-principles-the-definitive-guide-75e30a284dea

Slide 22

Slide 22 text

Introducing tests... To your team

Slide 23

Slide 23 text

Automation ! Introducing tests to a team

Slide 24

Slide 24 text

Metrics: First Introducing tests to a team Gotta start somewhere huh ?

Slide 25

Slide 25 text

Metrics: Asking the team to write tests Introducing tests to a team

Slide 26

Slide 26 text

Metrics: Something interesting happened Introducing tests to a team

Slide 27

Slide 27 text

Metrics: Something interesting happened Introducing tests to a team

Slide 28

Slide 28 text

Metrics: Something interesting happened Introducing tests to a team You get coverage ! And you get coverage ! Every file gets coverage !

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Pair programming ! A few caveats: ● Keyboard and layout ● IDE Setup ● Remote ● Live share plugins https://www.commitstrip.com Introducing tests to a team

Slide 31

Slide 31 text

Introducing tests to a team ● Automation ● Metrics ● Pair programming Agile XP http://www.extremeprogramming.org/

Slide 32

Slide 32 text

Introducing tests... To the production pipeline

Slide 33

Slide 33 text

Automate the CI / CD Introducing tests to the production pipeline

Slide 34

Slide 34 text

Integrate tests to the CI Introducing tests to the production pipeline

Slide 35

Slide 35 text

Make sure no artefact is created if tests fail. Introducing tests to the production pipeline https://github.com/codedance/Retaliation

Slide 36

Slide 36 text

Introducing tests to the production pipeline ● Automate the CI/CD ● Add tests (and lints) to the pipeline ● Do not build artefacts with defects ● Make sure any team member knows how to ship code https://android.jlelse.eu/solid-principles-the-definitive-guide-75e30a284dea

Slide 37

Slide 37 text

Build a trust environment to your org ● Pair programming creates a learning / sharing environment ● An automated CI/CD pipeline builds team confidence ● Tests allow you to ship code faster, with less bugs ● Pragmatism allow members to iterate / improve the process Conclusion

Slide 38

Slide 38 text

Thank you !

Slide 39

Slide 39 text

Awesome links https://martinfowler.com - Martin Fowler blog (All of it, seriously) https://blog.cleancoder.com/ - Robert “Uncle Bob” C Martin’s blog https://medium.com/@kentbeck_7670 - Kent Beck Medium blog https://www.youtube.com/watch?v=URSWYvyc42M Sandy Metz - The magic tricks of testing https://vimeo.com/68730418 Katrina Owen - 467 tests, 0 failures, 0 confidence - Railsberry 2013

Slide 40

Slide 40 text

Thank you ! Jeremy Lempereur @o0Ignition0o Let’s share knowledge on the Polymorphic Slack channel : https://bit.ly/2HbNg6G