How to output in CLI during execution of PHP Unit tests?
UPDATE
Just realized another way to do this that works much better than the --verbose
command line option:
class TestSomething extends PHPUnit_Framework_TestCase {
function testSomething() {
$myDebugVar = array(1, 2, 3);
fwrite(STDERR, print_r($myDebugVar, TRUE));
}
}
This lets you dump anything to your console at any time without all the unwanted output that comes along with the --verbose
CLI option.
As other answers have noted, it's best to test output using the built-in methods like:
$this->expectOutputString('foo');
However, sometimes it's helpful to be naughty and see one-off/temporary debugging output from within your test cases. There is no need for the var_dump
hack/workaround, though. This can easily be accomplished by setting the --verbose
command line option when running your test suite. For example:
$ phpunit --verbose -c phpunit.xml
This will display output from inside your test methods when running in the CLI environment.
See: Writing Tests for PHPUnit - Testing Output.
PHPunit result output on the CLI not showing test names
Use phpunit --testdox
On the cli this will give you a very readable testdox format and allow you to see and fix your multiple test suites easily e.g.
PHPUnit 3.7.37 by Sebastian Bergmann.
Configuration read from /home/badass-project/tests/phpunit.xml
AnalyticsViewers
[x] test getViewersForMonth throws for no valid date
[x] test getViewersForMonth limits correctly
[x] test getViewersForMonth only returns unprocessed records
[ ] test getViewersForMonth marks retrieved records as processed
[ ] test getViewersForMonth returns zero for no view data
[x] test getViewersForMonth returns valid data
Organisation
[x] test getOrganisation returns orgs
I use it in combination with the stack traces from a vanilla PHPUnit run to quickly setup.
It also has the added benefit of replacing underscores in your test function names with spaces. eg test_getViewersForMonth_returns_valid_data
becomes test getViewersForMonth returns zero for no view data
which is more human readable.
N.B. Generally speaking if you're following the PSR coding standards you should be using camelCase for method names but for unit tests methods I break this rule to reduce cognitive load during TDD development.
echo in phpunit tests
Current versions of PHPUnit >3.6.4
(and all 3.5.*
versions) will just print everything you echo
in a test case.
<?php
class OutputTestCase extends PHPUnit_Framework_TestCase {
public function testEcho() {
echo "Hi";
}
}
produces:
phpunit foo.php
PHPUnit 3.6.7 by Sebastian Bergmann.
.Hi
Time: 0 seconds, Memory: 3.25Mb
OK (1 test, 0 assertions)
So if you are on an old 3.6
version just upgrade :)
How do I test a command-line program with PHPUnit?
One way is to use the backtick operator (`) to capture the output of the program, then examine that output. This works well under Unix/Linux-style OSes, as you can also capture error outputs like STDERR. (It is more painful under Windows, but can be done (especially using Cygwin).)
For example:
public function testHelp()
{
$output = `./add-event --help 2>&1`;
$this->assertRegExp( '/^usage:/m', $output, 'no help message?' );
$this->assertRegExp( '/where:/m', $output, 'no help message?' );
$this->assertNotRegExp( '/where event types are:/m', $output, 'no help message?' );
}
You can see that both STDOUT and STDERR were captured to $output, then regex assertions were used to test whether the output resembled the correct output.
Hide output during PHPUnit test execution
You can set the setOutputCallback to a do nothing function. The effect is to suppress any output printed in the test or in the tested class.
As Example:
namespace Acme\DemoBundle\Tests;
class NoOutputTest extends \PHPUnit_Framework_TestCase {
public function testSuppressedOutput()
{
// Suppress output to console
$this->setOutputCallback(function() {});
print '*';
$this->assertFalse(false, "Don't see the *");
}
}
You can find some reference in the doc
Hope this help
PHPUnit configuration option to list executed tests
You can use the debug
flag as described in the doc in the command-line test runner:
--verbose
Output more verbose information, for instance the names of tests that
were incomplete or have been skipped.--debug
Output debug information such as the name of a test when its execution
starts.
Hope this help
PHPUnit print tests execution time
You can implement your own test runner, for example by extending PHPUnit_TextUI_TestRunner
and make it collect and print run times.
Related Topics
Storing Datetime as Utc in PHP/Mysql
Convert Lat/Longs to X/Y Co-Ordinates
Laravel 5 - Env() Always Returns Null
How to Properly Use Bearer Tokens
Is_Null($X) VS $X === Null in PHP
How to Inject a Repository into a Service in Symfony
Login Only If User Is Active Using Laravel
Memory_Get_Peak_Usage() with "Real Usage"
How to Validate Google Recaptcha on Form Submit
PHP Form Token Usage and Handling
PHP 7 Rc3: How to Install Missing MySQL Pdo
Increasing PHP Memory_Limit. at What Point Does It Become Insane
Which Compression Method to Use in PHP
Strict Standards: Only Variables Should Be Assigned by Reference PHP 5.4
Pass a Percent (%) Sign in a Url and Get Exact Value of It Using PHP