45
Checkout and do composer install https://github.com/lmuzinic/phpday-pragmatic-tdd git clone [email protected]:lmuzinic/phpday-pragmatic-tdd.git cd phpday-pragmatic-tdd composer install vendor/bin/phpunit OK (1 test, 1 assertion) EXERCISE 0 There is <10 MB of packages to download, so you can use your mobile data if WIFI does not work.

EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

Checkout and do composer install https://github.com/lmuzinic/phpday-pragmatic-tdd

[email protected]:lmuzinic/phpday-pragmatic-tdd.gitcdphpday-pragmatic-tddcomposerinstallvendor/bin/phpunit

OK(1test,1assertion)

EXERCISE 0

There is <10 MB of packages to download, so you can use your mobile data if WIFI does not work.

Page 2: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

PRAGMATIC TDD

Page 3: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

@lmuzinicLuka Muzinic

HELLO

Page 4: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

bit.ly/phpday-codementor

Page 5: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

@code4hire

Page 6: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

2018.webcampzg.org

Page 7: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

WORKSHOP RULESASK QUESTIONS IF YOU STILL DO NOT UNDERSTAND, ASK QUESTIONS AGAIN DISCUSS RIGHT NOW, DO NOT WAIT FOR THE “RIGHT MOMENT”

Page 8: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

WHY WE NEED TESTING?

Page 9: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping
Page 10: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

WHY AM I HERE?

Page 11: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

ARE WE SOFTWARE TESTERS?

Page 12: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

AND YET WE KEEP ON SAYING WE WRITE TESTS…

Page 13: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

WHERE CAN I GET MORE OF THOSE TESTS?

Page 14: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

EXCUSES, EXCUSES

TESTS SLOW US DOWNWE WILL NEVER GET TO 100% CODE COVERAGE WE DO NOT HAVE TIME TO LEARN TESTING, WE’RE TO BUSY SHIPPING CODE

Page 15: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

EXCUSES, EXCUSESTESTS SLOW US DOWN?

Page 16: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

DO YOU OFTEN DO THIS? ~phptest.php

OR THIS http://localhost/test_problem.php

STOP TESTFILEING

Page 17: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

EXCUSES, EXCUSESWE WILL NEVER GET TO 100% CODE COVERAGE

Page 18: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

CODE COVERAGE

Page 19: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

CODE COVERAGE

Page 20: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

CODE COVERAGE

Page 21: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

CODE COVERAGE

Page 22: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

20 > 80

Page 23: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

EXCUSES, EXCUSESWE DO NOT HAVE TIME TO LEARN TESTING, WE’RE TO BUSY SHIPPING CODE

Page 24: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

//@TODO: STANDSTILL

Page 25: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

DON’T BE SCARED OF PHPUNIT*IT IS JUST A CODE RUNNER UNIT, INTEGRATION OR ACCEPTANCE TESTS SMOKE TESTS WEBSITE SCRAPER …

Page 26: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?

TESTS ANSWER QUESTIONS

Page 27: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?

TESTS ANSWER QUESTIONS

Page 28: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?

TESTS ANSWER QUESTIONS

Page 29: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?

TESTS ANSWER QUESTIONS

Page 30: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?

TESTS ANSWER QUESTIONS

Page 31: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?

TESTS ANSWER QUESTIONS

Page 32: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?

TESTS ANSWER QUESTIONS

Page 33: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

DISCUSSIONIMPLEMENT A FOOTBALL LEAGUE MANAGEMENT APP - We want to display standings table on our website

WHAT IS YOUR BIGGEST CONCERN?

Page 34: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

HOW TO WRITE TESTS?GIVEN WHEN THEN SETUP EXERCISE VERIFY TEARDOWN

Page 35: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

ANATOMY OF PHPUNIT TEST CASEclassTeamTestextendsPHPUnit\Framework\TestCase;{private$team;

publicfunctionsetUp(){$this->team=Team::create(‘HellasVerona');}

publicfunctiontestGetName(){$name=$this->team->getName();

$this->assertEquals('HellasVerona',$name);}}

Page 36: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

WORKSHOP RULESASK QUESTIONS IF YOU STILL DO NOT UNDERSTAND, ASK QUESTIONS AGAIN DISCUSS RIGHT NOW, DO NOT WAIT FOR THE “RIGHT MOMENT”

Page 37: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping
Page 38: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

Checkout and do composer install https://github.com/lmuzinic/phpday-pragmatic-tdd

[email protected]:lmuzinic/phpday-pragmatic-tdd.gitcdphpday-pragmatic-tddcomposerinstallvendor/bin/phpunit

OK(1test,1assertion)

EXERCISE 0

Page 39: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

EXERCISE 1WRITE A FUNCTIONAL TEST FOR OUR APP - main goal of the test is to show how our code is supposed to work - let us play with with the design, try out how it feels - decide how the output is going to look

Page 40: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

EXERCISE 2RUN TEST - understand why it is failing, look at error messages

DECIDE WHAT IS CRITICAL TO WORK - estimate if you have time to write tests now

WRITE A TEST FOR THAT - create a separate test file as our focus is shifting

GOTO RUN TEST :)

Page 41: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

EXERCISE 3TIE IT ALL TOGETHER - make all tests green

Page 42: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

@lmuzinic luka.muzinic.net/talks

Luka Muzinic

QUESTIONS? /r/AMA?

bit.ly/phpday-codementor

twitter.com/code4hire

joind.in/talk/186e2

Page 43: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

HOMEWORKReading list https://www.devmynd.com/blog/five-factor-testing/ https://martinfowler.com/articles/practical-test-pyramid.html https://dev.to/theobendixson/the-problem-that-unit-tests-solve-b2l https://blog.liplex.de/testing-private-and-protected-methods-with-phpunit/

Libraries https://github.com/sebastianbergmann/phpunit https://github.com/phpspec/phpspec https://github.com/Codeception/Codeception https://github.com/phpstan/phpstan https://github.com/infection/infection

READING LIST

Page 44: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

HOMEWORKWRITE ANOTHER TEST - given a scenario where two teams have played a tie - implement the feature

REFACTOR - given a scenario where RuleBook also defines how much points should each team get after match win,

write additional test case where each win gets you 2 points - implement the feature

Page 45: EXERCISE 0 - Luka Mužinić · 2019-11-09 · excuses, excuses tests slow us down we will never get to 100% code coverage we do not have time to learn testing, we’re to busy shipping

KTHXBAIPhotos by Les Anderson, Joshua Earle, Ian Espinosa and Tom Roberts on Unsplash