isCompatible
클래스: matlab.unittest.fixtures.Fixture
네임스페이스: matlab.unittest.fixtures
두 픽스처가 호환되는지 확인
설명
tf = isCompatible(
는 두 픽스처의 호환성을 테스트 프레임워크에 보고합니다. 픽스처가 호환되는 경우 메서드는 논리값 fixture
1,fixture
2)1
(true
)을 반환합니다. 그렇지 않으면 논리값 0
(false
)을 반환합니다. 두 픽스처가 클래스가 동일하고 환경을 동일하게 변경한다면 이 둘은 호환됩니다.
픽스처를 구성할 수 있는 경우(예: 해당 클래스 생성자가 입력 인수를 받는 경우) Fixture
서브클래스에서 isCompatible
메서드를 구현하십시오. 프레임워크는 isCompatible
을 호출하여 동일한 Fixture
서브클래스의 인스턴스들이 동일한 공유 테스트 픽스처 상태에 해당하는지 확인합니다. 픽스처 호환성에 대한 정보는 프레임워크가 언제 해제 동작이나 설정 동작을 수행할지 결정할 때 유용합니다. 프레임워크는 동일한 클래스의 두 인스턴스에 대해서만 isCompatible
메서드를 호출합니다. 따라서, 픽스처의 클래스가 서로 다른 경우를 처리하는 코드를 구현할 필요가 없습니다.
입력 인수
fixture
— 픽스처
matlab.unittest.fixtures.Fixture
객체
픽스처로, matlab.unittest.fixtures.Fixture
객체로 지정됩니다.
예제
구성 가능한 픽스처의 호환성
숫자형 값의 출력 표시 형식을 변경하는 구성 가능한 픽스처를 만듭니다. 구성 가능한 픽스처를 정의하는 클래스는 setup
메서드 외에 isCompatible
메서드도 구현해야 합니다.
현재 폴더의 NumericFormatFixture.m
파일에서, matlab.unittest.fixtures.Fixture
인터페이스를 서브클래스화하여 NumericFormatFixture
클래스를 만듭니다. 이 클래스에 다음 요소를 추가합니다.
Format
속성 — 이 속성을 추가하여 픽스처를 구성할 때 지정된 수치 형식을 저장합니다.NumericFormatFixture
메서드 — 이 생성자 메서드를 추가하여Format
속성을 설정합니다.setup
메서드 — 이 메서드를 구현하여 픽스처가 출력 표시 형식을 지정된 수치 형식으로 변경하도록 합니다. 테스트 프레임워크가 픽스처를 해제할 때 환경이 복원되도록setup
메서드 내에서addTeardown
메서드를 호출합니다.isCompatible
메서드 — 이 메서드를 구현하여NumericFormatFixture
인스턴스가 공유 테스트 픽스처로 사용될 때 테스트 프레임워크가 이 인스턴스의 호환성을 테스트할 수 있도록 합니다.
classdef NumericFormatFixture < matlab.unittest.fixtures.Fixture properties (SetAccess=immutable) Format (1,1) string end methods function fixture = NumericFormatFixture(fmt) fixture.Format = fmt; end function setup(fixture) originalFormat = format; fixture.addTeardown(@format,originalFormat) format(fixture.Format) fixture.SetupDescription = "Set the numeric format to " + ... fixture.Format + "."; fixture.TeardownDescription = ... "Restored the numeric format to " + ... originalFormat.NumericFormat + "."; end end methods (Access=protected) function tf = isCompatible(fixture1,fixture2) tf = fixture1.Format == fixture2.Format; end end end
현재 폴더에서, NumericFormatFixture
인스턴스를 공유 테스트 픽스처로 사용하는 세 개의 테스트 클래스를 만듭니다. 각 클래스는 숫자형 값이 공유 테스트 픽스처에 의해 적용된 형식으로 표시되는지 테스트합니다. 이 예제를 단순화하기 위해 실제 값은 formattedDisplayText
함수의 호출로 생성됩니다. 실제 상황에서는 사용자가 정의한 코드를 테스트합니다.
TestA.m
이라는 파일에서 TestA
클래스를 만듭니다.
classdef (SharedTestFixtures={NumericFormatFixture("bank")}) ... TestA < matlab.unittest.TestCase methods (Test) function formatTest(testCase) actual = strtrim(formattedDisplayText(pi)); expected = "3.14"; testCase.verifyEqual(actual,expected) end end end
TestB.m
이라는 파일에서 TestB
클래스를 만듭니다.
classdef (SharedTestFixtures={NumericFormatFixture("bank")}) ... TestB < matlab.unittest.TestCase methods (Test) function formatTest(testCase) actual = strtrim(formattedDisplayText(100/3)); expected = "33.33"; testCase.verifyEqual(actual,expected) end end end
TestC.m
이라는 파일에서 TestC
클래스를 만듭니다.
classdef (SharedTestFixtures={NumericFormatFixture("hex")}) ... TestC < matlab.unittest.TestCase methods (Test) function formatTest(testCase) actual = strtrim(formattedDisplayText(1)); expected = "3ff0000000000000"; testCase.verifyEqual(actual,expected) end end end
TestA
클래스와 TestB
클래스에는 환경을 똑같이 변경하는 공유 픽스처가 할당되었습니다. 반면에 TestC
클래스에는 다른 수치 형식을 적용하는 픽스처가 할당되었습니다. 이 예제에 구현된 isCompatible
메서드에 따르면, 테스트 프레임워크는 TestA
와 TestB
의 픽스처가 호환되는 것으로 파악합니다. 그러나 TestC
의 픽스처는 다른 픽스처와 호환되지 않는다고 파악합니다.
픽스처 호환성에 대한 정보는 프레임워크가 언제 해제 동작이나 설정 동작을 수행할지 결정할 때 유용합니다. TestA
, TestB
, TestC
를 동일한 테스트 스위트로 실행하는 경우, TestA
와 TestB
의 클래스는 둘 다 동일한 환경을 필요로 하기 때문에 이 두 테스트 간에 전환할 때는 프레임워크가 픽스처를 해제하지 않습니다. 그러나 TestB
에서 TestC
로 전환할 때는 프레임워크가 기존 픽스처를 해제하고 TestC
에 필요한 새 픽스처를 설정합니다. 이 예제에서 모든 테스트가 통과합니다.
runtests(["TestA" "TestB" "TestC"]);
Setting up NumericFormatFixture Done setting up NumericFormatFixture: Set the numeric format to bank. __________ Running TestA . Done TestA __________ Running TestB . Done TestB __________ Tearing down NumericFormatFixture Done tearing down NumericFormatFixture: Restored the numeric format to short. __________ Setting up NumericFormatFixture Done setting up NumericFormatFixture: Set the numeric format to hex. __________ Running TestC . Done TestC __________ Tearing down NumericFormatFixture Done tearing down NumericFormatFixture: Restored the numeric format to short. __________
버전 내역
R2014a에 개발됨
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)