Running a Unit Test for a script

3 visualizaciones (últimos 30 días)
Connor Gill
Connor Gill el 4 de Ag. de 2015
Comentada: Connor Gill el 4 de Ag. de 2015
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.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 4 de Ag. de 2015
  3 comentarios
Sean de Wolski
Sean de Wolski el 4 de Ag. de 2015
Editada: Sean de Wolski el 4 de Ag. de 2015
Why would calling a function from the script not be the same as calling a script from the script?
Connor Gill
Connor Gill el 4 de Ag. de 2015
A script has no outputs, so there wouldn't be anything for the unit test to actually check, as far as I know.

Iniciar sesión para comentar.

Más respuestas (1)

Andy Campbell
Andy Campbell el 4 de Ag. de 2015
Editada: Andy Campbell el 4 de Ag. de 2015
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

Categorías

Más información sobre Testing Frameworks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by