matlab.unittest.plugins.CodeCoveragePlugin.forFile

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

Construct CodeCoveragePlugin for files

Description

example

matlab.unittest.plugins.CodeCoveragePlugin.forFile(file,'Producing',reportFormat) creates a plugin that produces a code coverage report for one or more .m, .mlx, or .mlapp files.

Input Arguments

expand all

Name of files to analyze, specified as a character vector, cell array of character vectors, string scalar, or string array. file is the absolute or relative path to one or more .m, .mlx, or .mlapp files.

Example: '../thisTest.m'

Example: {'Test_featureB.m','Test_featureA.m'}

Data Types: char | string

Report format, specified as an instance of the matlab.unittest.plugins.codecoverage.CoverageReport or the matlab.unittest.plugins.codecoverage.CoberturaFormat class.

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

Examples

expand all

In your working folder, create a file quadraticSolver.m with 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 class for the quadratic solver in ForFileSolverTest.m.

classdef ForFileSolverTest < 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, create a test suite for ForFileSolverTest.m, and create a test runner.

import matlab.unittest.TestRunner

testFile = 'ForFileSolverTest.m';
suite = testsuite(testFile);
runner = TestRunner.withTextOutput;

Create a plugin that outputs a code coverage report for the source code in Cobertura format.

import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.codecoverage.CoberturaFormat

sourceCodeFile = 'quadraticSolver.m';
reportFile = 'CoverageResults.xml';
reportFormat = CoberturaFormat(reportFile);
plugin = CodeCoveragePlugin.forFile(sourceCodeFile,'Producing',reportFormat);

Add the plugin to the test runner and run the tests.

runner.addPlugin(plugin);
result = runner.run(suite);
Running ForFileSolverTest
..
Done ForFileSolverTest
__________

Display the contents of the file with the code coverage results.

disp(fileread(reportFile))
<?xml version="1.0" encoding="utf-8"?>
<coverage branch-rate="NaN" branches-covered="NaN" branches-valid="NaN" complexity="NaN" line-rate="0.8" lines-covered="4" lines-valid="5" timestamp="737000.4343" version="NaN">
   <sources>
      <source>C:\work\</source>
   </sources>
   <packages>
      <package branch-rate="NaN" complexity="NaN" line-rate="0.8" name="">
         <classes>
            <class branch-rate="NaN" complexity="NaN" filename="quadraticSolver.m" line-rate="0.8" name="quadraticSolver">
               <methods/>
               <lines>
                  <line branch="NaN" condition-coverage="NaN" hits="2" number="4"/>
                  <line branch="NaN" condition-coverage="NaN" hits="2" number="5"/>
                  <line branch="NaN" condition-coverage="NaN" hits="2" number="6"/>
                  <line branch="NaN" condition-coverage="NaN" hits="2" number="9"/>
                  <line branch="NaN" condition-coverage="NaN" hits="0" number="10"/>
               </lines>
            </class>
         </classes>
      </package>
   </packages>
</coverage>
Introduced in R2017b