问题描述
我正在尝试使用PHPUnit中的通用数据提供程序来运行一些测试.
I am trying to run some tests using a common data provider in PHPUnit.
请参阅以下测试:
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use AppBundle\Tests\DataProvider\XmlDataProvider;
class DefaultControllerTest extends WebTestCase
{
/**
* @dataProvider XmlDataProvider::xmlProvider
* @covers ReceiveController::receiveAction()
* @param string
*/
public function testReceive($xml)
{
$client = static::createClient([], ['HTTP_HOST' => 'mt.host']);
$client->request(
'POST',
'/receive',
[],
[],
[],
$xml
);
$response = $client->getResponse();
$this->assertEquals(200, $response->getStatusCode());
}
}
现在,我需要一个外部数据提供程序类:
Now I want an external data provider class:
namespace AppBundle\Tests\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class XmlDataProvider extends WebTestCase
{
/**
* @dataProvider
*/
public static function xmlProvider()
{
return array([
'xml1' => '<?xml version="1.0" encoding="UTF-8"?><myTestableXml></myTestableXml>'
]);
}
}
但是当我运行phpunit时,我得到了:
But when I run phpunit I get:
2)警告在课堂上未找到测试 "AppBundle \ Tests \ DataProvider \ XmlDataProvider".
2) Warning No tests found in class "AppBundle\Tests\DataProvider\XmlDataProvider".
我该怎么做?
composer.json自动加载代码段供参考:
composer.json autoload snippet for reference:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"Tests\\": "tests"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": [
"vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
]
},
推荐答案
您需要使用完全限定的类名来引用数据提供者:
You need to reference the data provider using the fully-qualified classname:
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DefaultControllerTest extends WebTestCase
{
/**
* @dataProvider \AppBundle\Tests\DataProvider\XmlDataProvider::xmlProvider
* @covers ReceiveController::receiveAction()
* @param string $xml
*/
public function testReceive($xml)
{
// ...
}
}
自动加载
此外,请确保调整composer.json
中的自动加载配置,以便可以自动加载数据提供程序(可能需要根据"AppBundle \ Test"命名空间映射到的目录进行调整):
Autoloading
Also, make sure to adjust your autoload configuration in composer.json
, so the data provider can be autoloaded (might need adjustment depending on which directory the ’AppBundle\Test` namespace maps to):
{
"autoload-dev": {
"psr-4": {
"AppBundle\\Tests\\": "tests/"
}
}
}
或者,由于您建议您的自动加载配置如下所示:
Alternatively, since you suggest your autoloading configuration looks like this:
{
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
您需要将呈现的测试的名称空间从AppBundle\Tests
调整为Tests\AppBundle
.
you need to adjust your namespace for the presented tests from AppBundle\Tests
to Tests\AppBundle
.
注意与您的问题无关,但就我个人而言,我认为数据提供者不需要扩展WebTestCase
.
Note Unrelated to your question, but personally, I can't see a need for the data provider to extend WebTestCase
.
有关示例,请参见:
- https://github.com/refinery29/test-util#example
- https://github.com/symfony /symfony-demo/blob/master/composer.json#L6-L16
- https://github.com/refinery29/test-util#example
- https://github.com/symfony/symfony-demo/blob/master/composer.json#L6-L16
这篇关于如何在phpunit中引用外部数据提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!