Main Content

간단한 테스트 스위트 생성하기

이 예제에서는 SolverTest 테스트 케이스를 사용하여 테스트를 테스트 스위트로 결합하는 방법을 보여줍니다. 테스트 스위트를 네임스페이스와 클래스로 구성하든지, 파일과 폴더로 구성하든지, 혹은 두 가지 조합을 다 써서 구성하든지 간에, 테스트 조합에 대한 스위트를 만들려면 matlab.unittest.TestSuite 클래스에서 정적 from* 메서드를 사용하십시오.

2차 솔버 함수 생성하기

작업 폴더의 파일 quadraticSolver.m에서 2차 방정식의 근을 구하는 다음과 같은 함수를 만드십시오.

function r = quadraticSolver(a, b, c)
% quadraticSolver returns solutions to the 
% quadratic equation a*x^2 + b*x + c = 0.

if ~isa(a,'numeric') || ~isa(b,'numeric') || ~isa(c,'numeric')
    error('quadraticSolver:InputMustBeNumeric', ...
        'Coefficients must be numeric.');
end

r(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
r(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);

end

2차 솔버 함수에 대한 테스트 생성하기

작업 폴더의 파일 SolverTest.m에 다음 테스트 클래스를 만듭니다.

classdef SolverTest < matlab.unittest.TestCase
    % SolverTest tests solutions to the quadratic equation
    % a*x^2 + b*x + c = 0
    
    methods (Test)
        function testRealSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2,1];
            testCase.verifyEqual(actSolution,expSolution);
        end
        function testImaginarySolution(testCase)
            actSolution = quadraticSolver(1,2,10);
            expSolution = [-1+3i, -1-3i];
            testCase.verifyEqual(actSolution,expSolution);
        end
    end
    
end

TestSuite 클래스 가져오기

명령 프롬프트에서 matlab.unittest.TestSuite 클래스를 현재 가져오기 목록에 추가하십시오.

import matlab.unittest.TestSuite

SolverTest 클래스 정의 파일이 MATLAB® 경로에 있도록 하십시오.

SolverTest 클래스에서 테스트 스위트 생성하기

fromClass 메서드는 SolverTest 클래스에 있는 모든 Test 메서드로부터 테스트 스위트를 만듭니다.

suiteClass = TestSuite.fromClass(?SolverTest);
result = run(suiteClass);

SolverTest 클래스 정의 파일에서 테스트 스위트 생성하기

fromFile 메서드는 파일 이름을 사용하여 클래스를 식별하는 테스트 스위트를 만듭니다.

suiteFile = TestSuite.fromFile('SolverTest.m');
result = run(suiteFile);

현재 폴더의 모든 테스트 케이스 파일에서 테스트 스위트 생성하기

fromFolder 메서드는 지정된 폴더에 있는 모든 테스트 케이스 파일로부터 테스트 스위트를 만듭니다. 예를 들어, 다음 파일이 현재 폴더에 있습니다.

  • BankAccountTest.m

  • DocPolynomTest.m

  • FigurePropertiesTest.m

  • IsSupportedTest.m

  • SolverTest.m

suiteFolder = TestSuite.fromFolder(pwd);
result = run(suiteFolder);

단일 테스트 메서드에서 테스트 스위트 생성하기

fromMethod 메서드는 단일 테스트 메서드로부터 테스트 스위트를 만듭니다.

suiteMethod = TestSuite.fromMethod(?SolverTest,'testRealSolution')'
result = run(suiteMethod);

참고 항목

관련 항목