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
* [CodeIgniter 4 User Guide on Testing ](https://codeigniter4.github.io/userguide/testing/index.html )
* [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
> composer install
```
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
> ln -s ./vendor/bin/phpunit ./phpunit
```
2016-05-15 00:33:52 -05:00
2022-04-17 01:18:58 +03:30
You also need to install [XDebug ](https://xdebug.org/docs/install ) in order
2022-04-30 01:44:22 +03:30
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
exclude `@DatabaseLive` group. Or make a copy of **phpunit.dist.xml** -
2021-10-20 09:20:24 +09:00
call it **phpunit.xml** - and comment out the `<testsuite>` named `Database` . This will make
2020-02-20 21:47:49 +00:00
the tests run quite a bit faster.
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
> ./phpunit
```
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
> vendor\bin\phpunit
```
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
> ./phpunit tests/system/HTTP/
```
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
> ./phpunit tests/system/HTTP/RequestTest.php
```
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
> ./phpunit --exclude-group DatabaseLive,CacheLive
```
2016-06-04 17:39:56 +09:00
2016-05-15 00:33:52 -05:00
## Generating Code Coverage
2022-07-18 08:42:12 +09:00
To generate coverage information, including HTML reports you can view in your browser,
you can use the following command:
2019-01-18 00:24:08 -08:00
2022-07-18 08:37:18 +09:00
```console
> ./phpunit --colors --coverage-text=tests/coverage.txt --coverage-html=tests/coverage/ -d memory_limit=1024m
```
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
2022-07-18 08:42:12 +09:00
The text file can be found at **tests/coverage.txt** .
2019-01-18 00:24:08 -08:00
The HTML files can be viewed by opening **tests/coverage/index.html** in your favorite browser.
## 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.