phpunit中函数的运行时创建
有没有办法通过提供参数来创建testcases运行时? 测试用例的功能是相同的,只是名称和参数会有所不同 这里有一个例子phpunit中函数的运行时创建,phpunit,Phpunit,有没有办法通过提供参数来创建testcases运行时? 测试用例的功能是相同的,只是名称和参数会有所不同 这里有一个例子 public function testGetGiftsGivenVideo() { $fileName = 'get-gifts-given-video.json'; $filePath = $this->path . $fileName; $this->accessToken = $this->coreAP
public function testGetGiftsGivenVideo() {
$fileName = 'get-gifts-given-video.json';
$filePath = $this->path . $fileName;
$this->accessToken = $this->coreAPI->GetOAuthToken($this->loginFilePath);
$this->compareResults($filePath, $this->accessToken, true);
}
public function testGetGiftsReceivedAudio() {
$fileName = 'get-gifts-received-audio.json';
$filePath = $this->path . $fileName;
$this->accessToken = $this->coreAPI->GetOAuthToken($this->loginFilePath);
$this->compareResults($filePath, $this->accessToken, true);
}
public function testGetGiftsReceivedVideo() {
$fileName = 'get-gifts-received-video.json';
$filePath = $this->path . $fileName;
$this->accessToken = $this->coreAPI->GetOAuthToken($this->loginFilePath);
$this->compareResults($filePath, $this->accessToken, true);
}
现在所有这些函数都在做同样的事情 我在这里看到两种可能的解决方案: 正如axiac在评论中所建议的,您可以使用数据提供程序 数据提供程序是一种返回参数数组以使用不同参数多次调用测试方法的方法。有关更多信息,请参阅
/**
* @dataProvider provideGetGifts
* @param $filename
*/
public function testGetGifts($fileName) {
$filePath = $this->path . $fileName;
$this->accessToken = $this->coreAPI->GetOAuthToken($this->loginFilePath);
$this->compareResults($filePath, $this->accessToken, true);
}
public function provideGetGifts()
{
return array(
array('get-gifts-given-video.json'),
array('get-gifts-received-audio.json'),
array('get-gifts-received-video.json'),
);
}
第二种解决方案是,如果您希望有不同的方法名称,只需使用第二种方法,其中包括一些测试逻辑:
protected function compareAccessTokenWithFilePath($fileName) {
$filePath = $this->path . $fileName;
$this->accessToken = $this->coreAPI->GetOAuthToken($this->loginFilePath);
$this->compareResults($filePath, $this->accessToken, true);
}
public function testGetGiftsGivenVideo() {
$this->compareAccessTokenWithFilePath('get-gifts-given-video.json');
}
public function testGetGiftsReceivedAudio() {
$this->compareAccessTokenWithFilePath('get-gifts-given-audio.json');
}
public function testGetGiftsReceivedVideo() {
$this->compareAccessTokenWithFilePath('get-gifts-received-video.json');
}
就我个人而言,我更喜欢第一种解决方案。更清楚一点。这些方法的区别在哪里?或者您是否忘记更改
$fileName
的值?O是。谢谢你的更正。我刚刚编辑了这篇文章。是的,区别在于$filename。我有很多这样的测试用例,只是$filename不同。有没有办法避免这种代码重复?它看起来是一个很好的候选。我也考虑过,是的,我考虑过第二种解决方案,但它似乎不太好。至于时间,我将使用第一种解决方案。谢谢