I have just started using PHPUnit
. So far, everything is going perfect except the Data Provider
issue.
The problem is when I run test, it passes. But if I run it once again, it fails with the following error:
ArgumentCountError: Too few arguments to function ValidationTest::testValidateType(), 0 passed and at least 3 expected
If I make any changes to the data provider function (i.e. change the data to be returned, provider function name etc.) and re-run, it passes for once and fails with the above error for all consecutive test runs.
I am using the latest version of PHPUnit (updated an hour ago). Unfortunately I did not find any specific solution anywhere. So, I am really worried, am I doing very silly mistake(s)?
Not sure, but does PHPUnit use any caching mechanism to cache provider data? If yes, then is there any way to clean it (maybe using setUp
or tearDown
)?
Look forward to seeing expert opinions. Thanks in advance. :-)
The following code work once (pass once):
/**
* @covers Validation
* @coversDefaultClass Validation
*/
class ValidationTest extends TestCase {
protected $validation;
protected function setUp() {
$this->validation = new Validation();
}
/**
* @covers ::validateType
* @dataProvider validateTypeProdiver
*/
public function testValidateType($assertion, $argument, $type) {
$result = $this->validation->validateType($argument, $type);
switch ($assertion) {
case 'True':
$this->assertTrue($result);
break;
}
}
public function validateTypeProdiver() {
return [
['True', 'file.txt', 'str']
];
}
}
The following code always work (passes everytime):
/**
* @covers Validation
* @coversDefaultClass Validation
*/
class ValidationTest extends TestCase {
protected $validation;
protected function setUp() {
$this->validation = new Validation();
}
/**
* @covers ::validateType
*/
public function testValidateType() {
foreach ($this->validateTypeProdiver() as $args) {
$result = call_user_func_array([$this->validation, 'validateType'], array_slice($args, 1));
switch ($args[0]) {
case 'True':
$this->assertTrue($result);
break;
}
}
}
public function validateTypeProdiver() {
return [
['True', 'file.txt', 'str']
];
}
}
Here is the configuration:
<phpunit bootstrap="vendor/autoload.php" colors="true" verbose="true" beStrictAboutTestsThatDoNotTestAnything="true">
<testsuites>
<testsuite name="ProjectX Unit Test">
<directory suffix="Test.php">tests/ProjectX</directory>
<exclude>tests/ProjectX/assets</exclude>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/ProjectX</directory>
<exclude>src/ProjectX/nodejs-commands</exclude>
</whitelist>
</filter>
<php>
<ini name="error_reporting" value="-1" />
</php>
</phpunit>
0 passed
, means it does not pass any of the provided arguments. :-(Data Provider
. Otherwise they work fine. :-(Data Providers
documentation: "All data providers are executed before both the call to thesetUpBeforeClass
static method and the first call to thesetUp
method. Because of that you can't access any variables you create there from within a data provider. This is required in order for PHPUnit to be able to compute the total number of tests." Any hints?