matlab.unittest.TestSuite.fromNamespace
Class: matlab.unittest.TestSuite
Namespace: matlab.unittest
Create test suite from tests in namespace
Renamed from matlab.unittest.TestSuite.fromPackage
in R2024a
Syntax
Description
suite = matlab.unittest.TestSuite.fromNamespace(
creates a test suite from the tests in the specified namespace and returns the test suite as
a namespace
)matlab.unittest.TestSuite
array.
The parent folder of namespace
must be on the path when you call
the method to create a test suite and when you run the resulting test suite.
suite = matlab.unittest.TestSuite.fromNamespace(___,
specifies options using one or more name-value arguments in addition to the input argument
combinations in previous syntaxes. For example, Name,Value
)suite =
matlab.unittest.TestSuite.fromNamespace("myNamespace","IncludingInnerNamespaces",true)
creates a test suite from the tests in the specified namespace and any of its inner
namespaces.
Input Arguments
namespace
— Name of namespace containing tests
string scalar | character vector
Name of the namespace containing tests, specified as a string scalar or character vector.
Example: "myNamespace"
Example: "myNamespace.innerNamespace"
selector
— Selector
instance of selector class
Selector, specified as an instance of a class in the matlab.unittest.selectors
namespace. If you have a MATLAB®
Test™ license, you also can specify selector
as a
matlabtest.selectors.DependsOn
instance.
Example: matlab.unittest.selectors.HasTag
Name-Value Arguments
Specify optional pairs of arguments as
Name1=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.fromNamespace("myNamespace",IncludingInnerNamespaces=true)
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: suite =
matlab.unittest.TestSuite.fromNamespace("myNamespace","IncludingInnerNamespaces",true)
IncludingInnerNamespaces
— Option to include tests in inner namespaces
false
or 0
(default) | true
or 1
Option to include the tests in the inner namespaces of
namespace
, specified as a numeric or logical
0
(false
) or 1
(true
). By default, the method does not include the tests in
inner namespaces.
InvalidFileFoundAction
— Action to take against invalid test file
"warn"
(default) | "error"
Action to take against an invalid test file in namespace
,
specified as one of these values:
"warn"
— The method issues a warning for each invalid test file innamespace
and creates a test suite from the valid files."error"
— The method throws an error if it finds an invalid test file innamespace
.
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.
ExternalParameters
— External parameters to use in tests
array of matlab.unittest.parameters.Parameter
objects
External parameters to use in the tests, specified as an array of matlab.unittest.parameters.Parameter
objects. Use this argument to specify
external parameters instead of the existing parameters in a parameterized test. For more
information, see Use External Parameters in Parameterized Test.
BaseFolder
— Name of base folder
string array | character vector | cell array of character vectors
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 by
BaseFolder
. If none of the
Test
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.
DependsOn
— Names of files and folders that contain source code
string vector | character vector | cell vector of character vectors
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. For the testing framework to include a test in the filtered suite, the file that defines the test must depend on the specified source code. If none of the test files 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
— Name of test
string array | character vector | cell array of character vectors
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 the
Test
element must match one of the names specified by
Name
. 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.
ParameterProperty
— Name of parameterization property
string array | character vector | cell array of character vectors
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, the
Parameterization
property of the Test
element
must contain at least one of the property names specified by
ParameterProperty
. 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.
ParameterName
— Name of parameter
string array | character vector | cell array of character vectors
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:
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, the
Parameterization
property of the
Test
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.
ProcedureName
— Name of test procedure
string array | character vector | cell array of character vectors
Name of the test procedure, 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 ProcedureName
property of the Test
element must match one of the procedure names
specified by ProcedureName
. If none of the Test
elements have a matching procedure 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.
In a class-based test, the name of a test procedure is the name of a
Test
method that contains the test. In a function-based test, it
is the name of a local function that contains the test. In a script-based test, it is a
name generated from the test section title. Unlike the name of a test suite element, the
name of a test procedure does not include any namespace name, filename, or information
about parameterization.
Superclass
— Name of class that test class derives from
string array | character vector | cell array of character vectors
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, the
TestClass
property of the Test
element must
point to a test class that derives from one of the classes specified by
Superclass
. If none of the Test
elements match
a class, an empty test suite is returned.
Tag
— Name of tag
string array | character vector | cell array of character vectors
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 Tests in Namespace
Create test suites from the tests in a namespace by using the fromNamespace
static method.
This example assumes that a namespace named myNamespace
exists in your current folder. Create the namespace if it does not exist.
[~,~] = mkdir("+myNamespace")
In a file named add5.m
in myNamespace
, 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 myNamespace
. 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 myNamespace
. Create a test suite from the tests in myNamespace
and display the test names. The suite contains the tests defined in the Add5Test
class.
suite = TestSuite.fromNamespace("myNamespace");
disp({suite.Name}')
{'myNamespace.Add5Test/numericInput(type=double)'} {'myNamespace.Add5Test/numericInput(type=single)'} {'myNamespace.Add5Test/numericInput(type=int8)' } {'myNamespace.Add5Test/numericInput(type=int32)' } {'myNamespace.Add5Test/nonnumericInput' }
Create a test suite that includes only the parameterized tests from myNamespace
.
suite1 = TestSuite.fromNamespace("myNamespace",HasParameter);
disp({suite1.Name}')
{'myNamespace.Add5Test/numericInput(type=double)'} {'myNamespace.Add5Test/numericInput(type=single)'} {'myNamespace.Add5Test/numericInput(type=int8)' } {'myNamespace.Add5Test/numericInput(type=int32)' }
Create a test suite that includes only the tests whose name contains "nonnumeric"
from myNamespace
.
suite2 = TestSuite.fromNamespace("myNamespace","Name","*nonnumeric*"); disp({suite2.Name}')
{'myNamespace.Add5Test/nonnumericInput'}
Version History
Introduced in R2013aR2024a: Renamed from matlab.unittest.TestSuite.fromPackage
The method matlab.unittest.TestSuite.fromPackage
is now named
matlab.unittest.TestSuite.fromNamespace
. The behavior of this method
remains the same, and existing instances of
matlab.unittest.TestSuite.fromPackage
in your code continue to work as
expected. There are no plans to remove support for existing references to
matlab.unittest.TestSuite.fromPackage
.
R2024a: Include inner namespaces using IncludingInnerNamespaces
name-value argument, renamed from IncludingSubpackages
The IncludingSubpackages
name-value argument is now named IncludingInnerNamespaces
. The behavior remains the same, and existing instances of IncludingSubpackages
in your code continue to work as expected. There are no plans to remove support for existing references to IncludingSubpackages
.
R2024a: Specify any type of source file when filtering test suite by source code dependency
If you have a MATLAB
Test license, you can specify any type of source file using the
DependsOn
name-value argument or the
matlabtest.selectors.DependsOn
class. In previous releases, you can
specify files only with a .m
, .p
,
.mlx
, .mlapp
, .mat
, or
.slx
extension.
R2023a: Filter test suite by source code dependency
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 the
matlabtest.selectors.DependsOn
class to specify the source files and
folders.
R2022b: Specify action to take against invalid test files
To specify whether the testing framework issues a warning or throws an error when it
encounters an invalid test file in a namespace, use the
InvalidFileFoundAction
name-value argument.
R2022b: Parameter names generated from cell arrays are more descriptive
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.
See Also
Functions
Namespaces
Topics
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)