DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:unittesting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
devel:unittesting [2023-06-19 08:52] – [Environment] tbiskupdevel:unittesting [2024-04-06 11:40] (current) – old revision restored (2024-03-09 23:41) saggi
Line 5: Line 5:
 DokuWiki's unit tests are located in the ''_test'' directory of a [[devel:git]] checkout. **They are not included in the regular releases**. DokuWiki's unit tests are located in the ''_test'' directory of a [[devel:git]] checkout. **They are not included in the regular releases**.
  
-We use the [[http://www.phpunit.de/manual/current/en/index.html|PHPUnit]] test framework for PHP. This page guides you through preparing your test environment, running tests and writing your own tests.+We use the [[http://www.phpunit.de/manual/current/en/index.html|PHPUnit]] test framework for PHP. This page guides you through preparing your test environment, running testsand writing your own tests.
  
 ===== Setup ===== ===== Setup =====
  
-Download the correct phar file for your PHP version.+Install the needed requirements using [[composer]]:
  
-<code bash>+<code>
 cd dokuwiki/_test cd dokuwiki/_test
-php fetchphpunit.php+composer install
 </code> </code>
  
Line 26: Line 26:
 <code> <code>
 cd _test/ cd _test/
-php phpunit.phar+composer run test
 </code> </code>
  
Line 34: Line 34:
  
   cd _test   cd _test
-  php phpunit.phar tests/inc/input.test.php+  composer run test tests/inc/input.test.php
  
 ==== Grouped Tests ==== ==== Grouped Tests ====
Line 43: Line 43:
   cd _test   cd _test
   # run all tests that require an internet connection   # run all tests that require an internet connection
-  php phpunit.phar --group internet+  composer run test -- --group internet
   # run all tests that don't require an internet connection     # run all tests that don't require an internet connection  
-  php phpunit.phar --exclude-group internet+  composer run test -- --exclude-group internet
 </code> </code>
  
 +Note the ''<nowiki>--</nowiki>'' separator, which composer requires before script arguments starting with double dashes (like ''<nowiki>--group</nowiki>''). ((https://getcomposer.org/doc/articles/scripts.md#running-scripts-manually))
  
 ==== Plugins ==== ==== Plugins ====
Line 55: Line 56:
 <code bash> <code bash>
 cd _test cd _test
-php phpunit.phar --group plugin_extension+composer run test -- --group plugin_extension
 </code> </code>
 +
 +Note the ''<nowiki>--</nowiki>'' separator, which composer requires before script arguments starting with double dashes (like ''<nowiki>--group</nowiki>'').
  
 Please note that some plugins may require other plugins in order to pass, either as dependencies, or because some integration code is being tested. Check if they have a ''requirements.txt'' file in their folder for the needed plugins. Please note that some plugins may require other plugins in order to pass, either as dependencies, or because some integration code is being tested. Check if they have a ''requirements.txt'' file in their folder for the needed plugins.
Line 74: Line 77:
 This means a class ''\dokuwiki\File\PageResolver'' should have a test in ''\dokuwiki\test\File\PageResolverTest'' located in ''_test/tests/File/PageResolverTest.php''. This means a class ''\dokuwiki\File\PageResolver'' should have a test in ''\dokuwiki\test\File\PageResolverTest'' located in ''_test/tests/File/PageResolverTest.php''.
  
-Each test class need to inherit from [xref>_test/core/DokuWikiTest.php|\DokuWikiTest]]. A test class can have multiple test functions, each prefixed with ''test''. Inside these functions your assertion can be written.+Each test class need to inherit from [[xref>_test/core/DokuWikiTest.php|\DokuWikiTest]]. A test class can have multiple test functions, each prefixed with ''test''. Inside these functions your assertion can be written.
  
  
Line 139: Line 142:
  
  
-Finally you have to execute the request. This can be done by calling the ''$request%%->%%execute('someurl')'' method. The return value of the execute method is the html content rendered by DokuWiki.+Finally you have to execute the request. This can be done by calling the ''$request%%->%%execute('someurl')'' method. The result of the execute method is a [[xref>TestResponse]] object, which let you access headers and the html content rendered by DokuWiki. 
 +<code php> 
 +$response = $request->execute('/doku.php'); 
 +</code>
  
-Additionally there are two methods for ''POST'' and ''GET'' calls. They may be shorter then using the setter methods.+Additionally there are two methods for ''POST'' and ''GET'' calls. They may be shorter than using the setter methods, as these combine the setter and ''execute()''.
  
 <code php> <code php>
 // a get request // a get request
-$request->get(array('id' => 'start'), '/doku.php');+$response = $request->get(['id' => 'start'], '/doku.php');
 // a post request // a post request
-$request->post(array('id' => 'start'), '/doku.php');+$response = $request->post(['id' => 'start'], '/doku.php');
 </code> </code>
  
Line 156: 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 event.+Here this is used to hook an event  followed by an ''execute()'' with the default uri.
  
 <code php> <code php>
-function testHookTriggering() {+public function testHookTriggering() 
 +{
     global $EVENT_HANDLER;     global $EVENT_HANDLER;
  
Line 187: Line 194:
 With this you can operate in a jQuery like style on the returned HTML.  With this you can operate in a jQuery like style on the returned HTML. 
  
-simple example is:+Two simple examples are:
 <code php> <code php>
-function testSimpleRun() {+public function testSimpleRun() 
 +{
     // make a request     // make a request
     $request = new TestRequest();     $request = new TestRequest();
Line 195: Line 203:
  
     // get the generator name from the meta tag.     // get the generator name from the meta tag.
-    $generator = $response->queryHTML('meta[name="generator"]')->attr('content'); +    $generator = $response->queryHTML('meta[name="generator"]') 
-    +        ->attr('content'); 
     // check the result     // check the result
     $this->assertEquals('DokuWiki', $generator);     $this->assertEquals('DokuWiki', $generator);
 +}
 +</code>
 +
 +<code php>
 +public function testPageContent()
 +{
 +    // make a request
 +    $request = new TestRequest();
 +    $response = $request->get(['id' => 'wiki:dokuwiki']);
 +
 +    // search your html
 +    $links = $response->queryHTML('p')->find('a');
 +    $countLinks = $links->count();
 +
 +    // check the result
 +    $this->assertEquals(12, $countLinks);
 } }
 </code> </code>
Line 221: Line 246:
  * @group plugins  * @group plugins
  */  */
-class helper_plugin_data_test extends DokuWikiTest {+class helper_plugin_data_test extends DokuWikiTest 
 +{
  
-    protected $pluginsEnabled = array('data', 'sqlite');+    protected $pluginsEnabled = ['data', 'sqlite'];
  
     public static function setUpBeforeClass(){     public static function setUpBeforeClass(){
         parent::setUpBeforeClass();         parent::setUpBeforeClass();
         // copy our own config files to the test directory         // copy our own config files to the test directory
-        TestUtils::rcopy(dirname(DOKU_CONF), dirname(__FILE__).'/conf');+        TestUtils::rcopy(dirname(DOKU_CONF), dirname(__FILE__) . '/conf');
     }     }
  
     public function testExample() {     public function testExample() {
-        $this->assertTrue(true,'if this fails your computer is broken');+        $this->assertTrue(true, 'if this fails your computer is broken');
     }     }
 } }
Line 240: Line 266:
  
 <code php> <code php>
-    function setUp(){+    public function setUp() 
 +    {
         global $conf;         global $conf;
  
         parent::setUp();         parent::setUp();
  
-        $conf ['plugin']['creole']['precedence'] = 'Creole'; +        $conf['plugin']['creole']['precedence'] = 'Creole'; 
-        $conf ['plugin']['creole']['linebreak' = 'Whitespace';+        $conf['plugin']['creole']['linebreak'] = 'Whitespace';
     }     }
 </code> </code>
Line 254: Line 281:
 ===== Continous Integration with Github Actions ===== ===== Continous Integration with Github Actions =====
  
-Plugin authors are encouraged to have their tests run automatically on Github Actions.The [[http://pluginwizard.dokuwiki.org/|Plugin Wizard]] and the [[plugin:dev|dev plugin]] add an appropriate yaml configuration to your plugin when you select the Unit Test option. The needed build environment is provided by the [[https://github.com/splitbrain/dokuwiki-travis|dokuwiki-travis script]].+ 
 +Plugin authors are encouraged to have their tests run automatically on Github Actions.The [[http://pluginwizard.dokuwiki.org/|Plugin Wizard]] and the [[plugin:dev|dev plugin]] add an appropriate yaml configuration to your plugin when you select the Unit Test option. 
  
 ==== Requirements ==== ==== Requirements ====
 +FIXME update
  
 If your tests require additional plugins to be installed, provide a ''requirements.txt'' file in your plugin's root directory. See for the details the  [[https://github.com/splitbrain/dokuwiki-travis#plugins-with-dependencies|README ]] in the dokuwiki-travis repository. If your tests require additional plugins to be installed, provide a ''requirements.txt'' file in your plugin's root directory. See for the details the  [[https://github.com/splitbrain/dokuwiki-travis#plugins-with-dependencies|README ]] in the dokuwiki-travis repository.
Line 263: Line 292:
  
  
-FIXME this section needs to be rewritten for Github Actions since Travis is no longer recommended.+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://qunitjs.com/|qunit]] to the automated testing. The basis for this is npm, grunt and phantomjs((This approach has been inspired by [[https://jordankasper.com/automated-javascript-tests-using-grunt-phantomjs-and-qunit/]])). It is possible to integrate javascript-tests written in [[http://qunitjs.com/|qunit]] to the automated testing. The basis for this is npm, grunt and phantomjs((This approach has been inspired by [[https://jordankasper.com/automated-javascript-tests-using-grunt-phantomjs-and-qunit/]])).
devel/unittesting.1687157523.txt.gz · Last modified: 2023-06-19 08:52 by tbiskup

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki