Development > Topic

Automatic testing/#7276

Dec 4, 2008 / pixtur
Apr 22, 2012 / pixtur
 

Attached files

 

Automatic testing in Streberπ

We use simpletest's web_tester module for testing. This emulates a browser and simulates real clickflows and checks the results. You can login, change a users password, logout and test if the new password is working. So this is more integration testing than unit testing. This is nice for:
  1. Tests are very easy to write.
  2. Since pixtur added sql fixtures you can test complex setup involving several users and projects.
  3. You can easily valid html structure, user data escaping and much more.
It fails on:
  1. Javascript
  2. Internal structures

Running the testsπ

The tests are executed when requesting `http://localhost/streber/tests/testsuite_pages.php`. There are about 600 tests which take about 30 seconds on pixtur's notebook. Right now the tests uses database snapshots as sql-dumps as fixtures. These dumps are located at tests/fixtures/*.sql. The testing sql tables get the prefix test_ so your userdata will not be resetted all the time. Initialization is done with the new class "TestEnvironment"

Creating new fixturesπ

Creating a new fixture is done with install/db_backup.php. It writes a dump of all existing tables, so you have to remove the test_*-tables before calling this.

Updating fixtures databaseπ

The fixtures need to be updated every time, the database scheme (e.I. the database version) changes. If you don't do this, you will get errors like this:

test_fixtures_outofdate_bug.png

Implementation detailsπ

Test and development databaseπ

To distinguish between the two databases, we overwrite the USER_AGENT before running the web_tester:

from tests/test_item_visibility.php

    $this->addHeader('USER_AGENT: streber_unit_tester');  # will result in HTTP_USER_AGENT 

In index.php we check for this user_agent and add the prefix test_ to DB_TABLE_PREFIX. Before each test_case the test-database is completely recreated. After running all tests, the tables with tests_* will be dropped from the database by executing a long sql-statement:

from tests/testsuite_pages

TestEnvironment::prepare('fixtures/remove_tables.sql');

Todoπ

Although the current setting already provides a lot of features, it's still pretty hackish. Following this should be adjusted:
  1. Streber should detect the web_tester user_agent and automatically use the test_-tables.
  2. TestEnvironment should provide a method the clean up test tables.
  3. Setup basic project environment as fixture for testing.

Things that urgently need to be testedπ

  1. Form submitting esp for complex things like Tasks and People.
  2. Correct escaping of all user data.
  3. Item visibility in project environments.



also see:

2 Comments

pixtur:partly done

7 years ago

The original version of the unit-tests is already working. Now I have to figure out a method to overwrite the db-settings and provide fixture db-setups.


pixtur:

3 years ago

Tests do not work with anonymous user enabled.