Actions

Regression and unit tests: Difference between revisions

From LimeSurvey Manual

Line 25: Line 25:
# Download Selenium and the geckodriver:<syntaxhighlight lang="bash">$ wget "https://selenium-release.storage.googleapis.com/3.7/selenium-server-standalone-3.7.1.jar"</syntaxhighlight><syntaxhighlight lang="bash">$ wget "https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz"</syntaxhighlight><syntaxhighlight lang="bash">$ tar xvzf geckodriver-v0.23.0-linux64.tar.gz</syntaxhighlight>
# Download Selenium and the geckodriver:<syntaxhighlight lang="bash">$ wget "https://selenium-release.storage.googleapis.com/3.7/selenium-server-standalone-3.7.1.jar"</syntaxhighlight><syntaxhighlight lang="bash">$ wget "https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz"</syntaxhighlight><syntaxhighlight lang="bash">$ tar xvzf geckodriver-v0.23.0-linux64.tar.gz</syntaxhighlight>
# Make sure you have Java installed, and start Selenium in a separate terminal:<syntaxhighlight lang="bash">$ java -jar selenium-server-standalone-3.7.1.jar -enablePassThrough false</syntaxhighlight>If you want to use the headless browser, prepend with:<syntaxhighlight lang="bash">MOZ_HEADLESS=1</syntaxhighlight>
# Make sure you have Java installed, and start Selenium in a separate terminal:<syntaxhighlight lang="bash">$ java -jar selenium-server-standalone-3.7.1.jar -enablePassThrough false</syntaxhighlight>If you want to use the headless browser, prepend with:<syntaxhighlight lang="bash">MOZ_HEADLESS=1</syntaxhighlight>
# Run tests with <syntaxhighlight lang="bash">$ DOMAIN=your_limesurvey_domain ./third_party/bin/phpunit </syntaxhighlight><code lang="html">PASSWORD</code> is the admin user password (defaults to "password")<br/><code lang="html">DOMAIN</code> is the URL to your localhost LimeSurvey installation, e.g. "localhost/limesurvey" or "limesurvey.localhost".<br/><code lang="html">DBPASSWORD</code> is the database password you would enter during installation.<br/><code lang="html">DBUSER</code> is the database user you would enter during installation.
# Run tests with <syntaxhighlight lang="bash">$ DOMAIN=<local domain> ./third_party/bin/phpunit </syntaxhighlight>where "<local domain>" is replaced with your localhost domain, e.g. "localhost" or "localhost/limesurvey".


=== Windows ===
=== Windows ===

Revision as of 14:09, 24 September 2019

  Warning : NEVER run tests on a production system. The tests WILL delete and modify the limesurvey database as well as the config.php file. Always do a backup of important data before running the tests.


 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.
 Hint: Every time you discover a new bug, you should add a regression test to make sure it does not appear again.


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

Since LImeSurvey 3.0.0, we use Selenium, headless Firefox (geckodriver) and Facebook web driver to test with the browser from PHPUnit.

Introduction

An extensive test suite will give the programmer a better sense of security, and allow him/her to make bigger re-factorizations without risking the soundness of the codebase. 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.

You can run the tests locally if you want, or you can setup a docker container that mimics the Travis CI environment. Alternatively, you can visit Travis CI after you've pushed to github to make sure your commits didn't break anything. If you think Travis is falsely reporting a bug, you should contact the person who constructed the test.

Installation

Linux

  1. A good idea is to clear the database and make a fresh install using the command-line (backup any data you want to save first):
    DROP DATABASE limesurvey; CREATE DATABASE limesurvey
    
    $ DBENGINE=INNODB php application/commands/console.php install admin password TravisLS no@email.com verbose
    
  2. To enable unit testing, you have to issue the command
    $ touch enabletests
    
    in the LimeSurvey root folder.
  3. To install Facebook web driver and PHPUnit, run
    $ composer install
    
  4. Install node (needed for EM tests). Not used right now.
  5. Download Selenium and the geckodriver:
    $ wget "https://selenium-release.storage.googleapis.com/3.7/selenium-server-standalone-3.7.1.jar"
    
    $ wget "https://github.com/mozilla/geckodriver/releases/download/v0.23.0/geckodriver-v0.23.0-linux64.tar.gz"
    
    $ tar xvzf geckodriver-v0.23.0-linux64.tar.gz
    
  6. Make sure you have Java installed, and start Selenium in a separate terminal:
    $ java -jar selenium-server-standalone-3.7.1.jar -enablePassThrough false
    
    If you want to use the headless browser, prepend with:
    MOZ_HEADLESS=1
    
  7. Run tests with
    $ DOMAIN=<local domain> ./third_party/bin/phpunit
    
    where "<local domain>" is replaced with your localhost domain, e.g. "localhost" or "localhost/limesurvey".

Windows

Install docker on windows.

Open your command prompt and enter

$ docker run --name travis-debug -dit travisci/ci-garnet:packer-1512502276-986baf0 /sbin/init

TODO.

Mac

TODO

Usage

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

$ PASSWORD=adminpassword DOMAIN=your_limesurvey_domain phpunit

in the LimeSurvey root folder to run the tests.

If LimeSurvey is installed on your web root, use "DOMAIN=localhost".

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

During testing, you might want to use --group <test group> and/or --stop-on-failure.

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

Travis

All tests are run automatically after each commit using Travis continuous integration. https://travis-ci.org/LimeSurvey/LimeSurvey/