matlab.unittest.plugins.ToFile class

Package: matlab.unittest.plugins
Superclasses: matlab.unittest.plugins.OutputStream

Output stream to write text output to file


The ToFile class creates an output stream that writes text output to a file. Whenever text prints to this stream, the output stream opens the file, appends the text, and closes the file.


matlab.unittest.plugins.ToFile(fname) creates an OutputStream that writes text output to the file, fname.

Input Arguments


Name of file to write the output text, specified as a string. If fname exists, the text from the stream is appended to the file.



Name of file to redirect text output from the plugin, specified in the input argument, fname.

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB® documentation.


collapse all

Direct TAPPlugin Output Text to Separate File

In your working folder, create the file ExampleTest.m containing the following test class.

classdef ExampleTest < matlab.unittest.TestCase
        function testOne(testCase)  % Test fails
            testCase.verifyEqual(5, 4, 'Testing 5==4')
        function testTwo(testCase)  % Test passes
            testCase.verifyEqual(5, 5, 'Testing 5==5')
        function testThree(testCase)
            % test code

The verifyEqual qualification in testOne causes a test failure. The qualifications in testOne and testTwo include an instance of a matlab.unittest.diagnostics.StringDiagnostic.

At the command prompt, create a test suite from the ExampleTest class.

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.TAPPlugin
import matlab.unittest.plugins.ToFile

suite   = TestSuite.fromClass(?ExampleTest);

Create a test runner that displays output to the command window.

runner = TestRunner.withTextOutput;

Create a TAPPlugin that explicitly specifies that its output should go to the file, MyTapOutput.tap.

filename = 'MyTapOutput.tap';
plugin = TAPPlugin.producingOriginalFormat(ToFile(filename));

Add the plugin to the TestRunner and run the suite.

result =;
Running ExampleTest

Verification failed in ExampleTest/testOne.

    Test Diagnostic:
    Testing 5==4

    Framework Diagnostic:
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Index    Actual    Expected    Error    RelativeError
                _____    ______    ________    _____    _____________
                1        5         4           1        0.25         
    Actual Value:
    Expected Value:

    Stack Information:
    In C:\work\ExampleTest.m (ExampleTest.testOne) at 4
Done ExampleTest

Failure Summary:

     Name                 Failed  Incomplete  Reason(s)
     ExampleTest/testOne    X                 Failed by verification.

Only the test failures produce output to the screen. By default, TestRunner.withTextOutput uses a FailureDiagnosticsPlugin to display output on the screen.

Observe contents in the file created by the plugin.

not ok 1 - ExampleTest/testOne
ok 2 - ExampleTest/testTwo
ok 3 - ExampleTest/testThree

Introduced in R2014a

Was this topic helpful?