2014-09-21 02:07:58 +02:00
summary: Deploy robust applications by bundling Unit and Behavior tests with your application code and modules.
2011-02-07 07:48:44 +01:00
# Unit and Integration Testing
2013-10-04 03:24:56 +02:00
For behaviour testing in SilverStripe, check out [SilverStripe Behat Documentation ](https://github.com/silverstripe-labs/silverstripe-behat-extension/ ).
## Introduction
2012-03-24 22:16:59 +01:00
The SilverStripe core contains various features designed to simplify the process of creating and managing automated tests.
2011-02-07 07:48:44 +01:00
2013-10-04 03:24:56 +02:00
SilverStripe uses [PHPUnit ](http://www.phpunit.de ) for unit tests, and the framework contains features to simplify the
process of creating and managing tests.
If you're more familiar with unit testing, but want a refresher of some of the concepts and terminology, you can browse
2015-02-28 01:09:15 +01:00
the [Testing Glossary ](testing_glossary ). To get started now, follow the installation instructions below.
2013-10-04 03:24:56 +02:00
2014-09-08 07:13:36 +02:00
If you are familiar with PHP coding but new to unit testing then check out Mark's presentation [Getting to Grips with SilverStripe Testing ](http://www.slideshare.net/maetl/getting-to-grips-with-silverstripe-testing ).
2013-10-04 03:24:56 +02:00
2015-07-16 11:32:42 +02:00
You should also read over the [PHPUnit manual ](http://www.phpunit.de/manual/current/en/ ). It provides a lot of
2011-02-07 07:48:44 +01:00
fundamental concepts that we build on in this documentation.
2015-07-16 11:32:42 +02:00
## Running Tests
2011-02-07 07:48:44 +01:00
2015-07-16 11:32:42 +02:00
In order to run tests, you need to install SilverStripe using [/getting-started/composer ](Composer ),
which will pull in the required development dependencies to run tests.
These are not included in the standard archive downloads provided from silverstripe.org.
2014-09-08 07:13:36 +02:00
2015-07-16 11:32:42 +02:00
Tests are run from the commandline, in your webroot folder:
2014-09-12 07:02:04 +02:00
2015-07-16 11:32:42 +02:00
* `vendor/bin/phpunit` : Runs all tests (as defined by `phpunit.xml` )
* `vendor/bin/phpunit framework/tests/` : Run all tests of a specific module
* `vendor/bin/phpunit framework/tests/filesystem` : Run specific tests within a specific module
* `vendor/bin/phpunit framework/tests/filesystem/FolderTest.php` : Run a specific test
* `vendor/bin/phpunit framework/tests '' flush=all` : Run tests with optional request parameters (note the empty second argument)
2014-09-12 07:02:04 +02:00
2015-07-16 11:32:42 +02:00
Check the PHPUnit manual for all available [command line arguments ](http://www.phpunit.de/manual/current/en/textui.html ).
2011-02-07 07:48:44 +01:00
2015-07-16 11:32:42 +02:00
On Linux or OSX, you can avoid typing the full path on every invocation by adding `vendor/bin`
to your `$PATH` definition in the shell profile (usually `~/.profile` ): `PATH=./vendor/bin:$PATH`
## Generating a Coverage Report
PHPUnit can generate a code coverage report ([docs](http://www.phpunit.de/manual/current/en/code-coverage-analysis.html))
which shows you how much of your logic is executed by your tests. This is very useful to determine gaps in tests.
2011-02-07 07:48:44 +01:00
2015-07-16 11:32:42 +02:00
:::bash
vendor/bin/phpunit --coverage-html < output-folder > < optional-tests-folder >
2011-02-07 07:48:44 +01:00
2015-07-16 11:32:42 +02:00
To view the report, open the `index.html` in `<output-folder>` in a web browser.
Typically, only your own custom PHP code in your project should be regarded when producing these reports. To exclude
some `thirdparty/` directories add the following to the `phpunit.xml` configuration file.
:::xml
< filter >
< blacklist >
< directory suffix = ".php" > framework/dev/< / directory >
< directory suffix = ".php" > framework/thirdparty/< / directory >
< directory suffix = ".php" > cms/thirdparty/< / directory >
<!-- Add your custom rules here -->
< directory suffix = ".php" > mysite/thirdparty/< / directory >
< / blacklist >
< / filter >
## Configuration
The `phpunit` executable can be configured by [command line arguments ](http://www.phpunit.de/manual/current/en/textui.html )
or through an XML file. File-based configuration has
2013-09-12 19:22:46 +02:00
the advantage of enforcing certain rules across test executions (e.g. excluding files from code coverage reports), and
of course this information can be version controlled and shared with other team members.
2011-02-07 07:48:44 +01:00
2013-09-12 19:22:46 +02:00
SilverStripe comes with a default `phpunit.xml.dist` that you can use as a starting point. Copy the file into a new
`phpunit.xml` and customize to your needs - PHPUnit will auto-detect its existence, and prioritize it over the default
file.
2011-02-07 07:48:44 +01:00
2013-09-12 19:22:46 +02:00
There's nothing stopping you from creating multiple XML files (see the `--configuration` flag in
[PHPUnit documentation ](http://www.phpunit.de/manual/current/en/textui.html )). For example, you could have a
`phpunit-unit-tests.xml` and `phpunit-functional-tests.xml` file (see below).
2011-02-07 07:48:44 +01:00
2013-09-12 19:22:46 +02:00
### Database Permissions
2011-02-07 07:48:44 +01:00
2015-07-16 11:32:42 +02:00
SilverStripe tests create their own temporary database on every execution. Because of this the database user in your config file
2013-09-12 19:22:46 +02:00
should have the appropriate permissions to create new databases on your server, otherwise tests will not run.
2011-02-07 07:48:44 +01:00
## Writing Tests
2017-07-03 03:22:12 +02:00
Tests are written by creating subclasses of [SapphireTest ](api:SilverStripe\Dev\SapphireTest ). You should put tests for your site in the
2011-02-07 07:48:44 +01:00
`mysite/tests` directory. If you are writing tests for a module, put them in the `(modulename)/tests` directory.
Generally speaking, there should be one test class for each application class. The name of the test class should be the
application class, with "Test" as a suffix. For instance, we have all the tests for `SiteTree` in
2012-03-24 22:16:59 +01:00
`framework/tests/SiteTreeTest.php`
2011-02-07 07:48:44 +01:00
You will generally write two different kinds of test classes.
* **Unit Test:** Test the behaviour of one of your DataObjects.
* **Functional Test:** Test the behaviour of one of your controllers.
2012-03-24 22:16:59 +01:00
Tutorials and recipes for creating tests using the SilverStripe framework:
2011-02-07 07:48:44 +01:00
2015-02-19 03:47:57 +01:00
* [Creating a SilverStripe test ](how_tos/write_a_sapphiretest ): Writing tests to check core data objects
* [Creating a functional test ](how_tos/write_a_functionaltest ): An overview of functional tests and how to write a functional test
* [Testing Outgoing Email ](how_tos/testing_email ): An overview of the built-in email testing code