Is there a way to run a unit test with a MATLAB script file testing certain variable values? I know a unit test can be used in functions, but I need a way to use it with basic script files as well.

 채택된 답변

Sean de Wolski
Sean de Wolski 2015년 8월 4일

0 개 추천

댓글 수: 3

Connor Gill
Connor Gill 2015년 8월 4일
That is creating a unit test in a script, but it still only checks outputs from a function file. I'm trying to find out if you can use a unit test to check a script for variable values.
Sean de Wolski
Sean de Wolski 2015년 8월 4일
편집: Sean de Wolski 2015년 8월 4일
Why would calling a function from the script not be the same as calling a script from the script?
Connor Gill
Connor Gill 2015년 8월 4일
A script has no outputs, so there wouldn't be anything for the unit test to actually check, as far as I know.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andy Campbell
Andy Campbell 2015년 8월 4일
편집: Andy Campbell 2015년 8월 4일

2 개 추천

Hi Conner,
You can can just call the script from within a test function or method. Then the variables that were created in the script will be created in the test function's workspace and can just be operated on as variables in that workspace. Using the test we talked about in your other question this may look like this:
foo_AndyCampbell.m
result = 5;
Problem1Test.m
classdef Problem1Test < matlab.unittest.TestCase
properties(ClassSetupParameter)
student = generateClassList;
end
properties
StudentFcn
end
methods(TestClassSetup)
function findStudentFunction(testCase, student)
% Look for foo_AndyCampbell, foo_ConnorGill, etc
% and store the function handle
testCase.StudentFcn = str2func(['foo_' student]);
end
end
methods(Test)
function testItWorks(testCase)
testCase.StudentFcn();
testCase.verifyEqual(result, 5)
end
end
end
function students = generateClassList
students = {'AndyCampbell', 'ConnorGill'};
end

카테고리

도움말 센터File Exchange에서 Testing Frameworks에 대해 자세히 알아보기

태그

질문:

2015년 8월 4일

댓글:

2015년 8월 4일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by