Design for Testability in der Praxis David Völkel, codecentric AG http://commons.wikimedia.org/wiki/file:pit_crew_hudson_valley.jpg http://commons.wikimedia.org/wiki/file:carservice.jpg
David Völkel * @davidvoelkel * https://www.xing.com/profile/david_voelkel
anti-pattern hard to test code Gute Testbarkeit http://commons.wikimedia.org/wiki/file:pit_crew_hudson_valley.jpg http://commons.wikimedia.org/wiki/file:carservice.jpg
Überblick * Testbarkeit * Test Driven Design vs. Legacy * Isolation * Isolierende Refactorings
Testbarkeit = Aufwand fürs Testen Kriterien * operability * decomposability * observability * controllability http://commons.wikimedia.org/wiki/file:carservice.jpg
Design for Testability (DfT) Designziel * wenig Testaufwand Ursprung E-Technik Tests SUT http://commons.wikimedia.org/wiki/file:carservice.jpg
Design for Testability (DfT) Designziel * wenig Testaufwand Ursprung E-Technik * Testschnittstellen Tests SUT http://commons.wikimedia.org/wiki/file:carservice.jpg
Test Driven Design vs. Legacy
Test Driven Development write failing test (refactor) make the test pass http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc10413.jpg
Test Driven Development write failing test (refactor) (refactor) make the test pass http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc10413.jpg
Test Driven Development write failing test (refactor) (refactor) make the test pass Externe Qualität Funktional http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc10413.jpg
Test Driven Development write failing test (refactor) (refactor) make the test pass Externe Qualität Funktional Interne Qualität => Test Driven Design
Qualität durch Redundanz 2x * Problem * Sprache * Personen Code Test * deklarativ * beispielhaft * algorithmisch http://commons.wikimedia.org/wiki/file:pc-netzteil_%28redundant%29.jpg
Test Driven Design Test * Drive Design kontinuierlich minimalistisch (YAGNI) testbar <=> gutes Design (Designskills unabdingbar) Code http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc10413.jpg
Test Driven Design Test * Drive Design kontinuierlich minimalistisch (YAGNI) testbar <=> gutes Design (Designskills unabdingbar) Code http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc10413.jpg
Test Driven Design * Drive Design kontinuierlich minimalistisch (YAGNI) testbar <=> gutes Design (Designskills unabdingbar) * Feedback auf Design Listen to your tests! Test Code http://commons.wikimedia.org/wiki/file:1948_hans_breinlinger_-_clown_mit_spiegel.jpg
Legacy Code = keine Tests schlecht testbares Design * eigener Code * 3rd Party Code (Frameworks) http://commons.wikimedia.org/wiki/file:kopalnia_wieczorek_rozbite_okna_12.08.jpg?uselang=de
Legacy TDD Renovierung Doppelt schwierig * Im Team noch wenig KnowHow (TDD, Design) * Schwer testbarer Code http://commons.wikimedia.org/wiki/file:kopalnia_wieczorek_rozbite_okna_12.08.jpg?uselang=de http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc10413.jpg
Legacy TDD Renovierung * Lazy * Fixierung mit Systemtests * Refactoring * TDD / Unittests für neue Features http://commons.wikimedia.org/wiki/file:kopalnia_wieczorek_rozbite_okna_12.08.jpg?uselang=de http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc10413.jpg Nach Feathers 2004
Isolation
front door * KEINE Isolation * integration / round trip tests Test Testling depended on components (DOCs) Begriffe nach Meszaros 2007 http://commons.wikimedia.org/wiki/file:stork_hotel_door.jpg
integration tests are a scam (J.B. Rainsberger)
back door collaboration tests * Unittests * mit Test Doubles Test Mocks Begriffe nach Meszaros 2007 http://commons.wikimedia.org/wiki/file:dummy_of_a_police_car.jpg
contract tests * erfüllt DOC den Contract? * abgestimmt mit collaboration tests Begriffe nach J.B. Rainsberger Test http://commons.wikimedia.org/wiki/file:dummy_of_a_police_car.jpg
Blätter Unittests gegen Blätter * Isolation geschenkt Test http://commons.wikimedia.org/wiki/file:dummy_of_a_police_car.jpg
Isolation Test Nutzen * Klarer Fokus - Testbarkeit - Fehlerlokalisierung - Wartbarkeit * Performanz => schnelleres Feedback * Weniger Testfälle a * b => a + b http://commons.wikimedia.org/wiki/file:cocks_in_separate_cages,_thailand.jpg
Problem GUI Testing? Keine Isolation => schlechte Wartbarkeit => schlechte Performance => schlechte Fehlerlokalisierung => fehleranfällig http://upload.wikimedia.org/wikipedia/commons/9/97/fire_2011.jpg?uselang=de
Testpyramide GUI * Testcoverage API / Service Component / Integration Unit http://commons.wikimedia.org/wiki/file:piramida_cheopsa.jpg
Isolation vs. ATDD / Outside-In? http://commons.wikimedia.org/wiki/file:actress-fear-and-panic.jpg
Isolation vs. ATDD / Outside-In? * Outside-In geht auch gegen API * GUI Tests isolieren * Integration testen, nicht Funktionalitäten der Beteiligten
Isolierbarkeit Test Problem: Test Doubles nicht setzbar Test Doubles
Isolierbarkeit Test Problem: Test Doubles nicht setzbar Refactorings Isolierbar Test Doubles
Isolierende Refactorings
Beispiel: Logger
Probleme Controllability (INPUT) Observability (OUTPUT)
Ursachen statische Abhängigkeiten * Konstruktoraufrufe * Singletons
Statischer Aufruf => Objekte Zitat aus Freeman / Pryce 2009
Statischer Aufruf => Objekte Klasse <=> * weniger Aufwand * don't mock concrete classes Zitat aus Freeman / Pryce 2009 Interface * Abhängigkeiten expliziter geringer (ISP) * Semantik durch Rollen
Setzbarkeit Dependency Injection * DOCs setzbar * Konstruktor vs. Setter * kein static http://commons.wikimedia.org/wiki/file:syringe_glove_01.jpg
Stub mit Bordmittel Controllability (INPUT) * Input kontrollierbar
Stub mit Mockframework * einfacher bei breiteren Interfaces
Test Spy mit Bordmitteln Observability (OUTPUT)
Test Spy mit Mockframework * knapp * behavior verification
Transitive Abhängigkeiten trainwreck code Begriff von Freeman / Price 2009... Problem: Implizite Abhängigkeiten GUI Test Service http://upload.wikimedia.org/wikipedia/commons/b/b5/durango_silverton_train.jpg?uselang=de
Transitive Abhängigkeiten... * Abhängigkeit explizit * nur zu Nachbarn GUI Test Service http://commons.wikimedia.org/wiki/file:1918trainwreck.jpg Begriff trainwreck code von Freeman / Price 2009
Dependency Injection Kontext Abhängigkeit http://commons.wikimedia.org/wiki/file:syringe_glove_01.jpg Objekte * kennen nur Nachbarn
Fokus SRP => Klasse http://commons.wikimedia.org/wiki/file:crossroads.jpg
Unfokussiert... 2 Aspekte auf einmal
Extract Class Refactoring fokussierter... einfacher testbar...
Designprobleme Testbarkeit http://commons.wikimedia.org/wiki/file:policedog01.jpg
Designprobleme Testbarkeit * Abhängigkeitszyklen * fachlich nicht begründetete Abhängigkeiten * accidental complexity * Status im Testling * u.v.m. http://commons.wikimedia.org/wiki/file:policedog01.jpg
Listen DfT Strategie TDD http://commons.wikimedia.org/wiki/file:bluebird_land_speed_record_car_1935_rc1 http://commons.wikimedia.org/wiki/file:1948_hans_breinlinger_-_clown_mit_spiegel.jpg Isolation http://commons.wikimedia.org/wiki/file:cocks_in_separate_cages,_thailand.jpg
Quellen - http://www.testbarkeit.de - http://en.wikipedia.org/wiki/design_for_testing - Peter Zimmerer, 2012: Testability A Lever to Build Sustaining Systems, OOP Conference 2012 - http://secs.ceas.uc.edu/~cpurdy/sefall11/testing_payneetal_1997.pdf - Steve Freeman, Nat Pryce, 2009: Growing Object-Oriented Software guided by Tests - Micheal Feathers, 2004: Working effectively with legacy systems - Robert C. Martin, 2009: Clean Code - Gerard Meszaros, 2007: xunit Test Patterns bzw. http://xunitpatterns.com/ - http://de.wikipedia.org/wiki/gesetz_von_demeter - Testing Pyramid: - Mike Cohn, the forgotten layer of the test automation pyramid - http://blogs.agilefaqs.com/2011/02/01/inverting-the-testing-pyramid/ - J.B. Rainsberger: Integrations tests are a scam
Fragen und Diskussion! http://commons.wikimedia.org/wiki/file:carservice.jpg
Lizensiert unter Creative Commons 3.0 Attribution + Sharealike siehe http://creativecommons.org/licenses/by-sa/3.0/deed.de