matlab.unittest.TestSuite.fromFolder - Create test suite from tests in folder - MATLAB (original) (raw)
Class: matlab.unittest.TestSuite
Namespace: matlab.unittest
Create test suite from tests in folder
Syntax
Description
suite = matlab.unittest.TestSuite.fromFolder([folder](#mw%5F9a86f095-93e2-4905-873c-43a5d47defef))
creates a test suite from the tests in the specified folder and returns the test suite as amatlab.unittest.TestSuite array.
To run the test suite, the testing framework changes the current folder to the folder that defines the test content and adds it to the path for the duration of the test run.
suite = matlab.unittest.TestSuite.fromFolder([folder](#mw%5F9a86f095-93e2-4905-873c-43a5d47defef),[selector](#ref%5Fq237bgre93%5Fsep%5Fmw%5F5a5f33c9-88ba-4611-8b39-30ba9c575eee))
includes only the tests that satisfy the conditions of the specified selector.
suite = matlab.unittest.TestSuite.fromFolder(___,[Name,Value](#namevaluepairarguments))
specifies options using one or more name-value arguments in addition to the input argument combinations in previous syntaxes. For example, suite = matlab.unittest.TestSuite.fromFolder(pwd,"IncludingSubfolders",true)
creates a test suite from the tests in the current folder and any of its subfolders.
Input Arguments
Name of the folder containing tests, specified as a string scalar or character vector. If you specify a relative path, the path must be in the current folder. Otherwise, you must specify a full path.
Example: pwd
Example: "myFolder"
Example: "C:\work\myFolder"
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Example: suite = matlab.unittest.TestSuite.fromFolder(pwd,IncludingSubfolders=true)
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: suite = matlab.unittest.TestSuite.fromFolder(pwd,"IncludingSubfolders",true)
Option to include the tests in the subfolders of folder, specified as a numeric or logical 0
(false
) or1
(true
). By default, the method does not include the tests in subfolders.
Action to take against an invalid test file in folder, specified as one of these values:
"warn"
— The method issues a warning for each invalid test file infolder
and creates a test suite from the valid files."error"
— The method throws an error if it finds an invalid test file infolder
.
An invalid test file is a test file from which the framework cannot generate a test suite. Examples include a test file that contains syntax errors, a function-based test file that is missing local functions, and a file with a Test
method that is passed an undefined parameterization property.
Name of the base folder that contains the test file, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the Test
element must be contained in one of the base folders specified byBaseFolder
. If none of theTest
elements match a base folder, an empty test suite is returned. Use the wildcard character (*
) to match any number of characters. Use the question mark character (?
) to match a single character.
For test files defined in namespaces, the base folder is the parent of the top-level namespace folder.
Names of the files and folders that contain source code, specified as a string vector, character vector, or cell vector of character vectors. This argument filters the test suite by including only the tests that depend on the specified source code. If none of the tests depend on the source code, an empty test suite is returned.
The specified value must represent at least one existing file. If you specify a folder, the framework extracts the paths to the files within the folder.
You must have a MATLAB® Test™ license to use DependsOn
. For more information about selecting tests by source code dependency, see matlabtest.selectors.DependsOn (MATLAB Test).
Example: DependsOn=["myFile.m" "myFolder"]
Example: DependsOn=["folderA" "C:\work\folderB"]
Name of the test, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the Name
property of theTest
element must match one of the names specified byName
. If none of the Test
elements have a matching name, an empty test suite is returned. Use the wildcard character (*
) to match any number of characters. Use the question mark character (?
) to match a single character.
For a given test file, the name of a test uniquely identifies the smallest runnable portion of the test content. The test name includes the namespace name, filename (excluding the extension), procedure name, and information about parameterization.
Name of a test class property that defines a parameter used by the test, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, theParameterization
property of the Test
element must contain at least one of the property names specified byParameterProperty
. If none of the Test
elements have a matching property name, an empty test suite is returned. Use the wildcard character (*
) to match any number of characters. Use the question mark character (?
) to match a single character.
Name of a parameter used by the test, specified as a string array, character vector, or cell array of character vectors. MATLAB generates parameter names based on the test class property that defines the parameters. For example:
- If the property value is a cell array, MATLAB generates parameter names from the elements of the cell array by taking into account their values, types, and dimensions.
- If the property value is a structure, MATLAB generates parameter names from the structure fields.
The ParameterName
argument filters the test suite. For the testing framework to include a test in the filtered suite, theParameterization
property of theTest
element must contain at least one of the parameter names specified by ParameterName
. If none of the Test
elements have a matching parameter name, an empty test suite is returned. Use the wildcard character (*
) to match any number of characters. Use the question mark character (?
) to match a single character.
Name of the class that the test class derives from, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, theTestClass
property of the Test
element must point to a test class that derives from one of the classes specified bySuperclass
. If none of the Test
elements match a class, an empty test suite is returned.
Name of a tag used by the test, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the Tags
property of the Test
element must contain at least one of the tag names specified by Tag
. If none of the Test
elements have a matching tag name, an empty test suite is returned. Use the wildcard character (*
) to match any number of characters. Use the question mark character (?
) to match a single character.
Examples
Create test suites from the tests in a folder by using the fromFolder
static method.
This example assumes that a folder named myFolder
exists in your current folder. Create the folder if it does not exist.
[,] = mkdir("myFolder")
In a file named add5.m
in the myFolder
folder, create the add5
function. The function accepts a numeric input and increments it by 5. If called with a nonnumeric input, the function throws an error.
function y = add5(x) % add5 - Increment input by 5 if ~isa(x,"numeric") error("add5:InputMustBeNumeric","Input must be numeric.") end y = x + 5; end
To test the add5
function, create the Add5Test
class in a file named Add5Test.m
in the myFolder
folder. The class tests the function against numeric and nonnumeric inputs.
classdef Add5Test < matlab.unittest.TestCase properties (TestParameter) type = {'double','single','int8','int32'}; end
methods (Test)
function numericInput(testCase,type)
actual = add5(cast(1,type));
testCase.verifyClass(actual,type)
end
function nonnumericInput(testCase)
testCase.verifyError(@() add5("0"),"add5:InputMustBeNumeric")
end
end
end
Import the classes used in this example.
import matlab.unittest.TestSuite import matlab.unittest.selectors.HasParameter
Make sure that your current folder is the parent folder of myFolder
. Create a test suite from the tests in the myFolder
folder and display the test names. The suite contains the tests defined in the Add5Test
class.
suite = TestSuite.fromFolder("myFolder"); disp({suite.Name}')
{'Add5Test/numericInput(type=double)'}
{'Add5Test/numericInput(type=single)'}
{'Add5Test/numericInput(type=int8)' }
{'Add5Test/numericInput(type=int32)' }
{'Add5Test/nonnumericInput' }
Create a test suite that includes only the parameterized tests from the myFolder
folder.
suite1 = TestSuite.fromFolder("myFolder",HasParameter); disp({suite1.Name}')
{'Add5Test/numericInput(type=double)'}
{'Add5Test/numericInput(type=single)'}
{'Add5Test/numericInput(type=int8)' }
{'Add5Test/numericInput(type=int32)' }
Create a test suite that includes only the tests whose name contains "nonnumeric"
from the myFolder
folder.
suite2 = TestSuite.fromFolder("myFolder","Name","nonnumeric"); disp({suite2.Name}')
{'Add5Test/nonnumericInput'}
Version History
Introduced in R2013a
When you select function-based or class-based tests using theDependsOn
name-value argument or thematlabtest.selectors.DependsOn
class (requires MATLAB Test), the method more accurately selects tests that depend on the specified source code. If the method can determine which individual tests in the test file depend on the source code, then it selects only the dependent tests and excludes the rest. Otherwise, the method includes all the tests in the test file.
In previous releases, the method includes all the tests in a test file if the file depends on the specified source code, without attempting to exclude tests that are not dependent on the source code.
If you have a MATLAB Test license, you can specify any type of source file using theDependsOn
name-value argument or thematlabtest.selectors.DependsOn
class. In previous releases, you can specify files only with a .m
, .p
,.mlx
, .mlapp
, .mat
, or.slx
extension.
If you have a MATLAB Test license, you can filter a test suite by test file dependency on specified source code. Use the DependsOn
name-value argument or thematlabtest.selectors.DependsOn
class to specify the source files and folders.
To specify whether the testing framework issues a warning or throws an error when it encounters an invalid test file in a folder, use theInvalidFileFoundAction
name-value argument.
When you assign a nonempty cell array to a parameterization property, the testing framework generates parameter names from the elements of the cell array by taking into account their values, types, and dimensions. In previous releases, if the property value is a cell array of character vectors, the framework generates parameter names from the values in the cell array. Otherwise, the framework specifies parameter names as value1
, value2
, …, valueN
.
If your code uses parameter names to create or filter test suites, replace the old parameter names with the descriptive parameter names. For example, update suite = testsuite(pwd,"ParameterName","value1")
by replacing value1
with a descriptive parameter name.
The matlab.unittest.TestSuite.fromFolder
method treats folders and namespaces the same way, and includes tests defined within namespace folders when creating a test suite. For example, suite = matlab.unittest.TestSuite.fromFolder(pwd,IncludingSubfolders=true)
creates a suite from all the test files in the current folder and any of its subfolders, including namespace folders. In previous releases, the method ignores any tests defined in a namespace folder and its subfolders.
This behavior change also applies to the testsuite, runtests, and runperf functions when they operate on a folder containing tests. With the consistent treatment of folders and namespaces, creating a suite from all test files within a folder and its subfolders becomes more convenient and independent of the folder structure.
To exclude tests defined within namespaces, filter the suite being constructed or returned by fromFolder
. For example, create a filtered test suite comprising tests whose names do not include any dots (that is, do not refer to any namespaces).
import matlab.unittest.TestSuite import matlab.unittest.selectors.HasName import matlab.unittest.constraints.ContainsSubstring suite = TestSuite.fromFolder(pwd,HasName(~ContainsSubstring(".")), ... IncludingSubfolders=true);