I need to run a method before each test method. Based on the documentation https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.before, I did this little test:
/**
* #before
*/
public function setupSomeFixtures()
{
echo "setupSomeFixtures\n";
}
/**
* #after
*/
public function tearDownSomeFixtures()
{
echo "tearDownSomeFixtures\n";
}
public function testTruc()
{
echo "un test\n";
}
The output:
PHPUnit 3.7.21 by Sebastian Bergmann.
Configuration read from <root>\phpunit.xml
.un test
Time: 5 seconds, Memory: 14.25Mb
OK (1 test, 0 assertions)
Am I missing something?
The Before annotation was introduced in the phpunit version 3.8 so in your 3.7 don't works.
This is my output:
PHPUnit 4.2.6 by Sebastian Bergmann.
Configuration read from /<omissis>/app/phpunit.xml.dist
The Xdebug extension is not loaded. No code coverage will be generated.
.setupSomeFixtures
tearDownSomeFixtures
.setupSomeFixtures
tearDownSomeFixtures
.setupSomeFixtures
tearDownSomeFixtures
IsetupSomeFixtures
tearDownSomeFixtures
.setupSomeFixtures
un test
tearDownSomeFixtures
Time: 451 ms, Memory: 23.00Mb
So upgrade to phpunit 3.8 or major
Related
I want to get coverage.xml for CodeCov.
PHP 8.0.2
PHPUnit 9.5.2
Xdebug 3.0.2
My class. It's very simple, just practice for code coverage
src/Car.php
<?php
/**
* This class is for car.
*/
class Car
{
public function GetColor()
{
return 'Blue';
}
}
Test file.
tests/CarTest.php
<?php
include_once __DIR__ .'/../src/Car.php';
use PHPUnit\Framework\TestCase;
/**
* #coversDefaultClass Car
*/
class CarTest extends TestCase
{
/**
* #covers Car::GetColor
*/
public function testCarGoodColor(): void
{
$car = new Car();
$this->assertSame('Blue', $car->GetColor());
}
}
When I enter
./vendor/bin/phpunit tests/ --coverage-clover ./coverage.xml
I get this message:
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
Runtime: PHP 8.0.2 with Xdebug 3.0.2
Configuration: /Applications/CI-CD/phpunit.xml
. 1 / 1 (100%)
Time: 00:00.187, Memory: 8.00 MB
OK (1 test, 1 assertion)
Generating code coverage report in Clover XML format ... PHP Fatal error: Cannot declare
class Car, because the name is already in use in /Applications/CI-CD/src/Car.php on line 8
Fatal error: Cannot declare class Car, because the name is already in use in /Applications/CI-CD/src/Car.php on line 8
How to resolve this issue with classes?
Big thanks to LazyOne
Problem was with
include_once __DIR__ .'/
../src/Car.php';
I fix it with autoload.
Also, I had index.php in src/ and I add it to ignore for code coverage.
I am trying to mock a class for phpunit. Php unit fails with the error Could not load mock ... class already exists. This is the only test I'm running, so it can't be the case that the class is mocked already.
Any suggestion would be appreciated.
Here is the error case:
namespace Tests\Feature;
use Tests\TestCase;
class DeactivateACSTest extends TestCase
{
public function testDeactivateAcs()
{
$deviceController = \Mockery::mock('overload:App\Http\Controllers\Cloud\DeviceController');
$deviceController
->shouldReceive('deactivateACS')
->andReturn('hilfehilfehilfe');
$devCon = new \App\Http\Controllers\Cloud\DeviceController();
$this->assertEquals('hilfehilfehilfe', $devCon->deactivateACS());
}
}
When running it without --code-coverage it works:
[13:10:15] vagrant#homestead [~/Code/ekp] $ phpunit --filter DeactivateACS
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.
==> Tests\Feature\DeactivateACSTest ✓
Time: 1.08 seconds, Memory: 16.00MB
OK (1 test, 3 assertions)
However, when running it with --code-coverage it fails:
[13:10:23] vagrant#homestead [~/Code/ekp] $ phpunit --coverage-html coverage --coverage-text=code_coverage.txt --filter DeactivateACSTest
PHPUnit 6.5.10 by Sebastian Bergmann and contributors.
==> Tests\Feature\DeactivateACSTest ⚈
Time: 5.79 seconds, Memory: 44.00MB
There was 1 error:
1) Tests\Feature\DeactivateACSTest::testDeactivateAcs
Mockery\Exception\RuntimeException: Could not load mock \App\Http\Controllers\Cloud\DeviceController, class already exists
/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery/Container.php:220
/home/vagrant/Code/ekp/vendor/mockery/mockery/library/Mockery.php:116
/home/vagrant/Code/ekp/tests/Feature/DeactivateACSTest.php:11
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Generating code coverage report in HTML format ... done
You should add these annotations before the functions that are mocking this class.
/**
* #runInSeparateProcess
* #preserveGlobalState disabled
*/
For reference you can check out the phpunit documentation.
https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.runInSeparateProcess
https://phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.preserveGlobalState
I ran into the same issue and fixed like this:
There was another test in my unit tests (not mockery test) which had require_once on the PHP file that had the class I was mocking. I've removed that line.
I've added processIsolation="true" in test suite
I'm working on Laravel 5.4 and trying to write the unit test to cover my function.
But I got a crazy bug. Please have look at my code and help me.
Here is my test file
class UserControllerTest extends TestCase
{
use DatabaseTransactions;
protected $isCreateToken = true;
public function testViewAccountDetailWithFieldsOK()
{
$this->get('/v1.0/me');
$this->assertResponseStatus(200);
}
public function testViewAccountDetailOK()
{
$this->get('/v1.0/me');
$this->assertResponseStatus(200);
}
}
When I try to run command: phpunit
I got this error:
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 710 ms, Memory: 14.00MB
There was 1 failure:
1) Tests\Controller\Api\UserControllerTest::testViewAccountDetailOK
Expected status code 200, got 404.
Failed asserting that 404 matches expected 200.
/Users/johnnguyen/Workspace/Laravel/mobile/vendor/laravel/browser-kit-testing/src/Concerns/MakesHttpRequests.php:744
/Users/johnnguyen/Workspace/Laravel/mobile/tests/Controller/Api/UserControllerTest.php:24
/Users/johnnguyen/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:186
/Users/johnnguyen/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:116
But when I run separately for each function:
John-Nguyen:mobile johnnguyen$ phpunit --filter=testViewAccountDetailOK
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 591 ms, Memory: 14.00MB
OK (1 test, 2 assertions)
2 functions are the same code.
but I got the issue 404: NotFoundHttpException
This is the first time I got this issue.
Can anyone help me for this bug?
Many thanks.
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.
Why PHPUnit return -1, when all test run OK.
phpunit application/models2/Sima/UserTest.php
PHPUnit 3.7.29 by Sebastian Bergmann.
Configuration read from /home/bamboo/bamboo-home/xml-data/build-dir/SIMA-MAIN-JOB1/tests/phpunit.xml
.
Time: 866 ms, Memory: 19.25Mb
OK (1 test, 2 assertions)