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?
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.
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.
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).
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)
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:
- Copy
PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php
into your webserver's document root directory.- In your webserver's
php.ini
configuration file, configurePHPUnit/Extensions/SeleniumTestCase/prepend.php
andPHPUnit/Extensions/SeleniumTestCase/append.php
as the auto_prepend_file and auto_append_file, respectively.- In your test case class that extends
PHPUnit_Extensions_SeleniumTestCase
, useprotected $coverageScriptUrl = 'http://host/phpunit_coverage.php';
to configure the URL for thephpunit_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With