]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - doc/md/Unit-tests.md
Merge pull request #1552 from ArthurHoaro/feature/better-initializer
[github/shaarli/Shaarli.git] / doc / md / Unit-tests.md
CommitLineData
54ab5636 1The testing framework used is [PHPUnit](https://phpunit.de/); it can be installed with [Composer](https://getcomposer.org/), which is a dependency management tool.
53ed6d7d 2
54ab5636 3## Setup a testing environment
da9b0e3e 4
76c3a4db 5### Install composer
43ad7c8e 6
76c3a4db 7You can either use:
da9b0e3e 8
6fa3c87d 9- a system-wide version, e.g. installed through your distro's package manager (eg. `sudo apt install composer`)
10- a local version, downloadable [here](https://getcomposer.org/download/). To update a local composer installation, run `php composer.phar self-update`
53ed6d7d 11
da9b0e3e 12
54ab5636 13### Install Shaarli development dependencies
53ed6d7d 14
da9b0e3e 15```bash
16$ cd /path/to/shaarli
6fa3c87d 17$ composer install
da9b0e3e 18```
19
54ab5636 20### Install Xdebug
53ed6d7d 21
a8a38401 22Xdebug must be installed and enable for PHPUnit to generate coverage reports. See http://xdebug.org/docs/install.
76c3a4db 23
da9b0e3e 24```bash
7a7a5237 25# for Debian-based distributions
3b04d19a 26$ aptitude install php-xdebug
7a7a5237 27
28# for ArchLinux:
da9b0e3e 29$ pacman -S xdebug
30```
31
dbbcb0c6 32Then add the following line to `/etc/php/<PHP_VERSION>/cli/php.ini`:
a8a38401 33
da9b0e3e 34```ini
35zend_extension=xdebug.so
36```
37
54ab5636 38## Run unit tests
53ed6d7d 39
7a7a5237 40Run `make test` and ensure tests return `OK`. If tests return failures, refer to PHPUnit messages and fix your code/tests accordingly.
da9b0e3e 41
a8a38401 42By default, PHPUnit will run all suitable tests found under the `tests` directory. Each test has 3 possible outcomes:
43ad7c8e
V
43
44- `.` - success
45- `F` - failure: the test was run but its results are invalid
46 - the code does not behave as expected
47 - dependencies to external elements: globals, session, cache...
48- `E` - error: something went wrong and the tested code has crashed
49 - typos in the code, or in the test code
50 - dependencies to missing external elements
da9b0e3e 51
52If Xdebug has been installed and activated, two coverage reports will be generated:
43ad7c8e
V
53
54- a summary in the console
55- a detailed HTML report with metrics for tested code
56 - to open it in a web browser: `firefox coverage/index.html &`
b230bf20
A
57
58### Executing specific tests
53ed6d7d 59
60Add a [`@group`](https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.group) annotation in a test class or method comment:
b230bf20
A
61
62```php
63/**
64 * Netscape bookmark import
65 * @group WIP
66 */
67class BookmarkImportTest extends PHPUnit_Framework_TestCase
68{
53ed6d7d 69 [...]
b230bf20
A
70}
71```
72
73To run all tests annotated with `@group WIP`:
74```bash
75$ vendor/bin/phpunit --group WIP tests/
76```
54ab5636 77
78### Running tests inside Docker containers
79
80Test Dockerfiles are located under `tests/docker/<distribution>/Dockerfile`,
81and can be used to build Docker images to run Shaarli test suites under common
82Linux environments.
83
84Dockerfiles are provided for the following environments:
85
86- `alpine36` - [Alpine 3.6](https://www.alpinelinux.org/downloads/)
87- `debian8` - [Debian 8 Jessie](https://www.debian.org/DebianJessie) (oldstable)
88- `debian9` - [Debian 9 Stretch](https://wiki.debian.org/DebianStretch) (stable)
89- `ubuntu16` - [Ubuntu 16.04 Xenial Xerus](http://releases.ubuntu.com/16.04/) (LTS)
90
91What's behind the curtains:
92
93- each image provides:
94 - a base Linux OS
95 - Shaarli PHP dependencies (OS packages)
96 - test PHP dependencies (OS packages)
97 - Composer
98- the local workspace is mapped to the container's `/shaarli/` directory,
99- the files are rsync'd so tests are run using a standard Linux user account
100 (running tests as `root` would bypass permission checks and may hide issues)
101- the tests are run inside the container.
102
103To run tests inside a Docker container:
104
105```bash
106# build the Debian 9 Docker image for unit tests
107$ cd /path/to/shaarli
108$ cd tests/docker/debian9
109$ docker build -t shaarli-test:debian9 .
110
111# install/update 3rd-party test dependencies
112$ composer install --prefer-dist
113
114# run tests using the freshly built image
115$ docker run -v $PWD:/shaarli shaarli-test:debian9 docker_test
116
117# run the full test campaign
118$ docker run -v $PWD:/shaarli shaarli-test:debian9 docker_all_tests
119```