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
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