Actions

Regression and unit tests: Difference between revisions

From LimeSurvey Manual

Line 39: Line 39:
</syntaxhighlight>
</syntaxhighlight>


Go to the [http://docs.seleniumhq.org/download/ Selenium webpage] and download the standalone server. Make sure you have the latest version of Java installed so you can run it. Then start the server with command:
Go to the [http://phantomjs.org/download.html phantomjs webpage] and download the Linux file. Then start the server with command:


<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
$ java -jar selenium-server-standalone-<version>.jar -enablePassThrough false
$ ./phantomjs-2.1.1-linux-x86_64/bin/phantomjs  --webdriver=127.0.0.1:4444
</syntaxhighlight>
 
Another way to run Selenium is with <code>xvfb</code>. This way, you get rid of the annoying Firefox popup at each test run.
 
<syntaxhighlight lang='bash'>
$ xvfb-run java -jar selenium-server-standalone-<version>.jar -enablePassThrough false
</syntaxhighlight>
</syntaxhighlight>



Revision as of 16:33, 2 November 2017

  Warning : Every time you discover a new bug, you should add a regression test to make sure it does not appear again.


  Warning : NEVER run tests on a production system. The tests WILL modify the database.


 Hint: Always make sure new code follows the PSR-2 standard for PHP. Use codesniffer and mess detector to catch common bugs and style fixes in your code.


Since LimeSurvey 2.65.6 you have the possibility to add unit tests to LimeSurvey.

Since LImeSurvey LS 3, we use Selenium to test with the browser from PHPUnit.

Introduction

In an ideal world, a regression test is added every time a new bug is discovered. That way you make sure you never have to fix the same bug twice.

Installation

LInux

To enable unit testing, you have to issue the command

$ touch enabletests

in the LimeSurvey root folder.

Make sure you have Composer installed.

The test system uses phpunit and Selenium, which you need to install first with the following commands:

$ composer install

This will alter the autoload files. Make sure not to commit the changes made to autoload.php and other files. They are adapted to production version of LimeSurvey, but when running composer install you are installing the development version. To adapt your local installation to production again, run:

$ composer install --no-dev --optimize-autoloader

Go to the phantomjs webpage and download the Linux file. Then start the server with command:

$ ./phantomjs-2.1.1-linux-x86_64/bin/phantomjs  --webdriver=127.0.0.1:4444

Finally, you need to download the latest geckodriver and unpack it in the LimeSurvey root folder. On my Ubuntu machine, I use geckodriver-v0.19.1-linux64.tar.gz.

Windows

TODO

Mac

TODO

Usage

After all steps for the installation are completed, you should be able to write

$ SUBDOMAIN=your_limesurvey_subdomain ./vendor/bin/phpunit

in the LimeSurvey root folder to run the tests.

If LimeSurvey is installed on your web root, just skip the SUBDOMAIN part.

This is an example of the output:

 $ phpunit
 PHPUnit 5.6.2 by Sebastian Bergmann and contributors.

 ....                                                                4 / 4 (100%)

Time: 713 ms, Memory: 14.00MB

OK (4 tests, 24 assertions)

You can get some more information if you use the switch --testdox:

$ phpunit --testdox
PHPUnit 5.6.2 by Sebastian Bergmann and contributors.

ls\tests\DateTimeForwardBack
 [x] Q 1

ls\tests\DateTime
 [x] Wrong input
 [x] Correct date format
 [x] Q 1

What can be tested?

Right now, there are tests for the expression manager, qanda and updatedb helper. The Selenium driver makes it also possible to test the full browser experience, including JavaScript.

What should be tested?

Everything that can be reported as a bug should be tested, but especially critical is of course the relation between user input and database, that is, that the correct answer is saved.

Adding a test

First of all, make sure to head over to the phpunit web site and read the introduction.

When done with that, I will assume you want to add a new test for a question type. Tests for questions are stored in folder tests/questions.

You should create a minimal survey to reproduce your bug. Export it as an lss file and put it in tests/data/surveys. Then create a class for your test. Name the file the same as the class, like "DateTimeForwardBackTest.php". The class name must end with "Test", so phpunit can find it.

Inside the class you might want to add code to import and delete the survey. I usually do this class-wide, which means that the survey gets imported when the class is loaded and then deleted when all tests in the class is done (whether failed or successfully).

The static methods setupBeforeClass and teardownAfterClass is used to setup and teardown the test fixture, respectively. You can read more about fixtures in the phpunit manual.

Let's list what we know so far:

namespace ls\tests;

use PHPUnit\Framework\TestCase;

/**
 * @since 2017-06-16
 * @author Olle Haerstedt 
*/
class DateTimeDefaultAnswerExpressionTest extends \TestCase
{
  public static function setupBeforeClass()
  {
    // Init stuff
  }

  public static function teardownAfterClass()
  {
    // Tear down what you setup before.
  }
}

To test with Selenium and the Facebook web driver, check out the wiki page on github.

TODO