unit tests for dummies

21
Unit tests for dummies » Introduction » Basics » Hands on

Upload: cpsitgmbh

Post on 22-Jan-2018

844 views

Category:

Internet


2 download

TRANSCRIPT

0Nicole Cordes, T3DD16 - Unit tests for dummies

Unit tests for dummies

» Introduction

» Basics

» Hands on

1Nicole Cordes, T3DD16 - Unit tests for dummies

Introduction

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: [email protected]

3Nicole Cordes, T3DD16 - Unit tests for dummies

Basics

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

5Nicole Cordes, T3DD16 - Unit tests for dummies

BASICS„What are you talking about?“

test case

test

test

test

test

assertion

assertion

assertion

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

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

8Nicole Cordes, T3DD16 - Unit tests for dummies

Hands on

9Nicole Cordes, T3DD16 - Unit tests for dummies

USE CASE„ext_testing aka blog_example“

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`

11Nicole Cordes, T3DD16 - Unit tests for dummies

RUNNING TESTS„How to run tests within PhpStorm?“

» set up PHP version for your project

12Nicole Cordes, T3DD16 - Unit tests for dummies

RUNNING TESTS„How to run tests within PhpStorm?“

» set up configuration

13Nicole Cordes, T3DD16 - Unit tests for dummies

RUNNING TESTS„How to run tests within PhpStorm?“

» add environment variable

14Nicole Cordes, T3DD16 - Unit tests for dummies

RUNNING TESTS„How to run tests within PhpStorm?“

» run test class or single test

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 )

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

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!

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

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

20Nicole Cordes, T3DD16 - Unit tests for dummies

Thank youfor your attention!