phpBB

Development Wiki

Writing Functional Tests

From phpBB Development Wiki

Revision as of 20:18, 14 October 2011 by Igorw (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

General

Functional tests test software the way a user would. They simulate a user browsing the website, but they do these steps in an automated way.

phpBB allows you to write such tests. This document will tell you how.

Base structure

Your test case will have to inherit from the phpbb_functional_test_case class. You will be able to make requests to a fresh phpBB installation.

<?php

class phpbb_my_test extends phpbb_functional_test_case
{
    public function 
bootstrap()
    {
        
// setup code
    
}

    public function 
test_index()
    {
        
// test code
    
}
}

There is a bootstrap function where you can put code that is run before the test. Here you can set up some initial state required for your test.

Then you can define your tests, just as you would normally, by prefixing methods with "test_". In this case we will be testing the index.

Goutte and assertions

The functional testing framework is built using Goutte [1], which makes HTTP requests and allows us to interact with the page. The Goutte "client" is available in your tests through $this->request($method, $path). Alternatively you can also access it directly through $this->client.

When calling request(), you will get a crawler, which allows you to fetch parts of the page using CSS selectors. Here is an example:

class phpbb_my_test extends phpbb_functional_test_case
{
    public function 
test_index()
    {
        
$crawler $this->request('GET''index.php');
        
$this->assertGreaterThan(0$crawler->filter('.topiclist')->count());
    }
}

We perform a GET request to "index.php", then apply a CSS filter, querying for the .topiclist class. Then we make sure our query matched something.

If the board's index page were broken, our test would catch this, allowing us to detect the issue and fix it. The more things you test, the more breakages you will catch early.

For more information what you can do with Goutte, check out the GitHub project, Ryan Weaver's Goutte tutorial and Symfony's testing documentation.

Running

Requirements

To run the functional tests, you will need PHP 5.3.

Configuration

Running the tests requires your board to be accessible through some web server. You will also need to supply the path to this server in the 'tests/test_config.php' file. This is as simple as defining the '$phpbb_functional_url', which contains the URL for the directory containing the board. Make sure you include the trailing slash here.

<?php
$dbms 
'mysqli';
$dbhost 'localhost';
$dbport '';
$dbname 'phpbb_test';
$dbuser 'root';
$dbpasswd '';
$phpbb_functional_url 'http://localhost/phpBB3/';

Running PHPUnit

To then run the tests, you run PHPUnit, but use the phpunit.xml.functional config file instead of the default one. Specify this through the "-c" option:

phpunit -c phpunit.xml.functional

This will change your board's config.php file, but it makes a backup at config_dev.php, so you can restore it after the test run is complete.