Main Content

matlab.unittest.diagnostics.ConstraintDiagnostic 클래스

네임스페이스: matlab.unittest.diagnostics
슈퍼클래스: matlab.automation.diagnostics.Diagnostic

제약 조건에 흔히 사용되는 필드들을 사용한 진단

설명

matlab.unittest.diagnostics.ConstraintDiagnostic 클래스는 제약 조건에 흔히 사용되는 필드들을 사용한 진단을 제공합니다. 이러한 필드에는 설명, 조건, 실제 값, 예상 값이 포함됩니다. 자세한 내용은 진단 필드 항목을 참조하십시오.

ConstraintDiagnostic 클래스를 사용하면 사용자 지정 제약 조건에 의해 생성된 진단에 일반적인 디자인을 간단히 추가할 수 있습니다. ConstraintDiagnostic 객체를 사용하여 Constraint 서브클래스의 getDiagnosticFor 메서드 또는 BooleanConstraint 서브클래스의 getNegativeDiagnosticFor 메서드를 구현할 수 있습니다.

matlab.unittest.diagnostics.ConstraintDiagnostic 클래스는 handle 클래스입니다.

생성

설명

예제

diag = matlab.unittest.diagnostics.ConstraintDiagnosticConstraintDiagnostic 객체를 만듭니다. 그런 다음 객체의 속성을 설정하여 제약 조건 진단을 사용자 지정할 수 있습니다.

속성

모두 확장

ConstraintDiagnostic 클래스는 다음 속성 외에도 Diagnostic 클래스로부터 Artifacts 속성 및 DiagnosticText 속성을 상속합니다.

설명

일반 진단 정보로, string형 스칼라 또는 문자형 벡터로 지정됩니다.

특성:

GetAccess
public
SetAccess
public

Description 속성에 텍스트를 표시하기 위한 옵션으로, 숫자형 또는 논리값 0(false) 또는 1(true)로 지정됩니다. 기본적으로 제약 조건에는 설명이 표시되지 않습니다.

특성:

GetAccess
public
SetAccess
public
조건

서식 있는 조건 목록으로, 문자형 벡터로 반환됩니다. 프레임워크에서 진단을 표시할 때 각 조건은 새 줄(Newline)에서 화살표(-->) 구분 기호로 시작합니다.

조건은 테스트 실패의 원인과 관련된 정보를 포함하며 “하위 진단” 역할을 합니다. addCondition 메서드와 addConditionsFrom 메서드를 사용하여 목록에 조건을 추가합니다.

특성:

GetAccess
public
SetAccess
private

Conditions 속성에 저장된 조건 목록에 있는 조건의 개수로, 음이 아닌 정수 스칼라로 반환됩니다.

특성:

GetAccess
public
SetAccess
private

데이터형: double

Conditions 속성에 조건 목록을 표시하기 위한 옵션으로, 숫자형 또는 논리값 0(false) 또는 1(true)로 지정됩니다. 기본적으로 제약 조건에는 조건 목록이 표시되지 않습니다.

특성:

GetAccess
public
SetAccess
public
실제 값

테스트할 실제 값으로, 임의의 데이터형 값으로 지정됩니다.

특성:

GetAccess
public
SetAccess
public

실제 값에 대한 제목 정보로, string형 스칼라 또는 문자형 벡터로 지정됩니다.

특성:

GetAccess
public
SetAccess
public

실제 값과 제목 정보를 표시하기 위한 옵션으로, 숫자형 또는 논리값 0(false) 또는 1(true)로 지정됩니다. 기본적으로 제약 조건에는 실제 값이 표시되지 않습니다.

특성:

GetAccess
public
SetAccess
public
예상 값

예상 값으로, 해당되는 경우 임의의 데이터형 값으로 지정됩니다.

특성:

GetAccess
public
SetAccess
public

예상 값에 대한 제목 정보로, string형 스칼라 또는 문자형 벡터로 지정됩니다.

특성:

GetAccess
public
SetAccess
public

예상 값과 제목 정보를 표시하기 위한 옵션으로, 숫자형 또는 논리값 0(false) 또는 1(true)로 지정됩니다. 기본적으로 제약 조건에는 예상 값이 표시되지 않습니다.

특성:

GetAccess
public
SetAccess
public

메서드

모두 확장

예제

모두 축소

값이 예상 값과 동일한 크기인지 확인하는 사용자 지정 제약 조건을 만듭니다. 제약 조건에 대한 진단 정보를 생성하기 위해 ConstraintDiagnostic 클래스를 사용하여 getDiagnosticFor 메서드를 구현합니다.

현재 폴더의 파일에서 matlab.unittest.constraints.Constraint에서 파생되는 IsSameSizeAs라는 클래스를 만들고 satisfiedBy 메서드와 getDiagnosticFor 메서드를 구현합니다. getDiagnosticFor 메서드를 구현하기 위해 ConstraintDiagnostic 클래스의 인스턴스를 사용합니다. ConstraintDiagnostic 클래스를 사용하면 테스트 성공 또는 실패에 대한 다양한 진단 필드를 편리하게 사용자 지정할 수 있습니다.

classdef IsSameSizeAs < matlab.unittest.constraints.Constraint
    properties (SetAccess=immutable)
        ValueWithExpectedSize
    end

    methods
        function constraint = IsSameSizeAs(value)
            constraint.ValueWithExpectedSize = value;
        end

        function tf = satisfiedBy(constraint,actual)
            tf = constraint.sizeMatchesExpected(actual);
        end

        function diagnostic = getDiagnosticFor(constraint,actual)
            if constraint.sizeMatchesExpected(actual)
                diagnostic = diagnosticForPassingTest;
            else
                diagnostic = diagnosticForFailingTest(constraint,actual);
            end
        end
    end

    methods (Access=private)
        function tf = sizeMatchesExpected(constraint,actual)
            tf = isequal(size(actual), ...
                size(constraint.ValueWithExpectedSize));
        end

        function diag = diagnosticForPassingTest(~,~)
            import matlab.unittest.diagnostics.ConstraintDiagnostic
            diag = ConstraintDiagnostic;
            
            diag.DisplayDescription = true;
            diag.Description = "IsSameSizeAs passed.";
        end

        function diag = diagnosticForFailingTest(constraint,actual)
            import matlab.unittest.diagnostics.ConstraintDiagnostic
            diag = ConstraintDiagnostic;
            
            diag.DisplayDescription = true;
            diag.Description = "IsSameSizeAs failed.";

            diag.DisplayConditions = true;
            diag.addCondition("Sizes did not match.")

            diag.DisplayActVal = true;
            diag.ActValHeader = "Actual Size:";
            diag.ActVal = size(actual);

            diag.DisplayExpVal = true;
            diag.ExpValHeader = "Expected Size:";
            diag.ExpVal = size(constraint.ValueWithExpectedSize);
        end
    end
end

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

testCase = matlab.unittest.TestCase.forInteractiveUse;

실패 케이스를 테스트합니다. 프레임워크는 diagnosticForFailingTest 헬퍼 메서드에 구현된 제약 조건 진단을 표시합니다.

testCase.verifyThat(zeros(5),IsSameSizeAs(ones(1,5)))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsSameSizeAs failed.
    --> Sizes did not match.
    
    Actual Size:
         5     5
    Expected Size:
         1     5

진단의 설명 필드 앞에 텍스트를 삽입하는 제약 조건 진단을 만듭니다.

현재 폴더의 파일에 matlab.unittest.diagnostics.ConstraintDiagnostic에서 파생되는 CustomConstraintDiagnostic이라는 클래스를 만듭니다. 사용자 지정 진단 클래스가 해당 슈퍼클래스로부터 상속하는 getPreDescriptionString 메서드를 구현하여 진단의 선택적 설명 필드 앞에 텍스트를 삽입합니다.

classdef CustomConstraintDiagnostic < ...
        matlab.unittest.diagnostics.ConstraintDiagnostic
    properties (SetAccess=immutable)
        Context
    end

    methods
        function diag = CustomConstraintDiagnostic(context)
            arguments
                context (1,1) string = "Test Outcome Information"
            end
            diag.Context = context;
        end
    end

    methods (Access=protected)
        function str = getPreDescriptionString(diag)
            str = diag.Context;
        end
    end
end

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

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo

CustomConstraintDiagnostic 클래스를 인스턴스화하여 제약 조건 진단을 만듭니다.

diagnostic = CustomConstraintDiagnostic;
diagnostic.DisplayDescription = true;
diagnostic.Description = "My Custom Diagnostic";

실제로는 주로 제약 조건 진단을 사용하여 사용자 지정 제약 조건을 구현합니다. 이 예제를 단순화하기 위해 대화형 방식 테스트를 위한 테스트 케이스를 생성하고 CustomConstraintDiagnostic 객체를 사용하여 테스트 실패 시 진단 정보를 표시합니다. 프레임워크는 지정된 설명과 설명 앞에 삽입된 디폴트 텍스트를 모두 표시합니다.

testCase = TestCase.forInteractiveUse;
testCase.verifyThat(1,IsEqualTo(2),diagnostic)
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Test Outcome Information
    My Custom Diagnostic
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsEqualTo failed.
    --> NumericComparator failed.
        --> The numeric values are not equal using "isequaln".
        --> Failure table:
                Actual    Expected    Error    RelativeError
                ______    ________    _____    _____________
                                                            
                  1          2         -1          -0.5     
        
        Actual Value:
             1
        Expected Value:
             2

세부 정보

모두 확장

버전 내역

R2013a에 개발됨