클래스를 사용하여 간단한 테스트 케이스 작성하기
matlab.unittest.TestCase
클래스에서 상속되는 테스트 클래스 내에 단위 테스트를 정의하여 MATLAB® 프로그램을 테스트할 수 있습니다. 클래스 기반 테스트에서 단위 테스트는 소프트웨어 내의 단위의 정확성을 확인하는 방법입니다. 단위 테스트는 Test
특성을 사용하여 methods
블록 내에 정의되며, 값을 테스트하고 실패 상황에 대응하는 가설 검정을 사용할 수 있습니다. 클래스 기반 테스트에 대한 자세한 내용은 MATLAB에서 클래스 기반 단위 테스트 작성하기 항목을 참조하십시오.
이 예제에서는 클래스 기반 단위 테스트를 작성하여 현재 폴더 내 파일에 정의된 함수의 정확성을 가설 검정하는 방법을 보여줍니다. quadraticSolver
함수는 2차 다항식 계수를 입력값으로 받아 해당 2차 다항식의 근을 반환합니다. 계수가 숫자형이 아닌 값으로 지정된 경우 함수가 오류를 발생시킵니다.
function roots = 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 roots(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a); roots(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a); end
SolverTest 클래스 생성하기
현재 폴더의 SolverTest.m
파일에서 matlab.unittest.TestCase
클래스를 서브클래스화하여 SolverTest
클래스를 만듭니다. 이 클래스는 quadraticSolver
함수에 대한 테스트를 저장하는 위치를 제공합니다. Test
특성을 사용하여 methods
블록 내에 3개의 단위 테스트를 추가합니다. 이 단위 테스트는 실수 해, 허수 해 및 오류 조건에 대해 quadraticSolver
함수를 테스트합니다. 각 Test
메서드는 입력값으로 TestCase
인스턴스를 받아야 합니다. 블록 내의 테스트 순서는 중요하지 않습니다.
먼저, Test
메서드 realSolution
을 만들어 quadraticSolver
가 특정 계수에 대해 올바른 실수 해를 반환하는지를 확인합니다. 예를 들어, 방정식 은 실수 해 과 를 가집니다. 메서드는 이 방정식의 계수를 사용하여 quadraticSolver
를 호출합니다. 그런 다음 matlab.unittest.TestCase
의 verifyEqual
메서드를 사용하여 실제 출력값인 actSolution
을 예상 출력값 expSolution
과 비교합니다.
classdef SolverTest < matlab.unittest.TestCase methods(Test) function realSolution(testCase) actSolution = quadraticSolver(1,-3,2); expSolution = [2 1]; testCase.verifyEqual(actSolution,expSolution) end end end
두 번째 Test
메서드 imaginarySolution
을 만들어 quadraticSolver
가 특정 계수에 대해 올바른 허수 해를 반환하는지를 확인합니다. 예를 들어, 방정식 은 허수 해 와 를 가집니다. 위의 메서드와 마찬가지로 이 메서드는 이 방정식의 계수를 사용하여 quadraticSolver
를 호출한 후 verifyEqual
메서드를 사용하여 실제 출력값인 actSolution
을 예상 출력값 expSolution
과 비교합니다.
classdef SolverTest < matlab.unittest.TestCase methods(Test) function realSolution(testCase) actSolution = quadraticSolver(1,-3,2); expSolution = [2 1]; testCase.verifyEqual(actSolution,expSolution) end function imaginarySolution(testCase) actSolution = quadraticSolver(1,2,10); expSolution = [-1+3i -1-3i]; testCase.verifyEqual(actSolution,expSolution) end end end
마지막으로, Test
메서드 nonnumericInput
을 추가하여 quadraticSolver
가 숫자형이 아닌 계수에 대한 오류를 생성하는지를 확인합니다. matlab.unittest.TestCase
의 verifyError
메서드를 사용하여 함수가 입력값 1
, '-3'
및 2
를 사용하여 호출된 경우 'quadraticSolver:InputMustBeNumeric'
으로 지정된 오류를 발생시키는지 테스트합니다.
classdef SolverTest < matlab.unittest.TestCase methods(Test) function realSolution(testCase) actSolution = quadraticSolver(1,-3,2); expSolution = [2 1]; testCase.verifyEqual(actSolution,expSolution) end function imaginarySolution(testCase) actSolution = quadraticSolver(1,2,10); expSolution = [-1+3i -1-3i]; testCase.verifyEqual(actSolution,expSolution) end function nonnumericInput(testCase) testCase.verifyError(@()quadraticSolver(1,'-3',2), ... 'quadraticSolver:InputMustBeNumeric') end end end
SolverTest 클래스에 있는 테스트 실행하기
SolverTest
클래스에 있는 모든 테스트를 실행하려면 클래스에서 TestCase
객체를 생성한 후 객체에 대해 run
메서드를 호출하십시오. 이 예제에서는 3개의 테스트가 모두 통과했습니다.
testCase = SolverTest; results = testCase.run
Running SolverTest ... Done SolverTest __________
results = 1×3 TestResult array with properties: Name Passed Failed Incomplete Duration Details Totals: 3 Passed, 0 Failed, 0 Incomplete. 2.0136 seconds testing time.
또한 Test
메서드 중 하나로 지정된 단일 테스트를 실행할 수도 있습니다. 특정 Test
메서드를 실행하려면 메서드의 이름을 run
으로 전달하십시오. 예를 들어, realSolution
메서드를 실행합니다.
result = run(testCase,'realSolution')
Running SolverTest . Done SolverTest __________
result = TestResult with properties: Name: 'SolverTest/realSolution' Passed: 1 Failed: 0 Incomplete: 0 Duration: 0.0114 Details: [1×1 struct] Totals: 1 Passed, 0 Failed, 0 Incomplete. 0.011409 seconds testing time.