Maybe it's just me but #depends doesn't seem to be working as I'd expect it to. My code:
<?php
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
/*
* #depends testFunc1
*/
public function testFunc2()
{
exit('TEST FUNC 2 called');
}
public function testFunc1()
{
exit('TEST FUNC 1 called');
}
}
When I do phpunit MyTest.php I'd expect to see TEST FUNC 1 called but instead I see TEST FUNC 2 called. As is it seems to just be running the tests in the order they appear in the script, regardless of the #depends attribute, which really begs the question: what does #depends actually do?
I'm running PHPUnit 5.7.20.
You need to use /** instead of /* to start a docblock.
Related
When using the #covers-annotation, it's not been taken into account when displaying the code coverage.
No matter if I just "cover" the class, or the method, or both.
Test:
/**
* #covers \App\Controller\UserEditController
*/
class UserEditWebTest extends WebTestCase
{
/**
* #covers ::edit
*/
public function testEdit():void
{
//...
$this->assertSomething();
}
}
And the tested Controller:
namespace App\Controller;
class UserEditController
{
public function edit() {
//..
}
protected function someOtherFn():void
{
//..
}
}
Now UnitTest says on code-coverate output:
$ utest --coverage-text
Classes: 23.41% (125/534)
Methods: 32.75% (1091/3331)
Lines: 31.58% (4799/15198)
App\Controller\Admin\User\UserEditController
Methods: 0.00% ( 0/ 2) Lines: 44.78% ( 30/ 67)
Q: What am I doing wrong?
Why is Methods on 0/2 ?
I also tried removing the #covers-Annotation from the test-method docblock, or adding the full namespace of the controller before the ::edit - nothing works.
I cleared the cache of the test-environment
Solution
I found out, that using the #covers-annotation is not required at all, actually. If everything is configured correctly, the tested classes, methods and lines should automatically being registered correctly.
In PhpStorm, you can run a "coverage test" which is an xdebug run that shows all code covered.
The annotation was not been taken into account, so I had to actually change my test so that more of the code is truly being tested - e.g. adding tests for each possible case inside a method.
I am trying to set test expectations on a mock object that is created in a data provider and passed to my test method. This is useful because I can reused my data provider across different test cases and have the tests define what to expect on the mock. However, phpunit marks this test as risky when the case passes, but correctly fails the test when it does not pass. Is this a known thing that cannot be done?
I am using phpunit v9.3
Here is a contrived example to show the problem:
<?php
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class Test extends TestCase
{
public function provideMock(): array
{
return [
[$this->createMock(\DateTime::class)],
];
}
/** #dataProvider provideMock */
public function testMockPasses(MockObject $mock): void
{
$mock->expects($this->once())->method('format')->with('Y-m-d');
$mock->format('Y-m-d');
}
/** #dataProvider provideMock */
public function testMockFails(MockObject $mock): void
{
$mock->expects($this->once())->method('format')->with('Y-m-d');
$mock->format('Y-m-');
}
}
I would expect this to work fine as I am just passing the object to the method - all internal php stuff.
Try running PHPUnit with --verbose key - it may tell more about the reason of marking the test as risky.
I'm just test playing with Php unit.
Here is my DependencyFailureTest class:
require_once '../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class DependencyFailureTest extends \PHPUnit\Framework\TestCase
{
public function testOne()
{
$this->assertTrue(false);
}
/**
* #depends testOne
*/
public function testTwo()
{
}
}
But on running the command phpunit --verbose DependencyFailureTest it throws
Argument #3 (No Value) of PHPUnit_TextUI_ResultPrinter::__construct() must be a value from "never", "auto" or "always".
Can anybody give an explanation for this issue?
It must be a configuration issue. I copied your code and ran it on the command line with verbose and it worked fine with version 5.4.6.
I would reinstall phpunit and ensure you have the latest version.
Also, their sample test case from their Getting Started page is:
<?php
use PHPUnit\Framework\TestCase;
class MoneyTest extends TestCase
{
// ...
public function testCanBeNegated()
{
// Arrange
$a = new Money(1);
// Act
$b = $a->negate();
// Assert
$this->assertEquals(-1, $b->getAmount());
}
// ...
}
https://phpunit.de/getting-started.html
Notice the difference in your extension usage, although I don't think it is an issue, if you use their declaration as stated, it helps to isolate the problem.
I ran into this. Was passing --colors=true, but that is incorrect.
I'm just test playing with Php unit.
Here is my DependencyFailureTest class:
require_once '../vendor/autoload.php';
use PHPUnit\Framework\TestCase;
class DependencyFailureTest extends \PHPUnit\Framework\TestCase
{
public function testOne()
{
$this->assertTrue(false);
}
/**
* #depends testOne
*/
public function testTwo()
{
}
}
But on running the command phpunit --verbose DependencyFailureTest it throws
Argument #3 (No Value) of PHPUnit_TextUI_ResultPrinter::__construct() must be a value from "never", "auto" or "always".
Can anybody give an explanation for this issue?
It must be a configuration issue. I copied your code and ran it on the command line with verbose and it worked fine with version 5.4.6.
I would reinstall phpunit and ensure you have the latest version.
Also, their sample test case from their Getting Started page is:
<?php
use PHPUnit\Framework\TestCase;
class MoneyTest extends TestCase
{
// ...
public function testCanBeNegated()
{
// Arrange
$a = new Money(1);
// Act
$b = $a->negate();
// Assert
$this->assertEquals(-1, $b->getAmount());
}
// ...
}
https://phpunit.de/getting-started.html
Notice the difference in your extension usage, although I don't think it is an issue, if you use their declaration as stated, it helps to isolate the problem.
I ran into this. Was passing --colors=true, but that is incorrect.
My setup is something like this:
class MyTest extends PHPUnit_Framework_TestCase
{
// More tests before
public function testOne()
{
// Assertions
return $value;
}
/**
* #depends testOne
*/
public function testTwo($value)
{
// Assertions
}
// More tests after
}
I'd like to focus on testTwo but when I do phpunit --filter testTwo I get message like this:
This test depends on "MyTest::testOne" to pass.
No tests executed!
My question: Is there a way to run one test with all its dependencies?
There's not out of the box way to run automatically all the dependencies. You can however put your tests in groups with the #group annotation and then run phpunit --group myGroup.
I know, this is also not much convenient, but you can try
phpunit --filter 'testOne|testTwo'
According to phpunit docs we can use regexps as filter.
Also you may consider using data provider to generate your value for the second test. But be aware that data provider method will always be executed before all tests so it may slow down the execution if it has any heavy processing.
One more approach is to create some helper method or object that will do some actual job and cache results to be used by various tests. Then you won't need to use dependencies and your data will be generated on request and cached to be shared by different tests.
class MyTest extends PHPUnit_Framework_TestCase
{
protected function _helper($someParameter) {
static $resultsCache;
if(!isset($resultsCache[$someParameter])) {
// generate your $value based on parameters
$resultsCache[$someParameter] = $value;
}
return $resultsCache[$someParameter];
}
// More tests before
public function testOne()
{
$value = $this->_helper('my parameter');
// Assertions for $value
}
/**
*
*/
public function testTwo()
{
$value = $this->_helper('my parameter');
// Get another results using $value
// Assertions
}
// More tests after
}
use regex
phpunit --filter='/testOne|testTwo/'