将数据提供程序数据传递给安装程序[PHPUnit]
我第一次尝试让PHPUnit工作;我希望将数据传递给TestCase设置方法,以便为每个测试创建一个新实例。当我写将数据提供程序数据传递给安装程序[PHPUnit],php,testing,phpunit,Php,Testing,Phpunit,我第一次尝试让PHPUnit工作;我希望将数据传递给TestCase设置方法,以便为每个测试创建一个新实例。当我写 protected $furniture; /** * Instantiate an instance of Furniture for use with all test methods not involved with class constructor objectives * @dataProvider validConstructorArgumentProvider
protected $furniture;
/**
* Instantiate an instance of Furniture for use with all test methods not involved with class constructor objectives
* @dataProvider validConstructorArgumentProvider
*/
protected function setUp($sku, $name, $price, $dimensions) : void
{
$this->furniture = new Furniture($sku, $name, $price, $dimensions);
}
在我的课程开始时,测试运行程序会出现以下错误:
ArgumentCountError:函数WebApp\tests\FurnitureTest::setUp的参数太少,0传入/Users/ScottAnderson/Documents/Tech/commissions/shopping\u app\u test/vendor/phpunit/src/Framework/TestCase.php,第1024行正好是4
我注意到:
在调用setUpBeforeClass静态方法和第一次调用setUp方法之前,将执行所有数据提供程序。因此,您无法访问从数据提供程序中创建的任何变量。这是PHPUnit能够计算测试总数所必需的
我指定的数据提供程序如下所示:
private function validConstructorArgumentProvider()
{
return [
["TBL11FN", "Table", 60.0, [120, 50, 70]],
["CBN33FN", "Cabinet", 70.0, [60, 120, 210]],
["DSK5FN", "Desk", 55.0, [180, 70, 70]],
["PCT102FN", "Picture Frame", 13.0, [60, 40, 4]],
["LMP40FN", "Lamp Shade", 9.0, [40, 40, 25]]
];
}
class ThingTest extends TestCase
{
protected ?Thing $instance;
public function setUp()
{
parent::setUp();
$args = $this->getProvidedData();
if (count($args) !== 2)
{
$this->furniture = null;
return;
}
$this->furniture = new Furniture(...$args);
}
/**
* @dataProvider instanceProvider
*/
public function testValidInstanceCanBePrinted()
{
//testing code
}
/**
* @dataProvider constructorArgsProvider
*/
public function testCanBeInstantiatedWithValidArgs()
{
//testing code
}
public function constructorArgsProvider()
{
return [ ["these", "are" ], ["some", "args"], ["as", "example"] ]
}
…因此,可能无法将dataProvider参数提供给安装程序,但在这种情况下,我真的看不出每个测试方法的安装函数有什么意义。是否可以将参数从数据提供程序传递到“安装程序”
注:我已经看到了,但我已经按照最喜欢的答案的步骤进行了操作,但我仍然没有得到任何运气不可能将数据从数据提供商传递到安装程序。以前我写了一个完全不正确的答案,但是现在我得到了我想要的东西 仔细阅读后发现,dataProvider是在setUp函数之前执行的。我修改后的代码如下所示:
private function validConstructorArgumentProvider()
{
return [
["TBL11FN", "Table", 60.0, [120, 50, 70]],
["CBN33FN", "Cabinet", 70.0, [60, 120, 210]],
["DSK5FN", "Desk", 55.0, [180, 70, 70]],
["PCT102FN", "Picture Frame", 13.0, [60, 40, 4]],
["LMP40FN", "Lamp Shade", 9.0, [40, 40, 25]]
];
}
class ThingTest extends TestCase
{
protected ?Thing $instance;
public function setUp()
{
parent::setUp();
$args = $this->getProvidedData();
if (count($args) !== 2)
{
$this->furniture = null;
return;
}
$this->furniture = new Furniture(...$args);
}
/**
* @dataProvider instanceProvider
*/
public function testValidInstanceCanBePrinted()
{
//testing code
}
/**
* @dataProvider constructorArgsProvider
*/
public function testCanBeInstantiatedWithValidArgs()
{
//testing code
}
public function constructorArgsProvider()
{
return [ ["these", "are" ], ["some", "args"], ["as", "example"] ]
}
注意:长度测试是因为我有无效的构造函数参数,作为备用数据提供程序,在最后有一个预期的异常