Borrar filtros
Borrar filtros

Logging the full results of a unit test in MATLAB unit test Framework

22 visualizaciones (últimos 30 días)
I am using the MATLAB unit test framework to test the output of my code. In my test bed, I noticed that, when I use the 'TestReporterPlugin' to output the test results to a PDF, the elements of vectors over 10 elements in length will not print out explicitly.
For example, the test method below outputs the entire vector. Note that this is a test method of a subclass of 'matlab.unittest.TestCase'.
function testExample(testCase)
expected = ones(1,10);
actual = ones(1,10);
testCase.verifyEqual(actual,expected)
end
The test report output from this method is shown below:
verifyEqual passed.
--> The values are equal using "isequaln".
Actual Value:
1 1 1 1 1 1 1 1 1 1
Expected Value:
1 1 1 1 1 1 1 1 1 1
However, if I change 'actual' and 'expected' to 'ones(1,11)', the elements of the output are suppressed, as shown below:
verifyEqual passed.
--> The values are equal using "isequaln".
Actual Value:
1x11 double
Expected Value:
1x11 double
Is there a way that I can output the entire vector to the Test Report?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 17 de Abr. de 2024
Editada: MathWorks Support Team el 17 de Abr. de 2024
There are several known workarounds to modify the method "testExample"
1. You can use 'DisplayDiagnostic' objects as 'test diagnostics', which occupy the last input of the qualification method. 
The code snippet below details this approach:
function testExample(testCase)
import matlab.unittest.diagnostics.DisplayDiagnostic
actual = ones(1,11);
expected = ones(1,11);
testCase.verifyEqual(actual,expected,[DisplayDiagnostic(expected) DisplayDiagnostic(actual)]);
end
More information about the 'DisplayDiagnostic' class can be found at the link below:
2. You can create a nested function to customize the display, and pass this in as a test diagnostic. The code snippet below illustrates this approach:
function testExample(testCase)
actual = ones(1,11);
expected = ones(1,11);
testCase.verifyEqual(actual,expected,@displayActExp);
function displayActExp()
disp('Actual:');
disp (actual);
disp ('Expected:');
disp (expected);
end
end
The documentation for 'Function Handle Diagnostics' is linked below:
Both of these elements will produce the desired output in the Test Report.
  1 comentario
Steven Lord
Steven Lord el 10 de Mayo de 2018
verifyThat will accept a diagnostic input argument, either as a char vector or as a function handle.
>> TC = matlab.unittest.TestCase.forInteractiveUse;
>> import matlab.unittest.constraints.IsEqualTo;
>> verifyThat(TC, 1+1, IsEqualTo(3), 'Why doesn''t 1+1 equal 3?')
Interactive verification failed.
----------------
Test Diagnostic:
----------------
Why doesn't 1+1 equal 3?
---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> NumericComparator failed.
--> The values are not equal using "isequaln".
--> Failure table:
Actual Expected Error RelativeError
______ ________ _____ __________________
2 3 -1 -0.333333333333333
Actual Value:
2
Expected Value:
3
The diagnostic message looks like it's the third input if you call it using dot notation, but it's actually the fourth.
TC.verifyThat(1+1, IsEqualTo(3), 'Why doesn''t 1+1 equal 3?')

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Function-Based Unit Tests en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by