[Day 10] 简单的单元测试实作(四)-关於程序的问题,一律建议重构

为了要让程序码更简洁、更容易懂、及更容易维护,
我们今天要开始将之前的测试程序重构,(虽然好像才刚开始写)

我们原本是这样

public function test_leapyear_check_four()
{
    $response = $this->get('/getLeapYear/4');

    $this->assertSame("闰年", $response->getContent());
}

public function test_leapyear_check_2020()
{
    $response = $this->get('/getLeapYear/2020');

    $this->assertSame("闰年", $response->getContent());
}

现在我们要合并成一个 test_leapyear_check_year 函式,
其中$input是输入值,$output是输出值

public function test_leapyear_check_year($input, $output)
{
}

然後我们加上注解,其中dataProvider是PHPUnit提供的方法,
代表我们的资料来源是从input_number这个函式而来

/**
 * @param $input
 * @param $output
 * @dataProvider input_number
 */
public function test_leapyear_check_year($input, $output)
{
}

然後我们新增一个input_number函式

public function input_number()
{
}

这个函式回传测试资料,
也就是刚才那两个函式中的输入和输出资料

public function input_number()
{
    return [
        ['4', '闰年'],
        ['2020', '闰年'],
    ];
}

然後把原本的测试函式内的内容Copy过来

/**
 * @param $input
 * @param $output
 * @dataProvider input_number
 */
public function test_leapyear_check_year($input, $output)
{
    $response = $this->get('/getLeapYear/4');
    $this->assertSame("闰年", $response->getContent());
}

并且修改输入和输出资料

/**
 * @param $input
 * @param $output
 * @dataProvider input_number
 */
public function test_leapyear_check_year($input, $output)
{
    $response = $this->get("/getLeapYear/$input");
    $this->assertSame($output, $response->getContent());
}

然後删掉原本那两个程序,
现在我们的PHP档案的内容变成这样

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class MyFirstUnitTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }

    public function test_leapyear_return_200()
    {
        $response = $this->get('/getLeapYear/0');

        $response->assertStatus(200);
    }

    /**
     * @param $input
     * @param $output
     * @dataProvider input_number
     */
    public function test_leapyear_check_year($input, $output)
    {
        $response = $this->get("/getLeapYear/$input");
        $this->assertSame($output, $response->getContent());
    }

    public function input_number()
    {
        return [
            ['4', '闰年'],
            ['2020', '闰年'],
        ];
    }
}

再执行一次测试
php vendor/phpunit/phpunit/phpunit tests/Feature/MyFirstUnitTest.php
https://ithelp.ithome.com.tw/upload/images/20210911/20105694SZhI9zCYRK.png

果然是通过了,
这样我们就完成了基本的重构。


<<:  Android学习笔记02

>>:  鬼故事 - 糟了,是世界奇观

Material UI in React [ Day15 ] Navigation Stepper 步骤卡

Stepper Stepper 通过编号的步骤传达进度,它提供了类似向导的工作流程。 他除了有前面提...

[DAY 07] EC2 - 关於储存空间 EFS

回到储存空间部分, 因为有个 EFS也挺重要的 EFS (Elastic File System)...

Day24 资料的续传

由於Object一般都很大,几十GB都属於正常现象,所以上传或下载的过程中难免回遇到网路不稳的问题导...

【Day 04】 Data Analytics Pipeline 对应於 AWS 中的服务 ( 2 )

今天继续针对 Data Analytics Pipeline on AWS 中常见的 AWS 服务来...

[如何关掉TP-Link WR841N的Beacon]

目前在做学校实验,用analyzer收集wifi讯号时都会出现beacon的干扰,请问该如何关掉他或...