Main Content

앱 테스트 프레임워크와 모의 프레임워크를 사용하는 테스트 작성하기

이 예제에서는 앱 테스트 프레임워크와 모의 프레임워크를 사용하는 테스트를 작성하는 방법을 보여줍니다. 앱에는 파일 선택 대화 상자와 선택한 파일을 나타내는 레이블이 포함됩니다. 앱을 프로그래밍 방식으로 테스트하려면 모의 객체를 사용하여 파일 선택기의 동작을 정의하십시오.

앱 만들기

현재 작업 폴더에 launchApp 앱을 만듭니다. 이 앱을 사용하면 사용자는 입력 파일을 선택하고 앱에 파일 이름을 표시할 수 있습니다. 파일 선택 대화 상자는 사용자 입력이 있기 전에는 다른 창에 접근할 수 없는 모달 대화 상자입니다.

function app = launchApp
    f = uifigure;
    button = uibutton(f,'Text','Input file');
    button.ButtonPushedFcn = @(src,evt)pickFile;
    label = uilabel(f,'Text','No file selected');
    label.Position(1) = button.Position(1) + button.Position(3) + 25;
    label.Position(3) = 200;
    
    % Add components to an App struct for output
    app.UIFigure = f;
    app.Button = button;
    app.Label = label;
    
    function file = pickFile()
        [file,folder,status] = uigetfile('*.*');
        if status
            label.Text = file;
        end
    end
end

테스트 전에 이 앱의 속성을 살펴보려면 명령 프롬프트에서 앱의 인스턴스를 생성하십시오. 이 단계는 테스트에 반드시 필요하진 않지만 앱 테스트에서 사용하는 속성을 살펴보는 것은 도움이 됩니다. 예를 들어, app.Button을 사용하여 앱 객체 내 Input file 버튼에 액세스합니다.

app = launchApp;

App window displaying Input file button

수동 작업으로 앱 테스트하기

모의 객체를 사용하지 않고 LaunchAppTest 클래스를 생성합니다. 테스트에서는 현재 작업 폴더에 파일 input2.txt가 있다고 간주합니다. 파일이 존재하지 않는 경우 이 파일을 생성합니다. 이 테스트는 프로그래밍 방식으로 Input file 버튼을 누르고 레이블이 'input2.txt'와 일치하는지 확인합니다. 파일은 수동으로 선택해야 합니다.

classdef LaunchAppTest < matlab.uitest.TestCase
    properties
        TestFile = 'input2.txt';
    end
    methods(TestClassSetup)
        function checkTestFiles(tc)
            import matlab.unittest.constraints.IsFile
            tc.assumeThat(tc.TestFile,IsFile)
        end
    end
    methods (Test)
        function testInputButton(tc)
            app = launchApp;
            tc.addTeardown(@close,app.UIFigure);
            
            tc.press(app.Button);

            tc.verifyEqual(app.Label.Text,tc.TestFile)
        end
    end
end

테스트를 실행합니다. 파일 선택 대화 상자가 나타나면 MATLAB에서 테스트를 진행할 수 있도록 input2.txt를 선택합니다. 다른 파일을 선택하면 테스트가 실패합니다.

results = runtests('LaunchAppTest');
Running LaunchAppTest
.
Done LaunchAppTest
__________

완전히 자동화된 테스트 생성하기

수동 작업 없이 앱을 테스트하려면 모의 프레임워크를 사용하십시오. 앱에서 파일 선택 서비스를 구현하는 대신 이 서비스를 허용하도록 앱을 수정합니다(의존성 주입).

파일 선택 기능을 구현하는 Abstract 메서드를 사용하여 FileChooser 서비스를 만듭니다.

classdef FileChooser
    % Interface to choose a file
    methods (Abstract)
        [file,folder,status] = chooseFile(chooser,varargin)
    end
end

파일 선택에 uigetfile 함수를 사용하는 디폴트 FileChooser를 만듭니다.

classdef DefaultFileChooser < FileChooser
    methods
        function [file,folder,status] = chooseFile(chooser,varargin)
            [file,folder,status] = uigetfile(varargin{:});
        end
    end
end

선택적 FileChooser 객체를 허용하도록 앱을 변경합니다. 입력값 없이 메서드를 호출하면 앱에서는 DefaultFileChooser의 인스턴스를 사용합니다.

function app = launchApp(fileChooser)
    if nargin==0
        fileChooser = DefaultFileChooser;
    end
    f = uifigure;
    button = uibutton(f,'Text','Input file');
    button.ButtonPushedFcn = @(src,evt)pickFile(fileChooser);
    label = uilabel(f,'Text','No file selected');
    label.Position(1) = button.Position(1) + button.Position(3) + 25;
    label.Position(3) = 200;
    
    % Add components to an App struct for output
    app.UIFigure = f;
    app.Button = button;
    app.Label = label;
    
    function file = pickFile(fileChooser)
        [file,folder,status] = fileChooser.chooseFile('*.*');
        if status
            label.Text = file;
        end
    end
end

LaunchAppTest에 다음과 같이 수정 사항을 적용합니다.

  • 테스트가 matlab.uitest.TestCasematlab.mock.TestCase 모두로부터 상속되도록 변경합니다.

  • properties 블록과 TestClassSetup 블록을 제거합니다. 모의 객체가 chooseFile 메서드 호출의 출력값을 정의하기 때문에 테스트는 외부 파일 존재 여부에 좌우되지 않습니다.

  • 다음 작업을 수행할 수 있도록 testInputButton 테스트 메서드를 변경합니다.

    • FileChooser의 모의 객체 생성.

    • '*.*' 입력값으로 chooseFile 메서드 호출 시, 출력값이 테스트 파일 이름('input2.txt'), 현재 작업 폴더 및 선택한 필터 인덱스 1이 되도록 모의 객체 동작 정의. 이 출력값은 uigetfile 함수의 출력값과 유사합니다.

    • 버튼을 누르고 선택한 파일 이름 확인. 이러한 단계는 원래 테스트와 동일하지만 모의 객체가 출력값을 할당하기 때문에 테스트 진행을 위해 사용자가 앱과 상호 작용할 필요가 없습니다.

  • Cancel 버튼을 테스트하려면 다음 작업을 수행할 수 있도록 테스트 메서드 testInputButton_Cancel을 추가하십시오.

    • FileChooser의 모의 객체 생성.

    • '*.*' 입력값으로 chooseFile 메서드 호출 시, 출력값이 테스트 파일 이름('input2.txt'), 현재 작업 폴더 및 선택한 필터 인덱스 0이 되도록 모의 객체 동작 정의. 사용자가 파일을 선택한 후 Cancel을 선택하면 uigetfile 함수의 출력값과 유사한 출력값을 얻게 됩니다.

    • 버튼을 누르고 테스트가 chooseFile 메서드를 호출하는지 및 선택한 파일이 없다고 레이블에 표시되는지 확인.

classdef LaunchAppTest < matlab.uitest.TestCase & matlab.mock.TestCase
    methods (Test)
        function testInputButton(tc)
            import matlab.mock.actions.AssignOutputs
            fname = 'myFile.txt';
            
            [mockChooser,behavior] = tc.createMock(?FileChooser);
            when(behavior.chooseFile('*.*'),AssignOutputs(fname,pwd,1))
            
            app = launchApp(mockChooser);
            tc.addTeardown(@close,app.UIFigure);
            
            tc.press(app.Button);

            tc.verifyEqual(app.Label.Text,fname);
        end
        
        function testInputButton_Cancel(tc)
            import matlab.mock.actions.AssignOutputs
            
            [mockChooser, behavior] = tc.createMock(?FileChooser);
            when(behavior.chooseFile('*.*'),AssignOutputs('myFile.txt',pwd,0))
            
            app = launchApp(mockChooser);
            tc.addTeardown(@close,app.UIFigure);
            
            tc.press(app.Button);
            
            tc.verifyCalled(behavior.chooseFile('*.*'));
            tc.verifyEqual(app.Label.Text,'No file selected');
        end
    end
end

테스트를 실행하십시오. 수동으로 파일을 선택하지 않아도 테스트가 완료됩니다.

results = runtests('LaunchAppTest');
Running LaunchAppTest
..
Done LaunchAppTest
__________

참고 항목

|

관련 항목