3. 2Nicole Cordes, T3DD16 - Unit tests for dummies
INTRODUCTION
โWho is that girl?โ
ยป Nicole Cordes
ยป working at CPS-IT GmbH in Berlin
ยป community activity since 2011
ยป Core and Security Team member
ยป contributing to multiple public extensions
ยป Slack: @IchHabRecht
ยป Twitter: @IchHabRecht
ยป Mail: typo3@cordes.co
5. 4Nicole Cordes, T3DD16 - Unit tests for dummies
BASICS
โWhy do I need unit tests?โ
ยป verify your code does what you expect
ยป manual testing is slow and cumbersome
ยป automate testing is fast and cool
ยป new changes donโt break existing functionality
ยป keep others from breaking your code
ยป safety net for refactorings
6. 5Nicole Cordes, T3DD16 - Unit tests for dummies
BASICS
โWhat are you talking about?โ
test case
test
test
test
test
assertion
assertion
assertion
7. 6Nicole Cordes, T3DD16 - Unit tests for dummies
BASICS
โWhat are you talking about?โ
Processing of a test case
ยป find all tests within the class
ยป call dataProvider and store return values
ยป call one test after another
8. 7Nicole Cordes, T3DD16 - Unit tests for dummies
BASICS
โWhat are you talking about?โ
The life cycle of a test
ยป create an object of the test class
ยป call `setUp` method (protected!)
ยป objects and environment can be prepared
ยป call one test
ยป call `tearDown` method (protected!)
ยป objects and environment can be cleaned up
10. 9Nicole Cordes, T3DD16 - Unit tests for dummies
USE CASE
โext_testing aka blog_exampleโ
11. 10Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS
โHow to run tests from CLI?โ
ยป clone TYPO3 core
ยป run `composer install`
ยป use the cloned core for your development
ยป run `typo3_src/bin/phpunit -c typo3/sysext/core/Build/UnitTests.xml typo3conf/ext/ext_testing`
12. 11Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS
โHow to run tests within PhpStorm?โ
ยป set up PHP version for your project
13. 12Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS
โHow to run tests within PhpStorm?โ
ยป set up configuration
14. 13Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS
โHow to run tests within PhpStorm?โ
ยป add environment variable
15. 14Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS
โHow to run tests within PhpStorm?โ
ยป run test class or single test
16. 15Nicole Cordes, T3DD16 - Unit tests for dummies
GENERATED TESTS
โWhy should generated tests be deleted?โ
ControllerTest
ยป due to Extbase all generated actions are very small
ยป aggregate information and passing it to the view
ยป test if data is visible (= assigned correctly) is not part of unit testing
ยป acceptance testing
ยป all those tests should be rewritten as functional tests (see you on Sunday ๏)
17. 16Nicole Cordes, T3DD16 - Unit tests for dummies
GENERATED TESTS
โWhy should generated tests be deleted?โ
ModelTest
ยป generated tests simply test getter and setter
ยป no need to test getter and/or setter as long as they donโt implement functionality
18. 17Nicole Cordes, T3DD16 - Unit tests for dummies
CODING REAL UNIT TESTS
โHow does real unit testing work?โ
I
ยป no own logic means no need for unit tests
ยป we donโt need to test the core
ยป tests for Extbase are done within the core
ยป no need for unit tests doesnโt mean no need for other kinds of testing!
19. 18Nicole Cordes, T3DD16 - Unit tests for dummies
CODING REAL UNIT TESTS
โHow does real unit testing work?โ
II
ยป add own logic
ยป add test case for your class
ยป add white- and blacklist tests
ยป switch to dataProvider and think about edge cases
20. 19Nicole Cordes, T3DD16 - Unit tests for dummies
CODING REAL UNIT TESTS
โHow does real unit testing work?โ
III
ยป add more advanced logic
ยป add test case for your class
ยป remove coupling to external dependencies (mocking)
ยป define test expectations
ยป switch mocks to prophecies to be more verbose on decoupling