php单元测试进阶(4)- 入门 - 使用参数化测试

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下,执行测试,通过。

上一篇: php单元测试进阶(3)- 入门 – 添加检验
下一篇:php单元测试进阶(5)- 入门 – 异常测试

    原文作者:xieye
    原文地址: https://www.jianshu.com/p/22240ee32c1e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞