Medium 清新閱讀版:連結
今天要來為大家介紹幾個,在撰寫測試程式碼時可以利用的特殊函數。
setUp():我們可以在這個函數中,撰寫想要在每個測試案例函數執行前預執行的邏輯。
tearDown():我們可以在這個函數中,撰寫想要在每個測試案例函數執行後預執行的邏輯。
範例:
<?php
namespace Tests\Unit;
use App\Services\TestService;
use PHPUnit\Framework\TestCase;
class TestServiceTest extends TestCase
{
private $service;
public function setUp(): void
{
$this->service = app(TestService::class);
parent::setUp();
}
public function testCanCalcuateBmi()
{
$bmiActual = $this->service->calculateBmi(1.6, 64.0);
$bmiExpected = 64.0/(1.6*1.6);
$this->assertEquals($bmiExpected, $bmiActual);
}
public function tearDown(): void
{
parent::tearDown();
$this->service = null;
}
}
在以上程式碼中, setUp()會在每個測試案例函數執行前,先初始化 $this→service;tearDown() 則會在每個測試案例函數執行後,清空 $this→service。
setUpBeforeClass():與 setUp() 類似,但只會在每個測試類別的第1個測試案例函數執行前執行。
tearDownAfterClass():與 tearDonw() 類似,但只會在每個測試類別的第1個測試案例函數執行前執行。
範例:
<?php
namespace Tests\Unit;
use App\Services\TestService;
use PHPUnit\Framework\TestCase;
class TestServiceTest extends TestCase
{
private $service;
private static $flag;
public static function setUpBeforeClass(): void
{
self::$flag = true;
parent::setUpBeforeClass();
}
public function setUp(): void
{
$this->service = app(TestService::class);
parent::setUp();
}
public function testCanThrowExceptionWhenInvaliHeight()
{
$this->expectException(Exception::class);
$this->service->calculateBmi(0.0, 1.0);
}
public function testCanThrowExceptionWhenInvaliWeight()
{
$this->expectException(Exception::class);
$this->service->calculateBmi(1.0, 0.0);
}
public function testCanThrowExceptionWithMessageWhenInvaliData()
{
$this->expectExceptionMessage('Invalid');
$this->service->calculateBmi(0.0, 1.0);
}
public function testCanThrowExceptionWithMessageRegexMatcchWhenInvaliData()
{
$this->expectExceptionMessageMatches('/Invalid/');
$this->service->calculateBmi(0.0, 1.0);
}
public function testCanThrowExceptionWithCodeWhenInvaliData()
{
$this->expectExceptionCode(1);
$this->service->calculateBmi(0.0, 1.0);
}
public function tearDown(): void
{
parent::tearDownAfterClass();
$this->service = null;
}
public static function tearDownAfterClass(): void
{
self::$flag = false;
parent::tearDownAfterClass();
}
}
特別要注意的是,setUpBeforeClass() 及 tearDownAfterClass() 都只能宣告為靜態函數。
來到今天的重頭戲了,這個功能與前一天介紹的 @testWith 很像,話不多說,直接來看範例吧!
範例
<?php
namespace Tests\Unit;
use App\Services\TestService;
use Exception;
use PHPUnit\Framework\TestCase;
class TestServiceTest extends TestCase
{
/**
* @dataProvider bmiProvider
*/
public function testCanThrowException(float $height, float $weight)
{
$service = app(TestService::class);
$this->expectException(Exception::class);
$service->calculateBmi($height, $weight);
}
public function bmiProvider()
{
return [
[1.0, 0.0],
[0.0, 1.0],
[0.0, 0.0],
];
}
}
在以上程式碼中,我們建立了, 1個 Data Provider 函數 bmiProvider(),以及1個測試案例函數 testCanThrowException(),並且註記使用 Data Provider 函數 bmiProvider()。在此測試類別中,testCanThrowException()實際上會執行3次,分別是:
testCanThrowException(1.0, 0.0)
testCanThrowException(0.0, 1.0)
testCanThrowException(0.0, 0.0)
其實它的使用方式與前一天的 @testWith 極為類似,但用起來會更直覺,也比較像在撰寫測試程式。
以上就是今天的介紹了!
明天讓我們來研究 Seeder a.k.a 播種器吧。