]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - doc/md/Unit-tests.md
d200634f6ea6513ff5094abc7971e0752cd2b543
[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 Regarding Composer, you can either use:
6
7 - a system-wide version, e.g. installed through your distro's package manager
8 - a local version, downloadable [here](https://getcomposer.org/download/)
9
10 #### Sample usage
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 For Debian-based distros:
33 ```bash
34 $ aptitude install php5-xdebug
35 ```
36 For ArchLinux:
37 ```bash
38 $ pacman -S xdebug
39 ```
40
41 Then add the following line to `/etc/php/php.ini`:
42 ```ini
43 zend_extension=xdebug.so
44 ```
45
46 #### Run unit tests
47
48 Successful test suite:
49 ```bash
50 $ make test
51
52 -------
53 PHPUNIT
54 -------
55 PHPUnit 4.6.9 by Sebastian Bergmann and contributors.
56
57 Configuration read from /home/virtualtam/public_html/shaarli/phpunit.xml
58
59 ....................................
60
61 Time: 759 ms, Memory: 8.25Mb
62
63 OK (36 tests, 65 assertions)
64 ```
65
66 Test suite with failures and errors:
67 ```bash
68 $ make test
69 -------
70 PHPUNIT
71 -------
72 PHPUnit 4.6.9 by Sebastian Bergmann and contributors.
73
74 Configuration read from /home/virtualtam/public_html/shaarli/phpunit.xml
75
76 E..FF...............................
77
78 Time: 802 ms, Memory: 8.25Mb
79
80 There was 1 error:
81
82 1) LinkDBTest::testConstructLoggedIn
83 Missing argument 2 for LinkDB::__construct(), called in /home/virtualtam/public_html/shaarli/tests/Link\
84 DBTest.php on line 79 and defined
85
86 /home/virtualtam/public_html/shaarli/application/LinkDB.php:58
87 /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:79
88
89 --
90
91 There were 2 failures:
92
93 1) LinkDBTest::testCheckDBNew
94 Failed asserting that two strings are equal.
95 --- Expected
96 +++ Actual
97 @@ @@
98 -'e3edea8ea7bb50be4bcb404df53fbb4546a7156e'
99 +'85eab0c610d4f68025f6ed6e6b6b5fabd4b55834'
100
101 /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:121
102
103 2) LinkDBTest::testCheckDBLoad
104 Failed asserting that two strings are equal.
105 --- Expected
106 +++ Actual
107 @@ @@
108 -'e3edea8ea7bb50be4bcb404df53fbb4546a7156e'
109 +'85eab0c610d4f68025f6ed6e6b6b5fabd4b55834'
110
111 /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:133
112
113 FAILURES!
114 Tests: 36, Assertions: 63, Errors: 1, Failures: 2.
115 ```
116
117 #### Test results and coverage
118
119 By default, PHPUnit will run all suitable tests found under the `tests` directory.
120
121 Each test has 3 possible outcomes:
122
123 - `.` - success
124 - `F` - failure: the test was run but its results are invalid
125 - the code does not behave as expected
126 - dependencies to external elements: globals, session, cache...
127 - `E` - error: something went wrong and the tested code has crashed
128 - typos in the code, or in the test code
129 - dependencies to missing external elements
130
131 If Xdebug has been installed and activated, two coverage reports will be generated:
132
133 - a summary in the console
134 - a detailed HTML report with metrics for tested code
135 - to open it in a web browser: `firefox coverage/index.html &`
136
137 ### Executing specific tests
138
139 Add a [`@group`](https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.group) annotation in a test class or method comment:
140
141 ```php
142 /**
143 * Netscape bookmark import
144 * @group WIP
145 */
146 class BookmarkImportTest extends PHPUnit_Framework_TestCase
147 {
148 [...]
149 }
150 ```
151
152 To run all tests annotated with `@group WIP`:
153 ```bash
154 $ vendor/bin/phpunit --group WIP tests/
155 ```