In order for unit testing to work with a module or a theme, you must add the module or theme's composer.json file to test/composers.json and run
composer install --lockfrom the test directory.
tests/composer.json, we are referring the file included in the dist folder of this project, which is the root directory containing the test runner phpunit.xml.requre-dev. This file may or may not have anything to do with Drupal (depending on the module's implementation), but it is required by the test runner's unit testing strategy.composer update --lock on the test runner for dependencies to be installed for the test runner.\AKlump\DrupalTest\UnitTestBase, `Each module or theme provides tests and schema files relative to it's own directory (.). Following this convention allows the test runner to auto-discover these tests. e.g.,
.
└── tests
├── jsonschema
│ └── story_resource.json
└── src
├── Client
│ ├── Service
│ │ └── EarthriseServiceClientTest.php
├── Kernel
│ ├── Service
│ │ ├── BreakpointServiceKernelTest.php
│ └── TransformKernelTest.php
├── TestBase.php
└── Unit
├── Service
│ └── EarthriseServiceUnitTest.php
└── TransformUnitTest.php
For a module to be unit testable it must have a composer.json file, which autoloads it's classes, the path of which must be added to this project's composer.json file, in the section extra.merge-plugin.require. This is how the unit tests are able to autoload classes without bootstrapping Drupal, e.g.,
"extra": {
"merge-plugin": {
"require": [
"../web/sites/all/modules/custom/gop3_core/composer.json"
]
}
}
Unit and Kernel tests do not have to test a single class, for example if you are writing a test to cover theme functions. In order to make this happen you have to do the following in your test class:
class InTheLoftThemeKernelTest extends KernelTestBase {
protected $schema = [
// By setting this to false, we indicate we are not testing a class.
'classToBeTested' => FALSE,
];
...