Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

99% is not enough

758 Aufrufe

Veröffentlicht am

Everyone agrees that unit tests are essential for building robust and maintainable software components. But how much code coverage should a project have? We claim that even 99% coverage is not enough.

We will show how we write tests, what we needed to do in order to make our code testable and why reaching the magical goal of 100% code coverage is so important to us.

Veröffentlicht in: Software
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

99% is not enough

  1. 1. 99%IS NOT ENOUGH
  2. 2. Sebastian Thoß Kruno Knego Sebastian Heuer Backend Lead Backend Developer Developer Advocate
  3. 3. Goods and Bads • fully object-oriented • easy refactoring thanks to 100% code coverage • not enough automated acceptance tests • dependencies to legacy software (database changes, API calls)
  4. 4. Code Coverage 100% != 100%
  5. 5. public function execute() { $orders = $this->orderService->getOrders($this->orderNumber); $json = $this->orderJsonMapper->map($orders); return new CommandResult($json); } Example
  6. 6. public function testExecute() { $service = $this->createMock(OrderService::class); $mapper = $this->createMock(OrderJsonMapper::class); $order1 = $this->createMock(Order::class); $order2 = $this->createMock(Order::class); $command = new GetOrdersCommand('123', $service, $mapper); $service->expects($this->once()) ->method('getOrders') ->with('123') ->willReturn([$order1, $order2]); $mapper->expects($this->once()) ->method('map') ->with([$order1, $order2]); $command->execute(); }
  7. 7. 100%!
  8. 8. public function execute() { $orders = $this->orderService->getOrders($this->orderNumber); $json = $this->orderJsonMapper->map($orders); return new CommandResult(json_encode('{"foo":"bar"}')); } Breaking Stuff
  9. 9. Still 100%!
  10. 10. Test Pyramid UNITTESTS
  11. 11. Unit Testing leads to SOLID(ish) code • Single Responsibility • Open/Closed • Liskov Substitution • Interface Segregation • Dependency Inversion
  12. 12. Test Pyramid INTEGRATION TESTS UNITTESTS
  13. 13. Class 1 Class 2 Class 3Mock Integration Test
  14. 14. Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS UNITTESTS
  15. 15. Class 1 Class 2 Class 3 Acceptance Test
  16. 16. Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS UNITTESTS ? $$$ ¢
  17. 17. <?php namespace KartenmachereiPhpughh; use PHPUnit_Framework_TestCase; class SmokeTest extends PHPUnit_Framework_TestCase { /** * @dataProvider furyUrlProvider * * @param Url $url */ public function testFuryUrl(Url $url) { $result = $this->sendGetRequest($url); $this->assertSame(200, $result->getStatusCode()); $this->assertNotEmpty($result->getBody()); $this->assertLessThanOrEqual(100, $result->getTimeToFirstByteInMilliseconds()); }
  18. 18. Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS UNITTESTS SMOKE TESTS $$$ ¢
  19. 19. Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS SMOKE TESTS 92%COVERAGE UNITTESTS
  20. 20. Test Pyramid ACCEPTANCE TESTS INTEGRATION TESTS SMOKE TESTS UNITTESTS 100%COVERAGE
  21. 21. Why 100%?
  22. 22. Why 100%? • Robustness • Feeling of security when refactoring • Fixing bugs is cheaper • Reducing amount of major bugs found in production • Broken Windows Theory
  23. 23. "Factory Denver5" by dliban licensed under CC BY-NC-ND 2 - https://flic.kr/p/73y9dg
  24. 24. "Factory Denver5" by dliban licensed under CC BY-NC-ND 2 - https://flic.kr/p/73y9dg
  25. 25. But what about… • Setters? • Factories? • Trivial Classes?
  26. 26. Tips & Tricks
  27. 27. Strict PHPUnit Config <?xml version="1.0" encoding="UTF-8"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.6/phpunit.xsd" bootstrap="bootstrap.php" backupGlobals="false" backupStaticAttributes="false" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" forceCoversAnnotation="true" beStrictAboutCoversAnnotation="true">
  28. 28. Mocking public function testCalculatorMultiplies() { $multiplier = $this->getMultiplierMock(); $calculator = new Calculator($multiplier); // (…) } /** * @return PHPUnit_Framework_MockObject_MockObject|Multiplier */ private function getMultiplierMock() { return $this->createMock(Multiplier::class); }
  29. 29. @codeCoverageIgnore • errorHandlers • shutdownHandlers Be very strict about excluding code from coverage!
  30. 30. @codeCoverageIgnore Be very strict about excluding code from coverage! /** * @param mixed $targetNode * * @throws WrongInstanceException */ private function ensureIsElement($targetNode) { if (!$targetNode instanceof fDOMElement) { // @codeCoverageIgnoreStart throw new WrongInstanceException('target node is not instance of fDOMElement'); // @codeCoverageIgnoreEnd } }
  31. 31. Don't test your privates! • Uncovered private methods are probably obsolete • "If covering private methods by only testing public methods is hard, there might be another class in there struggling to get out"
  32. 32. "Who's going to pay for all that?" RelativeCostofaBugfix 40x 80x 120x 160x Requirements Design Code Developer Tests Acceptance Tests Operations 1x 5x 10x 20x 50x 150x Source: Barry Boehm, Keynote Address, EQUITY 2007.
  33. 33. https://www.instagram.com/kartenmacherei/ Q&A https://www.facebook.com/kartenmacherei/ +49 40 468996861 jobs@kartenmacherei.de http://www.kartenmacherei.de/recruiting

×