MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Apply Custom Fixture as Shared Fixture

In your working folder, create three test classes using a shared fixture. Using a shared
fixture allows the UserNameEnvironmentVariableFixture to be shared across
classes.

Create testA.m as follows.

classdef (SharedTestFixtures={...
UserNameEnvironmentVariableFixture('David')}) ...
testA < matlab.unittest.TestCase
methods (Test)
function t1(~)
fprintf(1, 'Current UserName: "%s"', getenv('UserName'))
end
end
end

Create testB.m as follows.

classdef (SharedTestFixtures={...
UserNameEnvironmentVariableFixture('Andy')}) ...
testB < matlab.unittest.TestCase
methods (Test)
function t1(~)
fprintf(1, 'Current UserName: "%s"', getenv('UserName'))
end
end
end

Create testC.m as follows.

classdef (SharedTestFixtures={...
UserNameEnvironmentVariableFixture('Andy')}) ...
testC < matlab.unittest.TestCase
methods (Test)
function t1(~)
fprintf(1, 'Current UserName: "%s"', getenv('UserName'))
end
end
end

At the command prompt, run the tests.

runtests({'testA','testB','testC'});

33 Unit Testing

Free download pdf