MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Test Suite Creation


Calling your function-based test returns a suite of Test objects. You also can use the
testsuite function or the TestSuite.fromFile method. If you want a particular test
and you know the test name, you can use TestSuite.fromName. If you want to create a
suite from all tests in a particular folder, you can use TestSuite.fromFolder.

Test Selection


With an explicit test suite, use selectors to refine your suite. Several of the selectors are
applicable only for class-based tests, but you can select tests for your suite based on the
test name:


  • Use the 'Name' name-value pair argument in a suite generation method, such as
    fromFile.

  • Use a selectors instance and optional constraints instance.


Use these approaches in a suite generation method, such as fromFile, or create a suite
and filter it using the TestSuite.selectIf method. For example, in this listing, the
four values of suite are equivalent.

import matlab.unittest.selectors.HasName
import matlab.unittest.constraints.ContainsSubstring
import matlab.unittest.TestSuite.fromFile

f = 'rightTriTolTest.m';
selector = HasName(ContainsSubstring('Triangle'));

% fromFile, name-value pair
suite = TestSuite.fromFile(f,'Name','*Triangle*')

% fromFile, selector
suite = TestSuite.fromFile(f,selector)

% selectIf, name-value pair
fullSuite = TestSuite.fromFile(f);
suite = selectIf(fullSuite,'Name','*Triangle*')

% selectIf, selector
fullSuite = TestSuite.fromFile(f);
suite = selectIf(fullSuite,selector)

33 Unit Testing

Free download pdf