diff options
Diffstat (limited to 'doc/md/Unit-tests.md')
-rw-r--r-- | doc/md/Unit-tests.md | 158 |
1 files changed, 60 insertions, 98 deletions
diff --git a/doc/md/Unit-tests.md b/doc/md/Unit-tests.md index f6030d5c..a9544656 100644 --- a/doc/md/Unit-tests.md +++ b/doc/md/Unit-tests.md | |||
@@ -1,126 +1,45 @@ | |||
1 | ### Setup your environment for tests | 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. |
2 | 2 | ||
3 | The framework used is [PHPUnit](https://phpunit.de/); it can be installed with [Composer](https://getcomposer.org/), which is a dependency management tool. | 3 | ## Setup a testing environment |
4 | 4 | ||
5 | ### Install composer | 5 | ### Install composer |
6 | 6 | ||
7 | You can either use: | 7 | You can either use: |
8 | 8 | ||
9 | - a system-wide version, e.g. installed through your distro's package manager | 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/). | 10 | - a local version, downloadable [here](https://getcomposer.org/download/). To update a local composer installation, run `php composer.phar self-update` |
11 | 11 | ||
12 | ```bash | ||
13 | # system-wide version | ||
14 | $ composer install | ||
15 | $ composer update | ||
16 | |||
17 | # local version | ||
18 | $ php composer.phar self-update | ||
19 | $ php composer.phar install | ||
20 | $ php composer.phar update | ||
21 | ``` | ||
22 | 12 | ||
23 | #### Install Shaarli dev dependencies | 13 | ### Install Shaarli development dependencies |
24 | 14 | ||
25 | ```bash | 15 | ```bash |
26 | $ cd /path/to/shaarli | 16 | $ cd /path/to/shaarli |
27 | $ composer update | 17 | $ composer install |
28 | ``` | 18 | ``` |
29 | 19 | ||
30 | #### Install and enable Xdebug to generate PHPUnit coverage reports | 20 | ### Install Xdebug |
31 | 21 | ||
32 | See http://xdebug.org/docs/install | 22 | Xdebug must be installed and enable for PHPUnit to generate coverage reports. See http://xdebug.org/docs/install. |
33 | 23 | ||
34 | For Debian-based distros: | ||
35 | ```bash | ||
36 | $ aptitude install php5-xdebug | ||
37 | ``` | ||
38 | For ArchLinux: | ||
39 | ```bash | 24 | ```bash |
25 | # for Debian-based distributions | ||
26 | $ aptitude install php-xdebug | ||
27 | |||
28 | # for ArchLinux: | ||
40 | $ pacman -S xdebug | 29 | $ pacman -S xdebug |
41 | ``` | 30 | ``` |
42 | 31 | ||
43 | Then add the following line to `/etc/php/php.ini`: | 32 | Then add the following line to `/etc/php/<PHP_VERSION>/cli/php.ini`: |
33 | |||
44 | ```ini | 34 | ```ini |
45 | zend_extension=xdebug.so | 35 | zend_extension=xdebug.so |
46 | ``` | 36 | ``` |
47 | 37 | ||
48 | #### Run unit tests | 38 | ## Run unit tests |
49 | |||
50 | Successful test suite: | ||
51 | ```bash | ||
52 | $ make test | ||
53 | |||
54 | ------- | ||
55 | PHPUNIT | ||
56 | ------- | ||
57 | PHPUnit 4.6.9 by Sebastian Bergmann and contributors. | ||
58 | |||
59 | Configuration read from /home/virtualtam/public_html/shaarli/phpunit.xml | ||
60 | |||
61 | .................................... | ||
62 | |||
63 | Time: 759 ms, Memory: 8.25Mb | ||
64 | |||
65 | OK (36 tests, 65 assertions) | ||
66 | ``` | ||
67 | |||
68 | Test suite with failures and errors: | ||
69 | ```bash | ||
70 | $ make test | ||
71 | ------- | ||
72 | PHPUNIT | ||
73 | ------- | ||
74 | PHPUnit 4.6.9 by Sebastian Bergmann and contributors. | ||
75 | |||
76 | Configuration read from /home/virtualtam/public_html/shaarli/phpunit.xml | ||
77 | |||
78 | E..FF............................... | ||
79 | |||
80 | Time: 802 ms, Memory: 8.25Mb | ||
81 | |||
82 | There was 1 error: | ||
83 | |||
84 | 1) LinkDBTest::testConstructLoggedIn | ||
85 | Missing argument 2 for LinkDB::__construct(), called in /home/virtualtam/public_html/shaarli/tests/Link\ | ||
86 | DBTest.php on line 79 and defined | ||
87 | |||
88 | /home/virtualtam/public_html/shaarli/application/LinkDB.php:58 | ||
89 | /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:79 | ||
90 | |||
91 | -- | ||
92 | |||
93 | There were 2 failures: | ||
94 | |||
95 | 1) LinkDBTest::testCheckDBNew | ||
96 | Failed asserting that two strings are equal. | ||
97 | --- Expected | ||
98 | +++ Actual | ||
99 | @@ @@ | ||
100 | -'e3edea8ea7bb50be4bcb404df53fbb4546a7156e' | ||
101 | +'85eab0c610d4f68025f6ed6e6b6b5fabd4b55834' | ||
102 | |||
103 | /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:121 | ||
104 | |||
105 | 2) LinkDBTest::testCheckDBLoad | ||
106 | Failed asserting that two strings are equal. | ||
107 | --- Expected | ||
108 | +++ Actual | ||
109 | @@ @@ | ||
110 | -'e3edea8ea7bb50be4bcb404df53fbb4546a7156e' | ||
111 | +'85eab0c610d4f68025f6ed6e6b6b5fabd4b55834' | ||
112 | |||
113 | /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:133 | ||
114 | |||
115 | FAILURES! | ||
116 | Tests: 36, Assertions: 63, Errors: 1, Failures: 2. | ||
117 | ``` | ||
118 | |||
119 | #### Test results and coverage | ||
120 | 39 | ||
121 | By default, PHPUnit will run all suitable tests found under the `tests` directory. | 40 | Run `make test` and ensure tests return `OK`. If tests return failures, refer to PHPUnit messages and fix your code/tests accordingly. |
122 | 41 | ||
123 | Each test has 3 possible outcomes: | 42 | By default, PHPUnit will run all suitable tests found under the `tests` directory. Each test has 3 possible outcomes: |
124 | 43 | ||
125 | - `.` - success | 44 | - `.` - success |
126 | - `F` - failure: the test was run but its results are invalid | 45 | - `F` - failure: the test was run but its results are invalid |
@@ -155,3 +74,46 @@ To run all tests annotated with `@group WIP`: | |||
155 | ```bash | 74 | ```bash |
156 | $ vendor/bin/phpunit --group WIP tests/ | 75 | $ vendor/bin/phpunit --group WIP tests/ |
157 | ``` | 76 | ``` |
77 | |||
78 | ### Running tests inside Docker containers | ||
79 | |||
80 | Test Dockerfiles are located under `tests/docker/<distribution>/Dockerfile`, | ||
81 | and can be used to build Docker images to run Shaarli test suites under common | ||
82 | Linux environments. | ||
83 | |||
84 | Dockerfiles 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 | |||
91 | What'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 | |||
103 | To 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 | ``` | ||