Download - Functional tests for dummies
0Nicole Cordes, T3DD16 - Unit tests for dummies
Functional 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 functional tests?“
» test a part of your software
» not about testing your functions or classes, but processes
» from a user's point of view
» for quality assurance (QA)
» unit tests don't ensure a correct workflow
» manual testing takes a lot of time
» encapsulated system (database) without site-effects or dependencies
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 one test after another
» test folder in typo3temp is created
» core is linked into test folder
» test extensions are linked into typo3conf/ext
» paths and files are linked
» LocalConfiguration.php is written
» PackageStates.php is written
» bootstrap is initialized
» database is created
» database scheme is imported
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/FunctionalTests.xml typo3conf/ext/ext_testing`
» ! be aware that the functional tests dirty your database
11Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS„How to use an own MySQL instance?“
On Unix
» sudo mkdir /tmp/mysqld-testing
» sudo chmod 700 /tmp/mysqld-testing
» sudo chown mysql:mysql /tmp/mysqld-testing
» sudo mysqld_safe --port=3307 --socket="/var/run/mysqld/mysqld-testing.sock" --user mysql --pid-
file="/var/run/mysqld/mysqld-testing.pid" --datadir="/tmp/mysqld-testing" --skip-grant-tables run
» typo3DatabaseHost="localhost" typo3DatabasePort=3307 typo3DatabaseName=“ext-testing" \
typo3DatabaseUsername=“root" typo3DatabasePassword="" \
typo3_src/bin/phpunit -c typo3/sysext/core/Build/FunctionalTests.xml
12Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS„How to use an own MySQL instance?“
On Windows (preparation)
» copy "C:\Program Files\MySQL Server\my.ini“ to "C:\Program Files\MySQL Server\my-testing.ini“
» change “port=3306” to “port=3307” in [client] and [mysqld] section
» change datadir=“C:/ProgramData/MySQL/MySQL Server 5.5/data” to
datadir=“C:/ProgramData/MySQL/MySQL Server 5.5/data-testing”
» change innodb_data_home_dir="C:/ProgramData/MySQL InnoDB Datafiles/“ to
innodb_data_home_dir="C:/ProgramData/MySQL InnoDB Datafiles Testing/"
» create folder “C:/ProgramData/MySQL/MySQL Server 5.5/data-testing”
» copy mysql folder, ib_logfile0 and ib_logfile1 from original data folder
» copy "C:/ProgramData/MySQL InnoDB Datafiles/“ to "C:/ProgramData/MySQL InnoDB Datafiles Testing/“
13Nicole Cordes, T3DD16 - Unit tests for dummies
RUNNING TESTS„How to use an own MySQL instance?“
On Windows (server handling)
» start server with "C:\Program Files\MySQL Server\bin\mysqld.exe" --defaults-file="C:\Program
Files\MySQL Server\my-testing.ini“
» the server starts in the foreground (no new prompt appears until the server exits later)
» stop the server with "C:\Program Files\MySQL Server\bin\mysqladmin.exe" -u root -p --port=3307
shutdown
» cmd /C “SET typo3DatabaseHost=localhost && SET typo3DatabasePort=3307 && \
SET typo3DatabaseName=ext-testing && SET typo3DatabaseUsername=root && \
SET typo3DatabasePassword= && \
typo3_src\bin\phpunit -c typo3/sysext/core/Build/FunctionalTests.xml”
18Nicole Cordes, T3DD16 - Unit tests for dummies
CODING FUNCTIONAL TESTS„How does functional testing work?“
I
» set up your expectations
» define static requirements
» add a test case for your class
» implement requirements and test (decouple objects as much as you can)
» implement assertion
19Nicole Cordes, T3DD16 - Unit tests for dummies
CODING FUNCTIONAL TESTS„How does functional testing work?“
II
» move your extension to GitHub
» set up the travis service in your repository settings
» add a composer.json file
» add .travis.yml
20Nicole Cordes, T3DD16 - Unit tests for dummies
Thank youfor your attention!