]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - doc/md/Unit-tests.md
htaccess: prevent accessing resources not managed by SCM
[github/shaarli/Shaarli.git] / doc / md / Unit-tests.md
1 ### Setup your environment for tests
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.
4
5 ### Install composer
6
7 You can either use:
8
9 - a system-wide version, e.g. installed through your distro's package manager
10 - a local version, downloadable [here](https://getcomposer.org/download/).
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
23 #### Install Shaarli dev dependencies
24
25 ```bash
26 $ cd /path/to/shaarli
27 $ composer update
28 ```
29
30 #### Install and enable Xdebug to generate PHPUnit coverage reports
31
32 See http://xdebug.org/docs/install
33
34 For Debian-based distros:
35 ```bash
36 $ aptitude install php5-xdebug
37 ```
38 For ArchLinux:
39 ```bash
40 $ pacman -S xdebug
41 ```
42
43 Then add the following line to `/etc/php/php.ini`:
44 ```ini
45 zend_extension=xdebug.so
46 ```
47
48 #### 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
121 By default, PHPUnit will run all suitable tests found under the `tests` directory.
122
123 Each test has 3 possible outcomes:
124
125 - `.` - success
126 - `F` - failure: the test was run but its results are invalid
127 - the code does not behave as expected
128 - dependencies to external elements: globals, session, cache...
129 - `E` - error: something went wrong and the tested code has crashed
130 - typos in the code, or in the test code
131 - dependencies to missing external elements
132
133 If Xdebug has been installed and activated, two coverage reports will be generated:
134
135 - a summary in the console
136 - a detailed HTML report with metrics for tested code
137 - to open it in a web browser: `firefox coverage/index.html &`
138
139 ### Executing specific tests
140
141 Add a [`@group`](https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.group) annotation in a test class or method comment:
142
143 ```php
144 /**
145 * Netscape bookmark import
146 * @group WIP
147 */
148 class BookmarkImportTest extends PHPUnit_Framework_TestCase
149 {
150 [...]
151 }
152 ```
153
154 To run all tests annotated with `@group WIP`:
155 ```bash
156 $ vendor/bin/phpunit --group WIP tests/
157 ```