2016-05-15 00:33:52 -05:00
|
|
|
# Running System Tests
|
|
|
|
|
2022-07-18 08:42:12 +09:00
|
|
|
This is the quick-start to CodeIgniter testing. Its intent is to describe what
|
|
|
|
it takes to set up your system and get it ready to run unit tests.
|
|
|
|
It is not intended to be a full description of the test features that you can
|
|
|
|
use to test your application. Those details can be found in the documentation.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:46:38 +09:00
|
|
|
## Resources
|
|
|
|
|
2024-02-16 10:25:22 +09:00
|
|
|
* [CodeIgniter 4 User Guide on Testing](https://codeigniter.com/user_guide/testing/index.html)
|
2022-07-18 08:46:38 +09:00
|
|
|
* [PHPUnit docs](https://phpunit.de/documentation.html)
|
|
|
|
* [Any tutorials on Unit testing in CI4?](https://forum.codeigniter.com/showthread.php?tid=81830)
|
|
|
|
|
2016-05-15 00:33:52 -05:00
|
|
|
## Requirements
|
|
|
|
|
2022-07-18 08:42:12 +09:00
|
|
|
It is recommended to use the latest version of PHPUnit. At the time of this
|
|
|
|
writing we are running version 9.x. Support for this has been built into the
|
|
|
|
**composer.json** file that ships with CodeIgniter and can easily be installed
|
2019-01-18 00:24:08 -08:00
|
|
|
via [Composer](https://getcomposer.org/) if you don't already have it installed globally.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2022-11-15 14:53:24 +08:00
|
|
|
composer install
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:46:38 +09:00
|
|
|
If running under macOS or Linux, you can create a symbolic link to make running tests a touch nicer.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2022-11-15 14:53:24 +08:00
|
|
|
ln -s ./vendor/bin/phpunit ./phpunit
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-11-15 14:54:38 +08:00
|
|
|
You also need to install [Xdebug](https://xdebug.org/docs/install) in order
|
|
|
|
for code coverage to be calculated successfully. After installing `Xdebug`, you must
|
|
|
|
add `xdebug.mode=coverage` in the **php.ini** file to enable code coverage.
|
2019-01-18 00:24:08 -08:00
|
|
|
|
2019-10-17 20:06:50 -05:00
|
|
|
## Setting Up
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:42:12 +09:00
|
|
|
A number of the tests use a running database.
|
2022-08-24 11:17:22 +09:00
|
|
|
The default configuration uses SQLite3 transient in-memory database, so it works if you can use SQLite3.
|
2022-08-23 18:05:15 +09:00
|
|
|
|
|
|
|
In order to change the database for testing, edit the details for the `tests` group in
|
2022-08-23 20:29:29 +09:00
|
|
|
**app/Config/Database.php** or **phpunit.xml** or use **.env** file.
|
2022-08-23 18:05:15 +09:00
|
|
|
|
|
|
|
E.g.:
|
|
|
|
```
|
|
|
|
database.tests.hostname = localhost
|
|
|
|
database.tests.database = ci4_test
|
|
|
|
database.tests.username = root
|
|
|
|
database.tests.password = root
|
|
|
|
database.tests.DBDriver = MySQLi
|
|
|
|
database.tests.DBPrefix = db_
|
|
|
|
database.default.port = 3306
|
|
|
|
```
|
|
|
|
|
2022-07-18 08:46:38 +09:00
|
|
|
Make sure that you provide a database engine that is currently running on your machine.
|
2022-08-23 18:05:15 +09:00
|
|
|
|
2022-07-18 08:46:38 +09:00
|
|
|
More details on a test database setup are in the
|
2022-04-28 11:46:13 +09:00
|
|
|
[Testing Your Database](https://codeigniter4.github.io/CodeIgniter4/testing/database.html) section of the documentation.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:42:12 +09:00
|
|
|
If you want to run the tests without using live database you can
|
2022-11-15 14:56:04 +08:00
|
|
|
exclude `@DatabaseLive` group. This will make the tests run quite a bit faster.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-11-15 14:51:18 +08:00
|
|
|
## Groups
|
|
|
|
|
|
|
|
Each test class that we are running should belong to at least one
|
2024-05-10 07:50:22 +09:00
|
|
|
[Group](https://docs.phpunit.de/en/10.5/attributes.html#group) attribute to the
|
|
|
|
test class.
|
2022-11-15 14:51:18 +08:00
|
|
|
|
|
|
|
The available groups to use are:
|
|
|
|
|
|
|
|
| Group | Purpose |
|
|
|
|
| --------------- | --------------------------------------------------------------------- |
|
|
|
|
| AutoReview | Used for tests that perform automatic code reviews |
|
|
|
|
| CacheLive | Used for cache tests that need external services (redis, memcached) |
|
|
|
|
| DatabaseLive | Used for database tests that need to run on actual database drivers |
|
|
|
|
| SeparateProcess | Used for tests that need to run on separate PHP processes |
|
|
|
|
| Others | Used as a "catch-all" group for tests not falling in the above groups |
|
|
|
|
|
2016-05-15 00:33:52 -05:00
|
|
|
## Running the tests
|
|
|
|
|
2019-10-17 19:43:37 -05:00
|
|
|
The entire test suite can be run by simply typing one command-line command from the main directory.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2022-11-15 14:53:24 +08:00
|
|
|
./phpunit
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-04-28 11:52:09 +09:00
|
|
|
If you are using Windows, use the following command.
|
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2022-11-15 14:53:24 +08:00
|
|
|
vendor\bin\phpunit
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2022-04-28 11:52:09 +09:00
|
|
|
|
2022-07-18 08:42:12 +09:00
|
|
|
You can limit tests to those within a single test directory by specifying the
|
2019-01-18 00:24:08 -08:00
|
|
|
directory name after phpunit. All core tests are stored under **tests/system**.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2022-11-15 14:53:24 +08:00
|
|
|
./phpunit tests/system/HTTP/
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2019-10-17 19:43:37 -05:00
|
|
|
Individual tests can be run by including the relative path to the test file.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2022-11-15 14:53:24 +08:00
|
|
|
./phpunit tests/system/HTTP/RequestTest.php
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2021-10-17 22:55:21 +09:00
|
|
|
You can run the tests without running the live database and the live cache tests.
|
2016-06-04 17:39:56 +09:00
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2022-11-15 14:53:24 +08:00
|
|
|
./phpunit --exclude-group DatabaseLive,CacheLive
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2016-06-04 17:39:56 +09:00
|
|
|
|
2016-05-15 00:33:52 -05:00
|
|
|
## Generating Code Coverage
|
|
|
|
|
2023-11-29 01:23:13 +00:00
|
|
|
The coverage reports are generated by default after the execution of tests. These reports
|
|
|
|
include a textual overview and HTML reports which can be opened in a browser.
|
|
|
|
|
|
|
|
The text file can be found at **build/coverage/text/coverage.txt**.
|
|
|
|
The HTML files can be viewed by opening **build/coverage/html/index.html** in your favorite browser.
|
|
|
|
|
|
|
|
Alternatively you can use the following command:
|
2019-01-18 00:24:08 -08:00
|
|
|
|
2022-07-18 08:37:18 +09:00
|
|
|
```console
|
2023-11-29 08:06:58 +00:00
|
|
|
./phpunit --colors --coverage-text=build/coverage/text/coverage.txt --coverage-html=build/coverage/html/ -d memory_limit=1024m
|
2022-07-18 08:37:18 +09:00
|
|
|
```
|
2019-01-18 00:24:08 -08:00
|
|
|
|
2022-07-18 08:42:12 +09:00
|
|
|
This runs all of the tests again collecting information about how many lines,
|
|
|
|
functions, and files are tested. It also reports the percentage of the code that is covered by tests.
|
|
|
|
It is collected in two formats: a simple text file that provides an overview as well
|
|
|
|
as a comprehensive collection of HTML files that show the status of every line of code in the project.
|
2019-01-18 00:24:08 -08:00
|
|
|
|
|
|
|
|
|
|
|
## PHPUnit XML Configuration
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2019-10-17 19:43:37 -05:00
|
|
|
The repository has a ``phpunit.xml.dist`` file in the project root that's used for
|
|
|
|
PHPUnit configuration. This is used to provide a default configuration if you
|
|
|
|
do not have your own configuration file in the project root.
|
2016-05-15 00:33:52 -05:00
|
|
|
|
2019-01-18 00:24:08 -08:00
|
|
|
The normal practice would be to copy ``phpunit.xml.dist`` to ``phpunit.xml``
|
2019-10-17 19:43:37 -05:00
|
|
|
(which is git ignored), and to tailor it as you see fit.
|
2022-07-18 08:42:12 +09:00
|
|
|
For instance, you might wish to exclude database tests, or automatically generate
|
2019-10-17 19:43:37 -05:00
|
|
|
HTML code coverage reports.
|