Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

matlab.unittest.constraints.Throws 클래스

네임스페이스: matlab.unittest.constraints
슈퍼클래스: matlab.unittest.constraints.Constraint

함수가 지정된 오류를 발생시키는지 테스트

설명

matlab.unittest.constraints.Throws 클래스는 함수 핸들이 지정된 오류를 발생시키는지 테스트하는 제약 조건을 제공합니다.

matlab.unittest.constraints.Throws 클래스는 handle 클래스입니다.

생성

설명

예제

c = matlab.unittest.constraints.Throws(identifier)는 함수 핸들이 identifier로 지정된 오류를 발생시키는지 테스트하는 제약 조건을 만듭니다. 테스트 프레임워크가 함수 핸들을 호출했을 때 실제 값이 지정된 오류를 발생시키는 함수 핸들이면 이 제약 조건이 충족됩니다.

예제

c = matlab.unittest.constraints.Throws(identifier,Name,Value)는 하나 이상의 이름-값 인수를 사용하여 옵션을 지정합니다. 예를 들어, c = matlab.unittest.constraints.Throws(identifier,"WhenNargoutIs",2)는 함수 핸들이 두 개의 출력 인수와 함께 호출될 때 지정된 오류를 발생시키는지 테스트하는 제약 조건을 만듭니다.

입력 인수

모두 확장

오류 ID로, string형 스칼라, 문자형 벡터 또는 meta.class 인스턴스로 지정됩니다.

identifiermeta.class 인스턴스인 경우, 발생한 오류가 지정된 클래스 또는 그 서브클래스 중 하나의 인스턴스이면 제약 조건이 충족됩니다

이 인수는 ExpectedException 속성을 설정합니다.

예: "MATLAB:UndefinedFunction"

예: ?MException

이름-값 인수

선택적 인수 쌍을 Name1=Value1,...,NameN=ValueN으로 지정합니다. 여기서 Name은 인수 이름이고 Value는 대응값입니다. 이름-값 인수는 다른 인수 뒤에 와야 하지만, 인수 쌍의 순서는 상관없습니다.

예: c = matlab.unittest.constraints.Throws(identifier,WhenNargoutIs=2)

R2021a 이전 릴리스에서는 쉼표를 사용하여 각 이름과 값을 구분하고 Name을 따옴표로 묶으십시오.

예: c = matlab.unittest.constraints.Throws(identifier,"WhenNargoutIs",2)

함수 핸들을 호출할 때 제약 조건이 요구하는 출력값 개수로, 음이 아닌 정수 스칼라로 지정됩니다.

이 인수는 Nargout 속성을 설정합니다.

데이터형: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

예상 오류의 필수 원인으로, string형 배열, 문자형 벡터로 구성된 셀형 배열 또는 meta.class 인스턴스의 배열로 지정됩니다. 함수 핸들이 오류를 발생시킬 때 지정된 원인이 하나라도 누락되면 제약 조건이 충족되지 않습니다.

이 인수는 RequiredCauses 속성을 설정합니다.

일련의 필수 원인만 고려할지 여부로, 숫자형 또는 논리값 0(false) 또는 1(true)로 지정됩니다. 값이 true인 경우, 예상 오류에 CausedBy 이름-값 인수로 지정되지 않은 원인이 포함되어 있으면 제약 조건이 충족되지 않습니다. 기본적으로 이 제약 조건은 추가 원인을 무시합니다.

이 인수는 RespectSet 속성을 설정합니다.

속성

모두 확장

예상 오류 ID로, 문자형 벡터 또는 meta.class 인스턴스로 반환됩니다.

이 속성은 identifier 입력 인수에 의해 설정됩니다.

특성:

GetAccess
public
SetAccess
immutable

함수 핸들을 호출할 때 제약 조건이 요구하는 출력값 개수로, 음이 아닌 정수 스칼라로 반환됩니다.

이 속성은 WhenNargoutIs 이름-값 인수에 의해 설정됩니다.

특성:

GetAccess
public
SetAccess
private

예상 오류의 필수 원인으로, 문자형 벡터로 구성된 셀형 배열 또는 meta.class 인스턴스의 배열로 반환됩니다.

이 속성은 CausedBy 이름-값 인수에 의해 설정됩니다.

특성:

GetAccess
public
SetAccess
private

일련의 필수 원인만 고려할지 여부로, logical 데이터형의 0 또는 1로 반환됩니다.

이 속성은 RespectingSet 이름-값 인수에 의해 설정됩니다.

특성:

GetAccess
public
SetAccess
private

예제

모두 축소

실제 값이 지정된 오류를 발생시키는 함수 핸들인지 테스트합니다.

먼저 이 예제에서 사용되는 클래스를 가져옵니다.

import matlab.unittest.TestCase
import matlab.unittest.constraints.Throws

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = TestCase.forInteractiveUse;

error 함수가 예상 ID에 해당하는 오류를 발생시키는지 확인합니다.

testCase.verifyThat(@() error("SOME:error:id","Error!"), ...
    Throws("SOME:error:id"))
Verification passed.

제약 조건을 string형 대신 meta.class 인스턴스로 전달하여 다시 테스트합니다. 테스트가 통과합니다.

testCase.verifyThat(@() error("SOME:error:id","Error!"), ...
    Throws(?MException))
Verification passed.

실제 오류 ID와 예상 오류 ID가 일치하지 않을 경우 테스트가 실패하는지 확인합니다.

testCase.verifyThat(@() error("SOME:error:id","Error!"), ...
    Throws("OTHER:error:id"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Throws failed.
    --> The function threw the wrong exception.
        
        Actual Exception:
            'SOME:error:id'
        Expected Exception:
            'OTHER:error:id'
    --> Actual Error Report:
            Error using @()error("SOME:error:id","Error!")
            Error!
    
    Evaluated Function:
      function_handle with value:
    
        @()error("SOME:error:id","Error!")

rand 함수를 테스트합니다. rand가 오류를 발생시키지 않으므로 이 테스트는 실패합니다.

testCase.verifyThat(@rand,Throws(?MException))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Throws failed.
    --> The function did not throw any exception.
        
        Expected Exception:
            ?MException
    
    Evaluated Function:
      function_handle with value:
    
        @rand

disp 함수는 어떤 출력값도 반환하지 않습니다. disp를 하나의 출력 인수와 함께 호출할 경우 지정된 오류가 발생하는지 확인합니다.

testCase.verifyThat(@() disp("Hello World!"), ...
    Throws("MATLAB:maxlhs","WhenNargoutIs",1))
Verification passed.

실제 값이 함수 핸들이 아닌 경우 Throws 제약 조건이 충족되지 않는지 확인합니다.

testCase.verifyThat(5,Throws("SOME:error:id"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Throws failed.
    --> The value must be an instance of the expected type.
        
        Actual Class:
            double
        Expected Type:
            function_handle
    
    Actual Value:
         5

Throws 제약 조건을 사용하여 오류의 원인이 있는지 테스트합니다.

먼저 이 예제에서 사용되는 클래스를 가져옵니다.

import matlab.unittest.TestCase
import matlab.unittest.constraints.Throws

대화형 방식 테스트를 위한 테스트 케이스를 생성합니다.

testCase = TestCase.forInteractiveUse;

오류의 두 가지 원인 cause1cause2와 함께 MException 객체 me를 생성합니다.

me = MException("TOP:id","Top-Level Error!");
cause1 = MException("CAUSE1:id1","First Cause of Error!");
cause2 = MException("CAUSE2:id2","Second Cause of Error!");
me  = me.addCause(cause1);
me  = me.addCause(cause2);

프레임워크가 오류를 발생시킬 때 cause1me의 원인인지 확인합니다.

testCase.verifyThat(@() me.throw,Throws("TOP:id","CausedBy","CAUSE1:id1"))
Verification passed.

원인을 meta.class 인스턴스로 지정하여 다시 테스트합니다. 테스트가 통과합니다. me에 원인이 없으면 테스트는 실패합니다.

testCase.verifyThat(@() me.throw,Throws("TOP:id","CausedBy",?MException))
Verification passed.

오류에 cause1 이외의 다른 원인이 없는지 테스트합니다. cause2도 예상 오류의 원인이므로 테스트가 실패합니다.

testCase.verifyThat(@() me.throw, ...
    Throws("TOP:id","CausedBy","CAUSE1:id1","RespectingSet",true))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Throws failed.
    --> The following causes were unexpectedly found in the exception tree:
        --> 'CAUSE2:id2'
    --> Actual Error Report:
            Error using @()me.throw
            Top Level Error!
            
            Caused by:
                First Cause of Error!
                Second Cause of Error!
    
    Actual Error Structure:
        ?MException 'TOP:id'
        --> ?MException 'CAUSE1:id1'
        --> ?MException 'CAUSE2:id2'
    
    Evaluated Function:
      function_handle with value:
    
        @()me.throw

지정된 원인이 오류에 속하지 않는 경우 제약 조건이 충족되지 않는지 확인합니다.

testCase.verifyThat(@() error("TOP:id","Top-Level Error!"), ...
    Throws("TOP:id","CausedBy","CAUSE1:id1"))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    Throws failed.
    --> The following causes were not found in the exception tree:
        --> 'CAUSE1:id1'
    --> Actual Error Report:
            Error using @()error("TOP:id","Top-Level Error!")
            Top-Level Error!
    
    Actual Error Structure:
        ?MException 'TOP:id'
    
    Evaluated Function:
      function_handle with value:
    
        @()error("TOP:id","Top-Level Error!")

버전 내역

R2013a에 개발됨