Introduction to Testing
- Posted March 24, 2017
- Addon Development
- Essentials
Related Links
Introduction
The make:addon
and make:stream
commands included in PyroCMS automatically generate a robust testing setup for your addon. This article will introduce the testing structure and how to use it.
Requirements
In order to run tests for addons in PyroCMS, you must have PHPUnit 5.7 installed globally on your system. For more information please refer to PHPUnit documentation.
Test Structure
All tests generated during addon development are located in the tests
folder of the addon. Every addon created using the make:addon
command will also generate a phpunit.xml
configuration file generated in its root directory. This file tells PHPUnit how to load the application and the test suite to run for the addon.
Factories
When using make:stream
a factory for your stream's entry model will be generated at factories/{Stream}ModelFactory.php
. Here you can define a factory for your entry model to use in tests.
Unit Tests
The make:stream
command will also generate a set of unit tests for each generated class in your stream directory. These will be generated in tests/Unit/{stream}
.
Feature Tests
The make:addon
command generates a basic feature test for your addon at tests/Feature/{addon}Test.php
.
Running Tests
To run tests for an addon, simply navigate to the addon folder and run phpunit
. This will run any tests within the /tests
folder that have test prefixed or suffixed to the method name. Alternatively, the comment syntax /** @test */
will also tell PHPUnit that the method is a test.