Logging the full results of a unit test in MATLAB unit test Framework
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 22 Nov. 2017
Bearbeitet: MathWorks Support Team
am 17 Apr. 2024
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?
Akzeptierte Antwort
MathWorks Support Team
am 17 Apr. 2024
Bearbeitet: MathWorks Support Team
am 17 Apr. 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:
https://www.mathworks.com/help/matlab/ref/matlab.automation.diagnostics.displaydiagnostic-class.html
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 Kommentar
Steven Lord
am 10 Mai 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?')
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Write Unit Tests finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!