Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run HTML test suite from PHPUnit?

I want to measure code coverage of an HTML test suite for selenium. Therefore I want to use PHPUnit in order to execute the suite, because PHPUnit has nice support for code coverage analysis.

Therefore: Is it possible to run an HTML test suite from PHPUnit?

like image 523
Chris Avatar asked Sep 06 '12 08:09

Chris


People also ask

How do I run a test in PHPUnit?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.

What is the PHPUnit XML configuration file used to organize tests?

The beStrictAboutChangesToGlobalState Attribute This attribute configures whether PHPUnit should mark a test as risky when global state is manipulated by the code under test (or the test code).


1 Answers

Short answer

Running individual HTML test files is not a problem, running HTML suites files however does not seem to work. As long as you put all the HTML test files from a suit in a directory by themselves you can just run runSelenese($folderName)

Long answer

I had no idea that running Selenium HTML files directly was even possible until I did some more digging.

What I used to do is convert/export them first with the Selenium IDE PHP Formatter plugin for Firefox.

Apparently this is not necessary. All the way at the bottom of Chapter 17. PHPUnit and Selenium the manual states:

Using the runSelenese($filename) method, you can also run a Selenium test from its Selenese/HTML specification. Furthermore, using the static attribute $seleneseDirectory, you can automatically create test objects from a directory that contains Selenese/HTML files. The specified directory is recursively searched for .htm files that are expected to contain Selenese/HTML. Example 17.5 shows an example.

Example 17.5: Use a directory of Selenese/HTML files as tests

<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class SeleneseTests extends PHPUnit_Extensions_SeleniumTestCase
{
    public static $seleneseDirectory = '/path/to/files';
}
?>

Once you have your Tests in PHPUnit you can use it to get code coverage from the Selenium Server.

Again, from the manual:

PHPUnit_Extensions_SeleniumTestCase can collect code coverage information for tests run through Selenium:

  1. Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
  2. In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
  3. In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use
    protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
    to configure the URL for the phpunit_coverage.php script.

As this can be a bit of a hassle to set up, I've always just used the Page Coverage plugin to get an insight into HTML page coverage.

like image 66
Potherca Avatar answered Sep 21 '22 20:09

Potherca