Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Latest commit

 

History

History
56 lines (42 loc) · 2.75 KB

CREATING.md

File metadata and controls

56 lines (42 loc) · 2.75 KB

Creating Tests

Resources

Test Cases

Every test needs a test case, or class that your tests extend. CodeIgniter 4 provides a few that you may use directly:

  • CodeIgniter\Test\CIUnitTestCase - for basic tests with no other service needs
  • CodeIgniter\Test\CIDatabaseTestCase - for tests that need database access

ModuleTests also provides some examples:

  • ModuleTests\Support\DatabaseTestCase - for database tests, pre-configured for migrations, seeds, and models from tests/_support
  • ModuleTests\Support\SessionTestCase - for session tests, pre-configured with a mock session driver

Most of the time you will want to write your own test cases to hold functions and services common to your test suites.

Tests

All tests go in the tests/ directory. ModuleTests provides two generic subfolders for you, unit and database - but feel free to make your own. Each test file is a class that extends a Test Case (see above) and contains methods for the individual tests. These method names must start with the word "test" and should have descriptive names for precisely what they are testing: testUserCanModifyFile() testOutputColorMatchesInput() testIsLoggedInFailsWithInvalidUser()

Writing tests is an art, and there are many resources available to help learn how. Review the links above and always pay attention to your Code Coverage.

Database Tests

ModuleTests provides examples for migrating, seeding, and testing against a mock or live1 database. The example files can be modified or replaced with your own:

  • tests/_support/Database/Migrations/create_test_tables.php
  • tests/_support/Database/Seeds/ExampleSeeder.php
  • tests/_support/Models/ExampleModel.php

Be sure to modify the test case (or create your own) to point to your seed and migrations and include any additional steps to be run before tests in the setUp() method:

  • tests/_support/DatabaseTestCase.php

1 Note: If you are using database tests that require a live database connection you will need to rename phpunit.xml.dist to phpunit.xml, uncomment the database configuration lines and add your connection details. Prevent phpunit.xml from being tracked in your repo by adding it to .gitignore.

Session Tests

Similar to database testing, ModuleTests provides a test case pre-configured with the mock session class to make testing sessions easier:

  • tests/_support/SessionTestCase.php