matlab.unittest.plugins.CodeCoveragePlugin.forFolder

Class: matlab.unittest.plugins.CodeCoveragePlugin
Package: matlab.unittest.plugins

Construct CodeCoveragePlugin for folders

Description

example

matlab.unittest.plugins.CodeCoveragePlugin.forFolder(folder) creates a plugin that produces a code coverage report for one or more folders. The plugin reports on the source code inside folder.

matlab.unittest.plugins.CodeCoveragePlugin.forFolder(folder,Name,Value) produces a code coverage report with additional options specified by one or more Name,Value pair arguments. For example, matlab.unittest.plugins.CodeCoveragePlugin.forFolder(pwd,'IncludingSubfolders',true) produces a code coverage report for source code in the current folder and its subfolders.

Input Arguments

expand all

Locations of folder containing source code, specified as a character vector, a cell array of character vectors, or a string array. folder is the absolute or relative path to one or more folders. If you specify multiple folders, MATLAB® opens a profile coverage report for each folder.

The source code folders must be on the MATLAB path and remain on the path during the test run.

Example: 'C:\projects\myproj'

Example: pwd

Example: {'C:\projects\myprojA','myprojB'}

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: matlab.unittest.plugins.CodeCoveragePlugin.forFolder(pwd,'IncludingSubfolders',true) produces a code coverage report for source code in the current folder and its subfolders.

Setting to include source code in subfolders of folder, specified by false or true. By default, CodeCoveragePlugin does not report on source code in subfolders.

Data Types: logical

Report format, specified as either a matlab.unittest.plugins.codecoverage.CoverageReport instance or an instance of a different class in the matlab.unittest.plugins.codecoverage package. By default, the report format is matlab.unittest.plugins.codecoverage.CoverageReport, which displays a MATLAB Code Coverage Report.

Example: matlab.unittest.plugins.codecoverage.CoberturaFormat('CoverageResults.xml')

Examples

expand all

In a new file, quadraticSolver.m, in your working folder, create the following function.

function roots = quadraticSolver(a,b,c)
% quadraticSolver returns solutions to the
% quadratic equation a*x^2 + b*x + c = 0.

checkInputs

roots(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
roots(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);

    function checkInputs
        if ~isa(a,'numeric') || ~isa(b,'numeric') || ~isa(c,'numeric')
            error('quadraticSolver:InputMustBeNumeric', ...
                'Coefficients must be numeric.')
        end
    end
end

Create a test for the quadratic solver. In a tests package (a +tests subfolder), create SolverTest.m containing the following test class.

classdef SolverTest < matlab.unittest.TestCase
    % SolverTest tests solutions to the quadratic equation
    % a*x^2 + b*x + c = 0
    
    methods (Test)
        function testRealSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2,1];
            testCase.verifyEqual(actSolution,expSolution)
        end
        function testImaginarySolution(testCase)
            actSolution = quadraticSolver(1,2,10);
            expSolution = [-1+3i, -1-3i];
            testCase.verifyEqual(actSolution,expSolution)
        end
    end
    
end

At the command prompt from within your original working folder, create a test suite from the tests package.

import matlab.unittest.TestSuite
import matlab.unittest.TestRunner
import matlab.unittest.plugins.CodeCoveragePlugin

suite = TestSuite.fromPackage('tests');

Create a test runner.

runner = TestRunner.withTextOutput;

Add CodeCoveragePlugin to the runner and run the tests. Specify that the source code folder is your current working folder. If you have other source code files in your current working folder, they show up in the coverage report. The folder that contains the source code (quadraticSolver.m) must be on the MATLAB search path.

runner.addPlugin(CodeCoveragePlugin.forFolder(pwd))
result = runner.run(suite);
Running tests.SolverTest
..
Done tests.SolverTest
__________

MATLAB creates a code coverage report for the quadratic solver function in a temporary folder.

Code coverage report for quadraticSolver

The checkinputs nested function does not have complete code coverage. Since the tests in SolverTest.m do not pass nonnumeric input to quadraticSolver, MATLAB does not exercise the code that throws an error if the inputs are not numeric. To address the missing coverage, add a test method to test the error condition.

Introduced in R2014b