Main Content

matlab.unittest.plugins.TestRunProgressPlugin.withVerbosity

Class: matlab.unittest.plugins.TestRunProgressPlugin
Namespace: matlab.unittest.plugins

Create plugin that displays test run progress at specified verbosity

Description

plugin = matlab.unittest.plugins.TestRunProgressPlugin.withVerbosity(level) creates a plugin that displays test run progress at the specified verbosity level. By default, the plugin directs its text output to the screen.

example

plugin = matlab.unittest.plugins.TestRunProgressPlugin.withVerbosity(level,stream) creates a plugin that directs its data to the specified output stream.

example

Input Arguments

expand all

Verbosity level, specified as an integer scalar from 0 through 4, a matlab.automation.Verbosity enumeration object, or a text representation of the enumeration.

Numeric RepresentationEnumeration Member NameVerbosity Description
0None

No information

1Terse

Minimal information

2Concise

Moderate amount of information

3Detailed

Some supplemental information

4Verbose

Lots of supplemental information

Example: 3

Example: "Detailed"

Output stream where the plugin directs text output, specified as a matlab.automation.streams.OutputStream object. By default, the plugin directs its output to the screen.

Example: matlab.automation.streams.ToFile("myFile.txt")

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand 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