LoginSignup
26
21

More than 5 years have passed since last update.

PHPUnitTestにおける例外処理のテスト

Posted at

こちらもシンプルですが意外と思いつかなかったので例外のテストもこちらに記載しておきます.

特定コードの例外が発生したかのテスト

HogeClassTest.php
public function testFuga(){
    try{
        HogeClass::fuga();
        // 例外が発生するはずのテストで例外が発生しなかったのでfail
        $this->fail('例外発生なし');
    }catch(Exception $e){
        // エラーコードでの比較
        $this->assertEquals(1, $e->getCode());
    }
}

例外が発生しなかったかのテスト

HogeClassTest.php
public function testFuga(){
    try{
        HogeClass::fuga();
        $this->true(true);
    }catch(Exception $e){
        $this->fail($e->getMessage());
    }
}
26
21
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
21