MATLAB Class-based unit tests: How to pass in variable containing data to test

3 ビュー (過去 30 日間)
doyz
doyz 2018 年 8 月 14 日
回答済み: Jeff Miller 2018 年 8 月 14 日
I'm not sure how to pass in variables when executing a unit test. These variables were created from another function not placed within the unit test.
*Method 1:*
classdef myTest < matlab.unittest.TestCase
properties
A, B, C
end
methods (Test)
function testDataCoverage(testCase)
expSol = afunction(A, B, C)
actSol = 10
testCase.verifyEqual(testCase, actSol, expSol)
end
end
end
I next tried to place the variable-creating function (getData) within the unit test but encountered this error:
Concrete class myTest does not define a TestParameter property named BNew for the dataCoverage method. Either implement the property or define the class as Abstract.
*Method 2:*
classdef myTest < matlab.unittest.TestCase
properties
end
methods (Test)
function testDataCoverage(testCase)
[A, B, C] = getData()
expSol = afunction(A, B, C)
actSol = 10
testCase.verifyEqual(testCase, actSol, expSol)
end
function [A, B, C] = getData()
...code here...
end
function Sol = afunction(A, BNew, C)
...code here...
end
end
end

回答 (1 件)

Jeff Miller
Jeff Miller 2018 年 8 月 14 日
Try putting getData and afunction in a separate "methods" section--note, plain "methods", without "(Test)"--that is placed before your "methods (Test)" section

カテゴリ

Help Center および File ExchangeCreate and Run Performance Tests についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by