CodeIgniter4/tests/README.md

141 lines
5.2 KiB
Markdown
Raw Normal View History

# Running System Tests
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.
2022-07-18 08:46:38 +09:00
## Resources
* [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)
## Requirements
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.
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
```
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.
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
```
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
A number of the tests use a running database.
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
[Testing Your Database](https://codeigniter4.github.io/CodeIgniter4/testing/database.html) section of the documentation.
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.
2022-11-15 14:51:18 +08:00
## Groups
Each test class that we are running should belong to at least one
[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 |
## 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.
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
```
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
```
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**.
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
```
2019-10-17 19:43:37 -05:00
Individual tests can be run by including the relative path to the test file.
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
```
You can run the tests without running the live database and the live cache tests.
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
```
## Generating Code Coverage
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
./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
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
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.
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.
For instance, you might wish to exclude database tests, or automatically generate
2019-10-17 19:43:37 -05:00
HTML code coverage reports.