问题
I've read the documentation on the topic, and my code follows all requirements of a data provider implementation. First of all, here's the full code of the test just in case it's relevant.
Here's the function implementing data provider:
/**
* Test the createGroup function
*
* @return void
* @author Tomas Sandven <tomas191191@gmail.com>
*
* @dataProvider provideFileImportTests_good
**/
public function testCreateGroup($file, $groupname, $group, $mapping)
{
// Create a test group
$id = $this->odm->createGroup($groupname, $group);
// Try to load it back out
$result = R::load(OmniDataManager::TABLE_GROUP, $id);
// Check that the result is not null
$this->assertFalse(is_null($result));
return $id;
}
PHPUnit just fails:
Missing argument 1 for tests\broadnet\broadmap\OmniDataManagerTest::testCreateGroup()
I've tried killing the application (die();
) inside the data provider function, and it never happens. The data provider function is available publicly in the same class, there are no typos in the function name and the testCreateGroup
function references it in the annotations in the comment, but the data provider function is never called.
Does anybody know why?
回答1:
Finally after hours of prodding this test file, I discovered that merely defining the constructor function breaks the functionality of data providers. Good to know.
To fix it, just call the parent constructor. Here's how that looked in my case:
public function __construct()
{
// Truncate the OmniDataManager tables
R::wipe(OmniDataManager::TABLE_GROUP);
R::wipe(OmniDataManager::TABLE_DATA);
parent::__construct(); // <- Necessary
}
回答2:
If you really need it, David Harkness had the right tip. Here's the code:
public function __construct($name = NULL, array $data = array(), $dataName = '') {
$this->preSetUp();
parent::__construct($name, $data, $dataName);
}
回答3:
For me only removing the constructor has worked. Calling the parent constructor inside my class test broke the annotations as well even with the latest stable version of PHPUnit (6.0.9).
I just moved the code I had on __constructor
to the setUp
function that is called before my unit tests run.
回答4:
To emphasise the point that micro_user made, the @dataProvider
annotation must be in a docblock comment. i.e. do this:
/**
* @dataProvider myDataProvider
*
*/
public function testMyMethod(...)
{
...
}
Don't do this since it won't work:
/*
* @dataProvider myDataProvider
*
*/
public function testMyMethod(...)
{
...
}
回答5:
Make sure that dataProvider is spelled right... @dataProvidor
vs @dataProvider
In the test function which needs a data provider, a docblock is needed which contains
/**
* @dataProvider providerItCanTest
*//
回答6:
That error means that at least one of the data arrays that your data-provider method is returning is empty. For example:
public function dataProvider() {
return array(
array(1, 2, 3),
array(), // this will cause a "Missing argument 1" error
array(4, 5, 6)
);
}
Because you're generating the data arrays dynamically, you'll need to debug your data source(s) and figure out why that would be the case.
回答7:
Spent hours trying to figure out what's wrong with dataProvider annotation. It simply wasn't called at all.
In my case problem was opcache. Check php.ini to make sure opcache.save_comments is enabled:
php -r "phpinfo();" | grep opcache.save_comments
To enable it add this to php.ini (or /usr/local/php5/php.d/20-extension-opcache.ini in my case because I'm using php for osx from liip.ch):
[opcache]
opcache.save_comments=1
回答8:
Hello to anyone still getting here from google :) I'm using PHP 7.0.5 and PHPUnit 5.3.2.
As @hubro mentions - don't use __construct()
as it breaks some PHPUnit annotations. Here is a SO tread with more details.
My test's class, MyStuffTest
, extends MyFancyTestcase
which extends PHPUnit_Framework_TestCase
. MyFancyTestcase
used __construct()
and I got the same error. It should use setupBeforeClass()
instead to setup static data shared among all test cases - db connection and such, no need for __construct()
. DataProvider works now.
来源:https://stackoverflow.com/questions/10175414/phpunit-dataprovider-simply-doesnt-work