matlab.unittest.plugins.ToFile class

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

Output stream to write text output to file

Description

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

Construction

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

Input Arguments

fname

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

Properties

Filename

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.

Examples

collapse all

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

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

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.

runner.addPlugin(plugin)
result = runner.run(suite);
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:
                Actual    Expected    Error    RelativeError
                ______    ________    _____    _____________
            
                5         4           1        0.25         
    
    Actual double:
             5
    Expected double:
             4

    ------------------
    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 DiagnosticsOutputPlugin to display output on the screen.

Observe contents in the file created by the plugin.

disp(fileread(filename))
1..3
not ok 1 - ExampleTest/testOne
# ================================================================================
# Verification failed in ExampleTest/testOne.
# 
#     ----------------
#     Test Diagnostic:
#     ----------------
#     Testing 5==4
# 
#     ---------------------
#     Framework Diagnostic:
#     ---------------------
#     verifyEqual failed.
#     --> The values are not equal using "isequaln".
#     --> Failure table:
#                 Actual    Expected    Error    RelativeError
#                 ______    ________    _____    _____________
#             
#                 5         4           1        0.25         
#     
#     Actual double:
#              5
#     Expected double:
#              4
# 
#     ------------------
#     Stack Information:
#     ------------------
#     In C:\work\ExampleTest.m (ExampleTest.testOne) at 4
# ================================================================================
# 
ok 2 - ExampleTest/testTwo
ok 3 - ExampleTest/testThree
Introduced in R2014a