matlab.unittest.TestSuite.fromProject - Create test suite from tests in project - MATLAB (original) (raw)

Class: matlab.unittest.TestSuite
Namespace: matlab.unittest

Create test suite from tests in project

Syntax

Description

[suite](#mw%5F70e8c658-6a71-480e-b02e-a4a86ab5ddfb) = matlab.unittest.TestSuite.fromProject([project](#mw%5F5b195f38-9671-4716-b71b-ed521979b961)) creates a TestSuite array from all test files contained in the specified project that are labeled with the Test classification. Theproject input is either a loaded matlab.project.Project object or the root folder of a project. This method is not recursive. It includes only those tests in the project specified. To include tests from referenced projects, specify the IncludingReferencedProjects name-value argument as true. For more information on projects, see Projects.

example

[suite](#mw%5F70e8c658-6a71-480e-b02e-a4a86ab5ddfb) = matlab.unittest.TestSuite.fromProject([project](#mw%5F5b195f38-9671-4716-b71b-ed521979b961),[selector](#mw%5F264834c8-ff8d-4175-9a9b-92c147c76e1e%5Fsep%5Fmw%5F5a5f33c9-88ba-4611-8b39-30ba9c575eee)) creates a TestSuite array from all test files contained in the specified project that are labeled with the Test classification and that satisfy the selector. For more information on selectors, see matlab.unittest.selectors.

[suite](#mw%5F70e8c658-6a71-480e-b02e-a4a86ab5ddfb) = matlab.unittest.TestSuite.fromProject(___,[Name,Value](#namevaluepairarguments)) creates a TestSuite array from all test files contained in the specified project that are labeled with the Test classification and that satisfy the conditions specified by one or more name-value arguments. Specify the name-value arguments after all of the arguments in any of the previous syntaxes.

example

Input Arguments

expand all

Project containing test files, specified as the path to the project root folder or an open Project object. A test file is a file that is classified as test by adding the Test label in the project.

Example: 'C:\MyProjects\ThisProject'

Data Types: char | string

Name-Value Arguments

expand all

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.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Option to include tests from referenced projects in theTestSuite, specified as logical true orfalse. By default, fromProject includes test files only from the project specified in the input. Passing a value oftrue for IncludingReferencedProjects results in a TestSuite array that includes the tests from the project specified in the input and tests from projects referenced from the parent project. For more information on referenced projects, see Componentize Large Projects.

Example: suite = matlab.unittest.TestSuite.fromProject(project,'IncludingReferencedProjects',true);

Data Types: logical

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:

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.

Output Arguments

Examples

expand all

Build a test suite from project files that are labeled asTest files. This example assumes that a project folder atC:/projects/project1 contains test files that are labeled with theTest classification. Use thematlab.unittest.TestSuite.fromProject static method to create a test suite using those tests.

Open project1 and pass the matlab.project.Project object to fromProject. Run the test suite and capture the results.

import matlab.unittest.TestSuite project = openProject('C:/projects/project1/'); suite = TestSuite.fromProject(project); result = run(suite)

Build a test suite from project files that are labeled asTest files in the project and all referenced projects.

import matlab.unittest.TestSuite project = openProject('C:/projects/project1/'); suite = TestSuite.fromProject(project,'IncludingReferencedProjects',true); result = run(suite)

Version History

Introduced in R2019a

expand all

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.

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.

If your project includes files with the Test classification,matlab.unittest.TestSuite.fromProject ignores the files that do not define test procedures when creating a test suite. For example, if an abstractTestCase class definition file is labeled with theTest classification, fromProject ignores it. In previous releases, MATLAB produces an error if fromProject is called on a project that uses the Test classification for any files other than concrete test files. With this change, fromProject becomes consistent with the matlab.unittest.TestSuite.fromFolder method: both methods create a test suite from all the concrete test files and ignore any other files in the folder.

This behavior change also applies to the testsuite, runtests, and runperf functions when they operate on code organized into files and folders within a project.