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

Asked

Viewed 494 times

3

I need to create several test scripts for WEB using Phpunit and Selenium. I was successful with my initial tests. I even managed to test the login and logout of my website effectively.

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

I would like to know how to, in a test script as shown below, run only some specific functions.

<?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 illustrate what I’m wanting to do: in the above script, I intend to call only the function testLogoutAction(). However, when I run the script, it executes all the functions declared in order.

This also happens when I join the class to another. For example, in the following test, it executes all functions of the previous code, and then continues the execution of the respective class tests.

<?php

require_once("LoginTest.php");

class ContasTest extends LoginTest
{ ...

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 command line:

filter runs only the tests that meet 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 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 Annotation @group, you can run all tests written by a single developer if you pass ,as a group, the name of Annotation @Author.

$ 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 will find more details on documentation.

  • Great! It worked really well this way. I appreciate it. Can tell if there is a way to use this filter through Cakephp’s test interface (Test Suite), or it is possible only from the command line itself?

  • I don’t know. I don’t use Cakephp. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.