]>
Commit | Line | Data |
---|---|---|
da9b0e3e | 1 | ### Setup your environment for tests |
53ed6d7d | 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. | |
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 | |
13 | #### Install Shaarli dev dependencies | |
53ed6d7d | 14 | |
da9b0e3e | 15 | ```bash |
16 | $ cd /path/to/shaarli | |
6fa3c87d | 17 | $ composer install |
da9b0e3e | 18 | $ composer update |
19 | ``` | |
20 | ||
21 | #### Install and enable Xdebug to generate PHPUnit coverage reports | |
53ed6d7d | 22 | |
76c3a4db | 23 | See http://xdebug.org/docs/install |
24 | ||
da9b0e3e | 25 | For Debian-based distros: |
26 | ```bash | |
27 | $ aptitude install php5-xdebug | |
28 | ``` | |
29 | For ArchLinux: | |
30 | ```bash | |
31 | $ pacman -S xdebug | |
32 | ``` | |
33 | ||
34 | Then add the following line to `/etc/php/php.ini`: | |
35 | ```ini | |
36 | zend_extension=xdebug.so | |
37 | ``` | |
38 | ||
39 | #### Run unit tests | |
53ed6d7d | 40 | |
da9b0e3e | 41 | Successful test suite: |
42 | ```bash | |
43 | $ make test | |
44 | ||
45 | ------- | |
46 | PHPUNIT | |
47 | ------- | |
48 | PHPUnit 4.6.9 by Sebastian Bergmann and contributors. | |
49 | ||
50 | Configuration read from /home/virtualtam/public_html/shaarli/phpunit.xml | |
51 | ||
52 | .................................... | |
53 | ||
54 | Time: 759 ms, Memory: 8.25Mb | |
55 | ||
56 | OK (36 tests, 65 assertions) | |
57 | ``` | |
58 | ||
59 | Test suite with failures and errors: | |
60 | ```bash | |
61 | $ make test | |
62 | ------- | |
63 | PHPUNIT | |
64 | ------- | |
65 | PHPUnit 4.6.9 by Sebastian Bergmann and contributors. | |
66 | ||
67 | Configuration read from /home/virtualtam/public_html/shaarli/phpunit.xml | |
68 | ||
69 | E..FF............................... | |
70 | ||
71 | Time: 802 ms, Memory: 8.25Mb | |
72 | ||
73 | There was 1 error: | |
74 | ||
75 | 1) LinkDBTest::testConstructLoggedIn | |
76 | Missing argument 2 for LinkDB::__construct(), called in /home/virtualtam/public_html/shaarli/tests/Link\ | |
77 | DBTest.php on line 79 and defined | |
78 | ||
79 | /home/virtualtam/public_html/shaarli/application/LinkDB.php:58 | |
80 | /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:79 | |
81 | ||
82 | -- | |
83 | ||
84 | There were 2 failures: | |
85 | ||
86 | 1) LinkDBTest::testCheckDBNew | |
87 | Failed asserting that two strings are equal. | |
88 | --- Expected | |
89 | +++ Actual | |
90 | @@ @@ | |
91 | -'e3edea8ea7bb50be4bcb404df53fbb4546a7156e' | |
92 | +'85eab0c610d4f68025f6ed6e6b6b5fabd4b55834' | |
93 | ||
94 | /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:121 | |
95 | ||
96 | 2) LinkDBTest::testCheckDBLoad | |
97 | Failed asserting that two strings are equal. | |
98 | --- Expected | |
99 | +++ Actual | |
100 | @@ @@ | |
101 | -'e3edea8ea7bb50be4bcb404df53fbb4546a7156e' | |
102 | +'85eab0c610d4f68025f6ed6e6b6b5fabd4b55834' | |
103 | ||
104 | /home/virtualtam/public_html/shaarli/tests/LinkDBTest.php:133 | |
105 | ||
106 | FAILURES! | |
107 | Tests: 36, Assertions: 63, Errors: 1, Failures: 2. | |
108 | ``` | |
109 | ||
110 | #### Test results and coverage | |
53ed6d7d | 111 | |
da9b0e3e | 112 | By default, PHPUnit will run all suitable tests found under the `tests` directory. |
113 | ||
114 | Each test has 3 possible outcomes: | |
43ad7c8e V |
115 | |
116 | - `.` - success | |
117 | - `F` - failure: the test was run but its results are invalid | |
118 | - the code does not behave as expected | |
119 | - dependencies to external elements: globals, session, cache... | |
120 | - `E` - error: something went wrong and the tested code has crashed | |
121 | - typos in the code, or in the test code | |
122 | - dependencies to missing external elements | |
da9b0e3e | 123 | |
124 | If Xdebug has been installed and activated, two coverage reports will be generated: | |
43ad7c8e V |
125 | |
126 | - a summary in the console | |
127 | - a detailed HTML report with metrics for tested code | |
128 | - to open it in a web browser: `firefox coverage/index.html &` | |
b230bf20 A |
129 | |
130 | ### Executing specific tests | |
53ed6d7d | 131 | |
132 | 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 |
133 | |
134 | ```php | |
135 | /** | |
136 | * Netscape bookmark import | |
137 | * @group WIP | |
138 | */ | |
139 | class BookmarkImportTest extends PHPUnit_Framework_TestCase | |
140 | { | |
53ed6d7d | 141 | [...] |
b230bf20 A |
142 | } |
143 | ``` | |
144 | ||
145 | To run all tests annotated with `@group WIP`: | |
146 | ```bash | |
147 | $ vendor/bin/phpunit --group WIP tests/ | |
148 | ``` |