php單元測試進階(4)- 入門 - 使用參數化測試
本系列文章主要代碼與文字來源于《單元測試的藝術》,原作者:Roy Osherove。譯者:金迎。
本系列文章根據php的語法與使用習慣做了改編。所有代碼在本機測試通過。如轉載請注明出處。
觀察測試代碼,發現其實可以合并,代碼會精簡很多,只需由另一個方法提供參數。
<?php
namespace tests\index\controller;
class LogAnalyzerTest extends \think\testing\TestCase
{
/**
* @test
* @dataProvider isValidFileName_Provider
* 注意,盡量使得測試的方法名稱有意義,這非常重要,便于維護測試代碼。有規律
*/
public function isValidFileName_VariousExtensions_ChecksThem($filename, $boo)
{
$analyzer = new \app\index\controller\LogAnalyzer();
$result = $analyzer->isValidLogFileName($filename);
$this->assertEquals($result, $boo);
}
public function isValidFileName_Provider()
{
return array(
array("file_with_bad_extension.foo", false),
array("file_with_good_extension.slf", true),
array("file_with_good_extension.SLF", true),
);
}
}
cmd下,執行測試,通過。