How to hide cakephp log in console result phpunit? - php

When running unit test by phpunit on console, it always displays the message I input in logfile:
This the sample file I will test:
Sample.php
public funtion testThis() {
CakeLog::write('error', "This is a test");
}
then when I run in console
$ ./Console/cake test app
I will get this result:
PHPUnit 3.7.38 by Sebastian Bergmann.
............This is a test..........50/50 (100%)

You need to check the configuration / configure the logger. My guess is that it doesn't have where to write, so is using STDOUT or STDERR for output.
Try to set up it with instructions from here (choose the appropriate Cake version).

Related

How to configure PHPUnit to regard dataprovider error as failure and not warning?

I run my PHP unit tests using PHPUnit. When a dataprovider fails to load, it results in a warning, instead of a failure.
Up until PHPUnit version 4, when a dataprovider fails to load, a failure is shown in the PHPUnit output.
Starting with PHPUnit 5, a warning is being issued instead. This is a problem for when running the tests using a script (for example in continuous integration) so I don't see the results.
Another difference is when running the code with PHP 5 vs PHP 7. When running the tests with PHP 5, instead of seeing the PHPUnit output I immediately get a PHP Fatal Error. With PHP 7 it only shows the failure/warning when PHPUnit gets to the failed test. This leads me to believe that this has something to do with the error_handler set by PHPUnit, that can catch the error that PHP7 throws but PHP5 doesn't.
Here is my PHP code:
class TestTest extends PHPUnit_Extensions_Database_TestCase
{
/**
* #dataProvider provider_sample
*/
public function test_sample($foo)
{
$this->assertString($foo);
}
public function provider_sample()
{
return [
[ClassName::string] // no such class, this should fail
];
}
public function getDataset()
{
return new ArrayDataSet([]);
}
}
Here are the results of running PHPUnit 4:
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
Runtime: PHP 7.2.5
Configuration: /home/some-path/phpunit.xml
Warning: The Xdebug extension is not loaded
No code coverage will be generated.
F
Time: 120 ms, Memory: 14.00MB
There was 1 failure:
1) Warning
The data provider specified for TestTest::test_sample is invalid.
Class 'ClassName' not found
FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
And here is the results of running the same code with PHPUnit 5:
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
Runtime: PHP 7.2.5
Configuration: /home/some-path/phpunit.xml
Error: No code coverage driver is available
W 1 / 1 (100%)
Time: 99 ms, Memory: 14.00MB
There was 1 warning:
1) Warning
The data provider specified for TestTest::test_sample is invalid.
Class 'ClassName' not found
WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.
The results of PHPUnit 4 is what I expect and want.
Is there a way to configure PHPUnit 5 and higher to behave the same?
I don't think you can do exactly what you're asking for, but if you want to integrate PHPUnit with a continuous integration server (for example, Jenkins), then you can spawn a JUnit-compatible report with warnings reported as errors instead and tell Jenkins to use that instead of PHPUnit report.
To do that, you will need to extend JUnit logger shipped with PHPUnit:
use PHPUnit\Util\Log\JUnit;
class MyJUnit extends Junit {
public function addWarning(Test $test, Warning $e, float $time): void {
$this->addError($test, $e, $time);
}
}
And register it as a listener in phpunit.xml:
<listeners>
<listener class="MyJUnit" file="MyJUnit.php">
<arguments>
<!--
Print the output to the file instead of stdout
which is the default.
-->
<string>junit.xml</string>
</arguments>
</listener>
</listeners>
Finally, go to Jenkins settings and add a JUnit job specifying the location of output file.
check the file name and import the
require_once 'ClassName.php';

How to run a single test case method in cakephp

I am using the cakephp test (php unit test) with xdebug to run the server side code and monitor the output from the terminal rather than using it for validation.
So, every time, i want to run some particular lib/controller/model method from the terminal and see the output, i have to comment out the other test case functions.
I know this might not be the right approach,
but i want to know, if there is a way, i can build up a wrapper around the cake test, that will take the argument of the method name which i want to run?
There is no need to write a wrapper script
To run one test method
Use the filter option:
-> phpunit --help
PHPUnit 4.4.1 by Sebastian Bergmann.
Usage: phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
...
Test Selection Options:
--filter <pattern> Filter which tests to run.
For example:
-> phpunit --debug --filter testValidationDefault tests/TestCase/Model/Table/PostsTableTest.php
PHPUnit 4.4.1 by Sebastian Bergmann.
Configuration read from /var/www/cakephp.dev/phpunit.xml.dist
Starting test 'App\Test\TestCase\Model\Table\PostsTableTest::testValidationDefault'.
I
Time: 130 ms, Memory: 9.75Mb
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 0, Incomplete: 1.
www-data # dev [ /var/www/cakephp.dev ]
->
To run only one test use the filter option in the cake command line tool:
cake test app Model/Post --filter testGetAllPostsByAuthor
Works on CakePhp 2.2

Laravel phpunit skipping custom test files

I have created a laravel test file using the command
php artisan make:test EventUserRelations
When I run phpunit, it is as if the file does not even exist. I tried to fail the test just to make sure it is being tested. Here is the code in the file:
class EventUserRelations extends TestCase
{
public function testExample()
{
$this->fail();
}
}
And here is the result.
phpunit --debug
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.
Starting test 'ExampleTest::testBasicExample'.
. 1 / 1 (100%)
Time: 119 ms, Memory: 20.75MB
In Laravel test files need to end with ..test.php. So change EventUserRelations.php to EventUserRelationsTest.php and it should work.

Laravel : phpunit cannot open file ExampleTest.php

Hi i am quit new in Laravel PHPUnit, getting the following error :
Laravel : phpunit cannot open file ExampleTest.php
I don't have idea why i am getting this error. I installed PHPUnit globally and when i run "phpunit" in terminal it runs fine. But I want to run it on specific file like :
phpunit ExampleTest
Thanks In Advance.
Make sure you are on the project root and referencing the file inside the tests folder.
Example:
phpunit tests/ExampleTest.php
I am working in Windows. Try to use the full path:
C:\Desktop\code\blog\vendor\bin>phpunit C:\Desktop\code\blog\tests\Feature\ExampleTest.php
PHPUnit 7.2.6 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 1.46 seconds, Memory: 10.00MB
OK (1 test, 1 assertion)
This started happening when I upgrade phpUnit, I had to update phpStorm to fix it.
try it like this:
vendor\bin\phpunit tests\Unit\ExampleTest.php
it will work surly
but first install phpunit globally.
Maybe you call wrong to your test.
Try like this :
php artisan test --filter YourTestClassName

Phpunit obscure error logging

I am using Phpunit 3.7, and I've been encountering following error logs when I run my tests through the command line:
Running TestSuite tests
PHPUnit 3.7.25 by Sebastian Bergmann.
Configuration read from phpunit.xml
..................E<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>Unsupported operand types</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
There is no log trace or anything that would help me nail down where the error actually is... how do I get phpunit to log this information?
Thanks
You can run the PHPUnit test in debug mode:
phpunit --debug
This will write the name of each executed test class and method in front of the test result and give you a way to find out which test fails and emits that SOAP XML failure.

Categories