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?
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:
Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
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.
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.
Unit testing PHP HTML output is a great idea. More people should do it, but turns out it has not been that simple in the past.
I had the same problem and can up with this: https://packagist.org/packages/phpfui/html-unit-tester It can validate HTML and CSS from a URL, file, or string snippet.
To do what you want, you should set up a local web server that your tests can hit for your application. You probably have this already, but make sure it works for your local env and any server you run tests on.
Then you can use the assertValidUrl method with a path to your local server. Since you are not comparing to a template, but just testing that the HTML is valid, you can quickly find recently introduced HTML errors.
Hope this helps.
Related
I'm looking through the documentation, but I'm not seeing any option to change the working directory used when running tests.
I'm using PhpUnit as it's included in Laravel. I want to be able to run vendor/bin/phpunit from my project's root directory, and have it run using the /public directory as the working directory.
I tried running ../vendor/bin/phpunit from the /public, but since the phpunit.xml file isn't in the public directory and I don't want to specify my config file path every time, that won't work.
Is there something I can add to my phpunit.xml file to tell it to run tests using the /public directory as the "cwd" (current working directory)?
Based on the feedback I received in the comments and the documentation, I determined the following:
It's probably not possible to change the cwd that phpunit uses by default (well, it's possible in PhpStorm, but not the command line without writing some kind of wrapper script)
Code that depends on being run from a specific directory is not a good idea.
What I had was some code in one of my classes like this:
$var = file_get_contents("../some_file.json");
This works fine -- until you try to add unit tests. The web server runs using the /public directory as the cwd, while phpunit will run using the root directory.
Rather than trying to force phpunit to always use a particular cwd (/public), I decided it's probably best to remove relative paths from the code that rely on a consistent cwd. So the line above becomes:
$var = file_get_contents(base_path("some_file.json"));
I didn't want to change production code that was already working just to get some tests in place, but this change seemed insignificant enough. (and it's an improvement anyway)
Well, you'd have to do the actual chdir in PHP, but you can define a bootstrap script in the XML (<phpunit bootstrap="./bootstrap.php">) and have that change the working directory.
Alternatively, you can put a setUpBeforeClass function into your test class that changes the working directory.
I want to execute different dump file for different codeception tests. Right now Db dump file is being executed from shell_exec command in _before method of AcceptanceHelper, that executes before each and every acceptance test. Something like suggested here. There are alot of tests in the app. So, the flow is as follows
- tests/acceptance/application/<contains alot of tests related to application>
- tests/acceptance/location/<contains alot of tests related to location>
Both test directories /application/ and /location/ uses same AcceptanceHelper. So, what i want is a different executable dump file for all of the tests inside /application/ directory than that of /location/ tests.
Think of something like Get current running test name. Let's say application_dump.sql for all tests inside /application/ directory and location_dump.sql for all tests inside /location/ directory.
P.S: Using a different suite for application and location is ideally not what i am looking for.
Just to help some one out there. As there was no proper way to achieve this as Get current running test name seems to be still in development.
So, this is how i managed to solve the issue. I have moved shell_exec commands out of _before method of AcceptanceHelper and created a new public method inside AcceptanceHelper which can be accessed via actor class $I in each and every acceptance test like so
$I->executeDbDump('application_dump.sql');
$I->executeDbDump('location_dump.sql');
Only drawback using this approach is i have to execute respective executeDbDump function before each and every test manually. But still seems to be best approach for the issue now.
I've been following the Zend Framework 2 tutorial on unit testing, but once I reached the section 'A failing test case' my test actually passed instead of failing.
I've changed the Album Controller to become a definite failing test e.g. $this->assertTrue(false) and it still passes.
However, when I edit the phpunit.xml file and change <directory>./AlbumTest</directory> to become <directory>./AlbumTest/Controller/AlbumController.php</directory> the test fails as desired.
Does anyone know how to solve this?
PHPUnit looks for files that end with Test.php. Since your file is named AlbumController.php, it's being ignored. When you give PHPUnit the full path to the file, then it's reading it. So renaming the file to AlbumControllerTest.php should fix your problem.
I have set up PhpStorm 5 with PHPUnit, and I'm curious if PhpStorm might have some functionality that will automatically run a unit test when saving a file. Like watchr and guard. I have tried search our beloved www and the PhpStorm docs, but haven't been able to find a solution for it.
As of version 6, PHPStorm has "File Watchers"
Open your project preferences.
Select File Watchers from the left hand list of options.
Click the + symbol at the bottom of the empty right hand panel.
Select <custom>
You will have to set up a command line for PHPUnit, it wont be the integrated testing, but you can have errors output to the console (which is good a good start!)
Various macro options are available to you, so you can include (for example) $FileNameWithoutExtension$Test.php in the arguments passed to your command line.
I personally had to set up two watchers. The first detected modifications to project files, and the second detected changes to test files (the second did not append Test.php to the filename) I also created a new project scope to exclude the tests directories from the first watcher.
You may also want to turn off immediate synchronisation, as this causes PHPUnit to run when PHPStorm auto-saves files.
My other settings are like:
File Type: PHP files (PHP)
Scope: Project excluding tests
Program: /path/to/php
Arguments: /path/to/phpunit --configuration /path/to/phpunit.xml.dist /path/to/tests/$FileNameWithoutExtension$Test.php
Working directory: $FileDir$
Output paths: $FileDir$
No output filters set, syntax error checks enabled, and console showing errors.
PHPUnit watcher named as hot phpunit runner
https://github.com/slavahatnuke/hot-phpunit-runner
You can also have a look at TDDRunner
It is console tool that execute PHPUnit autmaticly on file changes. You can also configure PHPUnit by excuting only one file ot whatever.
/usr/bin/tddrunner --group=test
There's a a German article providing further detail.
In 2017 Jetbrains release a feature that allow auto-run for tests.
It's located in the Run console, therefore it's linked to the run settings, easing the setup of this autorun.
See https://blog.jetbrains.com/phpstorm/2017/07/autorun-phpunit-tests-in-phpstorm-2017-2/
I'm migrating a php application from procedural to oop.
I use a DEBUG constant to activate errors and warnings output (in fact, I have thee, every one makes the output more verbose.
But I can't find a way to access those constants from within a method.
The constants are defined before autoload in a separate file.
In the utility file I have
define('DEBUG', TRUE);
And inside a given method I tried to
if(!defined('DEBUG')) define('DEBUG', FALSE);
But I always end up with DEBUG=FALSE.
What am I doing wrong? I'm a total noob to oop, so be gentle please :-)
Clarification
Every class has his own file.
In any given script, the first thing I do is to include the utility file. The utility file is the one who defines DEBUG and has the _autoload function.
script_file.php
includes utility_file.php
defines DEBUG
has _autoload function
according to this, you should access DEBUG (no prepending $) in your code directly. are you including or requiring your utility file in the same file that has the function you're talking about? i don't think this is an OOP problem
darkphoenix was right, This wasn't an OOP problem. This was a NetBeans problem.
I'm using NetBeand and uploading the files to a remote server upon save. I've set the DEBUG constant to TRUE in the utility file and hit save on NetBeans, the save process (apparently) went without problems (no warnings or anything).
Big was my surprise when latter I logged in via SSH did a cat on the file. The file was never saved to the server. My local copy has my last edit, but the remote one doesn't...
Moral of the story: I hate you NetBeans