devel:unittesting
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
devel:unittesting [2017-09-19 15:39] – Getting DokuWiki tests running with PHPUnit 6.3 and Frusterick Manners 80.229.9.103 | devel:unittesting [2024-04-06 11:40] (current) – old revision restored (2024-03-09 23:41) saggi | ||
---|---|---|---|
Line 5: | Line 5: | ||
DokuWiki' | DokuWiki' | ||
- | We use the [[http:// | + | We use the [[http:// |
===== Setup ===== | ===== Setup ===== | ||
+ | Install the needed requirements using [[composer]]: | ||
- | ==== Requirements ==== | + | < |
- | + | cd dokuwiki/_test | |
- | * PHPUnit 3.6.10+ http:// | + | composer install |
- | * PHP 5.3+ http:// | + | </code> |
- | + | ||
- | As at Sep 2017, if using latest PHPUnit 6.3 see [[devel: | + | |
- | + | ||
- | ==== PHPUnit Installation ===== | + | |
- | + | ||
- | Download the current phar from https:// | + | |
- | $> cd dokuwiki/ | ||
- | $> wget https:// | ||
- | $> chmod u+x phpunit | ||
===== Running Tests ===== | ===== Running Tests ===== | ||
Line 32: | Line 24: | ||
Just change to the '' | Just change to the '' | ||
- | | + | < |
- | | + | cd _test/ |
+ | composer run test | ||
+ | </ | ||
- | PHPUnit will fail on some systems with a //headers already sent// error. | + | ==== Single Test Files ==== |
- | This is a known problem with PHPUnit, the error can be avoided by passing the | + | |
- | ' | + | |
- | phpunit --stderr | + | You can run a specific test file by giving it as a parameter: |
+ | |||
+ | cd _test | ||
+ | composer run test tests/ | ||
==== Grouped Tests ==== | ==== Grouped Tests ==== | ||
- | Tests can be assigned to a group by mentioning the group name in the comment header. It is also possible to assign a file to multiple groups. Here is an example: | + | You include or exclude tests by their group: |
- | < | + | < |
- | <?php | + | cd _test |
- | /** | + | # run all tests that require an internet connection |
- | * Tests to ensure creole syntax is correctly processed | + | |
- | * and creates expected XHTML output | + | # run all tests that don't require an internet connection |
- | * | + | |
- | * @group plugin_creole | + | |
- | * @group plugins | + | |
- | */ | + | |
- | class plugin_creole_test extends DokuWikiTest { | + | |
- | ... | + | |
- | } | + | |
</ | </ | ||
- | This assigns | + | Note the '' |
- | Group names can be used to only run certain test groups or to exclude them from a test run. The following example only runs the tests for all plugins by using the option '' | + | ==== Plugins ==== |
- | phpunit --stderr --group plugins | + | Plugins tests are tagged with a group named '' |
- | To exclude certain test from running use the option '' | + | <code bash> |
- | + | cd _test | |
- | | + | composer run test -- --group |
- | | + | </ |
- | Test Test | + | |
- | ==== Selected Tests ==== | + | Note the ''< |
- | You can run a single test file by providing it as an argument | + | Please note that some plugins may require other plugins in order to pass, either as dependencies, |
- | + | ||
- | phpunit --stderr tests/ | + | |
===== Writing Tests ===== | ===== Writing Tests ===== | ||
PHPUnit makes writing new tests as painless as possible. In general it means writing a fairly simple PHP class and placing it in the right directory with the right file name. Once that's done, the test can immediately be executed as explained above. | PHPUnit makes writing new tests as painless as possible. In general it means writing a fairly simple PHP class and placing it in the right directory with the right file name. Once that's done, the test can immediately be executed as explained above. | ||
- | |||
For a general overview on how to write unit tests refer to the [[http:// | For a general overview on how to write unit tests refer to the [[http:// | ||
Line 86: | Line 71: | ||
==== Naming Conventions ==== | ==== Naming Conventions ==== | ||
- | Tests are placed somewhere below the '' | + | Every new bit of code should follow [[https:// |
+ | |||
+ | In accordance ẃith PHPUnit conventions test classes should end in '' | ||
+ | |||
+ | This means a class '' | ||
+ | |||
+ | Each test class need to inherit from [[xref>_test/core/DokuWikiTest.php|\DokuWikiTest]]. A test class can have multiple test functions, each prefixed with '' | ||
- | All tests need to end in '' | + | For legacy, functional code no namespace is used. Tests are located |
- | Each test file has to contain one < | ||
==== Environment ==== | ==== Environment ==== | ||
Line 107: | Line 98: | ||
Additionally the test suite runs a '' | Additionally the test suite runs a '' | ||
- | * reload the dokuwiki | + | * reload the DokuWiki |
* reload the plugin controller and plug-ins | * reload the plugin controller and plug-ins | ||
* reload the event handler | * reload the event handler | ||
- | * ensure | + | * ensure |
- | * ensure | + | * ensure |
* reload global variables like '' | * reload global variables like '' | ||
^ //Note:// Remember to call '' | ^ //Note:// Remember to call '' | ||
- | There is no real process isolation between all tests, each test class should have a pretty clean DokuWiki environment to work on. This also includes DokuWiki' | + | Even though there is no real process isolation between all tests, each test class should have a pretty clean DokuWiki environment to work on. This also includes DokuWiki' |
+ | |||
+ | To clean up and reset the remainder of a test, the methods '' | ||
==== Integration Tests ==== | ==== Integration Tests ==== | ||
Line 140: | Line 134: | ||
The request variables are set via setter methods and look like: | The request variables are set via setter methods and look like: | ||
+ | |||
^ Variable | ^ Variable | ||
| '' | | '' | ||
Line 147: | Line 142: | ||
- | Finally you have to execute the request. This can be done by calling the '' | + | Finally you have to execute the request. This can be done by calling the '' |
+ | <code php> | ||
+ | $response = $request-> | ||
+ | </ | ||
- | Additionally there are two methods for '' | + | Additionally there are two methods for '' |
<code php> | <code php> | ||
// a get request | // a get request | ||
- | $request-> | + | $response = $request-> |
// a post request | // a post request | ||
- | $request-> | + | $response = $request-> |
</ | </ | ||
Line 164: | Line 162: | ||
As mentioned the requests are not real requests. Every call you make in your test changes the behavior of DokuWiki on the request. | As mentioned the requests are not real requests. Every call you make in your test changes the behavior of DokuWiki on the request. | ||
- | Here this is used to hook a event. | + | Here this is used to hook an event |
<code php> | <code php> | ||
- | function testHookTriggering() { | + | public |
+ | { | ||
global $EVENT_HANDLER; | global $EVENT_HANDLER; | ||
Line 188: | Line 187: | ||
</ | </ | ||
- | === Example: Using phpQuery | + | === Example: Using php-dom-wrapper |
Sometimes you want to inspect the resulting html of a request. | Sometimes you want to inspect the resulting html of a request. | ||
- | To provide a little comfort we use the [[http://code.google.com/p/phpquery/|phpQuery]] library. | + | To provide a little comfort we use the [[https://github.com/scotteh/php-dom-wrapper|php-dom-wrapper]] library. |
- | With this you can use operate in a jQuery like style on the html. | + | |
- | A simple | + | With this you can operate in a jQuery like style on the returned HTML. |
+ | |||
+ | Two simple | ||
<code php> | <code php> | ||
- | function testSimpleRun() { | + | public |
+ | { | ||
// make a request | // make a request | ||
$request = new TestRequest(); | $request = new TestRequest(); | ||
Line 202: | Line 203: | ||
// get the generator name from the meta tag. | // get the generator name from the meta tag. | ||
- | $generator = $response-> | + | $generator = $response-> |
- | + | | |
// check the result | // check the result | ||
$this-> | $this-> | ||
} | } | ||
</ | </ | ||
+ | |||
+ | <code php> | ||
+ | public function testPageContent() | ||
+ | { | ||
+ | // make a request | ||
+ | $request = new TestRequest(); | ||
+ | $response = $request-> | ||
+ | |||
+ | // search your html | ||
+ | $links = $response-> | ||
+ | $countLinks = $links-> | ||
+ | |||
+ | // check the result | ||
+ | $this-> | ||
+ | } | ||
+ | </ | ||
+ | |||
==== Plugin and Template Tests ==== | ==== Plugin and Template Tests ==== | ||
- | Sometime you need unit tests in your extensions((extensions are plug-ins and templates)). Here you can use the same classes and methods as used in the DokuWiki core. The plugin wizard has an option to add an example test in your plugin skeleton. | + | Sometime you need unit tests in your extensions((extensions are plug-ins and templates)). Here you can use the same classes and methods as used in the DokuWiki core. The plugin wizard has an option to add an example test in your plugin skeleton. Alternatively you can use the [[plugin: |
- | Just put your tests under the ''< | + | Just put your tests under the ''< |
To work, your plugin needs to be enabled during the test. This is done by putting it and all plugins it depends on in the '' | To work, your plugin needs to be enabled during the test. This is done by putting it and all plugins it depends on in the '' | ||
Line 227: | Line 246: | ||
* @group plugins | * @group plugins | ||
*/ | */ | ||
- | class helper_plugin_data_test extends DokuWikiTest { | + | class helper_plugin_data_test extends DokuWikiTest |
+ | { | ||
- | protected $pluginsEnabled = array(' | + | protected $pluginsEnabled = [' |
public static function setUpBeforeClass(){ | public static function setUpBeforeClass(){ | ||
parent:: | parent:: | ||
// copy our own config files to the test directory | // copy our own config files to the test directory | ||
- | TestUtils:: | + | TestUtils:: |
} | } | ||
public function testExample() { | public function testExample() { | ||
- | $this-> | + | $this-> |
} | } | ||
} | } | ||
Line 246: | Line 266: | ||
<code php> | <code php> | ||
- | function setUp(){ | + | |
+ | | ||
global $conf; | global $conf; | ||
parent:: | parent:: | ||
- | $conf [' | + | $conf[' |
- | $conf [' | + | $conf[' |
} | } | ||
</ | </ | ||
Line 258: | Line 279: | ||
So the correct indexing for plugins is '' | So the correct indexing for plugins is '' | ||
- | ===== Continous Integration with Travis CI ===== | + | ===== Continous Integration with Github Actions |
- | Plugin authors are encouraged to register | + | |
+ | Plugin authors are encouraged to have their tests run automatically on Github Actions.The [[http:// | ||
+ | |||
+ | ==== Requirements ==== | ||
+ | FIXME update | ||
+ | |||
+ | If your tests require additional plugins to be installed, provide a '' | ||
==== Javascript + Frontend-tests ==== | ==== Javascript + Frontend-tests ==== | ||
- | It is possible to integrate javascript-tests written in [[http:// | + | |
+ | FIXME This section needs to be rewritten for Github Actions since Travis is no longer recommended. | ||
+ | |||
+ | It is possible to integrate javascript-tests written in [[http:// | ||
<file foo .travis.yml> | <file foo .travis.yml> | ||
Line 334: | Line 364: | ||
<script src=" | <script src=" | ||
<script src=" | <script src=" | ||
+ | |||
</ | </ | ||
Line 340: | Line 371: | ||
See the [[https:// | See the [[https:// | ||
- | =====PHPUnit 6.3 & PHP 7===== | ||
- | (as at Sep 2017 & Dokuwiki version, Frusterick Manners) | ||
- | PHPUnit 6.3 has modified its class names to make use of namespaces. | ||
- | |||
- | <code diff> | ||
- | diff --git a/ | ||
- | index 89a98a45e..b24e324a1 100644 | ||
- | --- a/ | ||
- | +++ b/ | ||
- | @@ -2,7 +2,7 @@ | ||
- | /** | ||
- | * Helper class to provide basic functionality for tests | ||
- | */ | ||
- | -abstract class DokuWikiTest extends PHPUnit_Framework_TestCase { | ||
- | +abstract class DokuWikiTest extends \PHPUnit\Framework\TestCase { | ||
- | |||
- | /** | ||
- | * tests can override this | ||
- | diff --git a/ | ||
- | index d517584e1..5ae710026 100644 | ||
- | --- a/ | ||
- | +++ b/ | ||
- | @@ -98,7 +98,7 @@ class io_replaceinfile_test extends DokuWikiTest { | ||
- | /** | ||
- | * Test passing an invalid parameter. | ||
- | * | ||
- | - * @expectedException PHPUnit_Framework_Error_Warning | ||
- | + * @expectedException \PHPUnit\Framework\Error\Warning | ||
- | */ | ||
- | | ||
- | { | ||
- | </ | ||
=====See also==== | =====See also==== | ||
* [[devel: | * [[devel: | ||
* [[devel: | * [[devel: |
devel/unittesting.1505828370.txt.gz · Last modified: 2017-09-19 15:39 by 80.229.9.103