Main Content

matlab.unittest.plugins.TestRunProgressPlugin Class

Namespace: matlab.unittest.plugins
Superclasses: matlab.unittest.plugins.TestRunnerPlugin, matlab.unittest.plugins.Parallelizable

Plugin that displays test run progress

Description

The matlab.unittest.plugins.TestRunProgressPlugin class provides a plugin that displays test run progress. You can use the plugin to control the amount of text output displayed during a test run as well as the location of the text output.

The matlab.unittest.plugins.TestRunProgressPlugin class is a handle class.

Creation

To create a TestRunProgressPlugin instance, use the withVerbosity static method.

Methods

expand all

Examples

collapse all

Run tests and control the amount of information to display by using the TestRunProgressPlugin class.

In a file named cylinderPlotTest.m in your current folder, create function-based tests to test a cylinder plot.

function tests = cylinderPlotTest
tests = functiontests(localfunctions);
end
 
function setupOnce(testCase)
testCase.TestData.Figure = figure;
addTeardown(testCase,@close,testCase.TestData.Figure)
end
 
function setup(testCase)
testCase.TestData.Axes = axes("Parent",testCase.TestData.Figure);
addTeardown(testCase,@clf,testCase.TestData.Figure)
cylinder(testCase.TestData.Axes,10)
end
 
function testXLim(testCase) 
xlim = testCase.TestData.Axes.XLim;
verifyLessThanOrEqual(testCase,xlim(1),-10)
verifyGreaterThanOrEqual(testCase,xlim(2),10)
end

function zdataTest(testCase)
s = findobj(testCase.TestData.Axes,"Type","surface");
verifyEqual(testCase,min(s.ZData(:)),0)
verifyEqual(testCase,max(s.ZData(:)),1)
end

Import the TestRunProgressPlugin class.

import matlab.unittest.plugins.TestRunProgressPlugin

Create a test suite from the test file.

suite = testsuite("cylinderPlotTest.m");

Run the tests using a default test runner. The test runner displays test run progress at the matlab.automation.Verbosity.Concise level (level 2).

runner = testrunner;
results = runner.run(suite);
Running cylinderPlotTest
..
Done cylinderPlotTest
__________

Create a new test runner and configure it using a plugin that displays test run progress at the matlab.automation.Verbosity.Terse level (level 1). Then, rerun the tests using the test runner.

runner = testrunner("minimal");
plugin = TestRunProgressPlugin.withVerbosity("Terse");
runner.addPlugin(plugin)
results = runner.run(suite);
..

Rerun the tests and display test run progress at the matlab.automation.Verbosity.Verbose level (level 4), which is the highest verbosity level.

runner = testrunner("minimal");
plugin = TestRunProgressPlugin.withVerbosity("Verbose");
runner.addPlugin(plugin)
results = runner.run(suite);
 Running cylinderPlotTest
  Setting up cylinderPlotTest
    Evaluating TestClassSetup: setupOnce
  Done setting up cylinderPlotTest in 0.093533 seconds
   Running cylinderPlotTest/testXLim
    Evaluating TestMethodSetup: setup
    Evaluating Test: testXLim
    Evaluating TestMethodTeardown: teardown
    Evaluating addTeardown function: clf
   Done cylinderPlotTest/testXLim in 0.15021 seconds
   Running cylinderPlotTest/zdataTest
    Evaluating TestMethodSetup: setup
    Evaluating Test: zdataTest
    Evaluating TestMethodTeardown: teardown
    Evaluating addTeardown function: clf
   Done cylinderPlotTest/zdataTest in 0.028826 seconds
  Tearing down cylinderPlotTest
    Evaluating TestClassTeardown: teardownOnce
    Evaluating addTeardown function: close
  Done tearing down cylinderPlotTest in 0.018563 seconds
 Done cylinderPlotTest in 0.29114 seconds
__________

Control the verbosity level and location to display test run progress information by using the TestRunProgressPlugin class.

In a file named ExampleTest.m in your current folder, create the ExampleTest test class.

classdef ExampleTest < matlab.unittest.TestCase
    methods (Test)
        function testOne(testCase)  % Test fails
            testCase.verifyEqual(5,4)
        end

        function testTwo(testCase)  % Test passes
            testCase.verifyEqual(5,5)
        end
    end
end

Import the classes used in this example.

import matlab.unittest.plugins.TestRunProgressPlugin
import matlab.automation.streams.ToFile

Create a test suite from the ExampleTest class.

suite = testsuite("ExampleTest");

Using a TestRunProgressPlugin instance, run the tests and display test run progress at the matlab.automation.Verbosity.Detailed level (level 3). By default, the plugin directs its text output to the screen.

runner = testrunner("minimal");
plugin = TestRunProgressPlugin.withVerbosity("Detailed");
runner.addPlugin(plugin)
results = runner.run(suite);
 Running ExampleTest
  Setting up ExampleTest
  Done setting up ExampleTest in 0 seconds
   Running ExampleTest/testOne
   Done ExampleTest/testOne in 0.54909 seconds
   Running ExampleTest/testTwo
   Done ExampleTest/testTwo in 0.011 seconds
  Tearing down ExampleTest
  Done tearing down ExampleTest in 0 seconds
 Done ExampleTest in 0.56009 seconds
__________

Create a new test runner and configure it using a plugin that directs its output to a file named myOutput.log in your current folder. If you rerun the tests, the information on test run progress no longer appears in the Command Window. The plugin directs the text output to the specified file instead of the screen.

runner = testrunner("minimal");
plugin = TestRunProgressPlugin.withVerbosity( ...
    "Detailed",ToFile("myOutput.log"));
runner.addPlugin(plugin)
results = runner.run(suite);

Display the contents of the file created by the plugin.

disp(fileread("myOutput.log"))
 Running ExampleTest
  Setting up ExampleTest
  Done setting up ExampleTest in 0 seconds
   Running ExampleTest/testOne
   Done ExampleTest/testOne in 0.051384 seconds
   Running ExampleTest/testTwo
   Done ExampleTest/testTwo in 0.0099046 seconds
  Tearing down ExampleTest
  Done tearing down ExampleTest in 0 seconds
 Done ExampleTest in 0.061288 seconds
__________

Version History

Introduced in R2014b