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