PHPUnit and Selenium - How to call specific functions of the test script?

3

I need to create multiple test scripts for WEB using PHPUnit and Selenium . I was successful with my initial tests. I was even able to test the login and logout of my web page effectively.

The problem is that I can only execute all the functions of the script at once, and this makes me waste a lot of time on a single test, besides preventing me from reusing the code already written for other tests.

I would like to know how to perform only a few specific functions in a test script like the one below.

<?php

class LoginTest extends PHPUnit_Extensions_Selenium2TestCase
{
 protected function setUp()
 {
    $url = EXAMPLE_URL;
    $this->setBrowser('firefox');
    $this->setBrowserUrl($url);
    $this->prepareSession();
    $this->url(EXAMPLE_URL/users/login);
    $this->currentWindow()->maximize();
    }

private function testTitle($title = null)
{
    if (!$title)
        $title = EXAMPLE_TITLE_LOGIN;
    $this->assertEquals($title,$this->title());
    }

private function testLoginFormExists()
{
    //Verifica se está na tela de login
    $this->testTitle();

    $name = $this->byName('data[User][username]');
    $passwd = $this->byName('data[User][password]');

    $this->assertEquals('',$name->value());
    $this->assertEquals('',$passwd->value());

    }

private function testLoginAction()
{
    //Verifica se o form de login existe
    $this->testLoginFormExists();

    $form = $this->byId('UserLoginForm');
    $action = $form->attribute('action');
    $this->assertEquals(EXAMPLE_URL/users/login, $action);

    $this->byName('data[User][username]')->value('admin');
    $this->byName('data[User][password]')->value('ventovento');

    $form->submit();

    //Verifica se logou corretamente
    $this->testTitle(EXAMPLE_TITLE_HOME);

    }

private function testLogoutAction()
{
    //Verifica se realiza login
    $this->testLoginAction();

    //Realiza logout
    $this->byId('img-logout')->click();

    //Verifica se realizou logout corretamente
    $this->testTitle();
    }

}

?>

To better exemplify what I'm trying to do: in the script above, I intend to call only the testLogoutAction () function. However, when I run the script, it executes all declared functions in order.

This also happens when I extend the class to another. For example, in the following test, it performs all the functions of the previous code, and then continues testing of the respective class.

<?php

require_once("LoginTest.php");

class ContasTest extends LoginTest
{ ...
    
asked by anonymous 14.04.2014 / 15:14

1 answer

2

Use the selection options on the command line:

$ phpunit -h

Test Selection Options:

  --filter <pattern>        Filter which tests to run.
  --testsuite <pattern>     Filter which testsuite to run.
  --group ...               Only runs tests from the specified group(s).
  --exclude-group ...       Exclude tests from the specified group(s).
  --list-groups             List available test groups.
  --test-suffix ...         Only search for test in files with specified
                            suffix(es). Default: Test.php,.phpt

ex:

class AssertionTest extends PHPUnit_Framework_TestCase{
    /**
     * @group grupo1
     * @author Paulo Henrique
     */
    public function testAssertTrue(){
        $this->assertTrue(2+3==5);
    }

    /**
     * @group grupo2
     */
    public function testAssertFalse(){
        $this->assertFalse(2+2 == 5);
    }
    /**
     * @group grupo2
     * @author Paulo Henrique
     */
    public function testAssertEquals(){
    $this->assertEquals( 2+5, 7, " It should be 7" ); 
    }
}

Running on the command line:

filter runs only tests that match the regex you write. ex:

$ phpunit --filter 'AssertTrue' AssertionsTest.php 
PHPUnit 3.7.22 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.25Mb

OK (1 test, 1 assertion)

group runs only the tests that have annotation @group .

$ phpunit --color --group grupo2 AssertionsTest.php 
PHPUnit 3.7.22 by Sebastian Bergmann.

..

Time: 0 seconds, Memory: 3.25Mb

OK (2 tests, 2 assertions)

In addition to the annotation @group, you can run all tests written by a single developer, if you pass the @author annotation name as a group.

$ phpunit --color --group "Paulo Henrique" AssertionsTest.php
PHPUnit 3.7.22 by Sebastian Bergmann.

..

Time: 0 seconds, Memory: 3.25Mb

OK (2 tests, 2 assertions)

You'll find more details on documentation .

    
14.04.2014 / 16:14