MATLAB Examples

Example 10

Full 'real world' example with passing, skipping & errors.

% Create a test suite based on the filename.
test_suite = junit.TestSuite(sprintf('%s Test Suite', mfilename));
% Number of test cases to create.
n_test_cases = 100;
% Random integer: [0, 100].
random_integers = randi([0, 100], 1, n_test_cases);
%
for idx = 1:numel(random_integers)
    random_integer = random_integers(idx);
    % Create a test case.
    test_case = junit.TestCase(sprintf('Test Case #%d', random_integer));
    try
        % Complete failure, just skip the test.
        assert(random_integer>10, 'Catastrophic Failure %d<10. Skipping Test', random_integer);
        try
            % Test failed to run.
            assert(random_integer>20, 'Test Failed: %d<20.', random_integer);
            try
                % Test had an error during run.
                assert(random_integer>40, 'Error runnining the test: %d<40', random_integer);
                % Passed all the tests, record a success.
                test_case.stdout=sprintf('Test Success! %d', random_integer);
            catch Error
                % Test had an error during execution.
                test_case.error(Error.identifier, Error.message);
                % Set the standard error.
                test_case.stderr=Error.message;
            end
        catch Fail
            % Test failed to run.
            test_case.fail(Fail.identifier, Fail.message);
        end
    catch Skip
        % Test was skipped.
        test_case.skipped(Skip.identifier, Skip.message)
    end
    % Add the current test case to the test suite's test cases.
    test_suite.append(test_case);
    % test_suite.test_cases = [test_suite.test_cases, test_case];
end
% Write the TestCase as an xml file with .m filename.
test_suite.xmlwrite(mfilename);
type([mfilename '.xml']);
<?xml version="1.0" encoding="utf-8"?>
<testsuites errors="22" failures="0" skipped="22" tests="100" time="0.00">
   <testsuite errors="22" failures="0" hostname="USGALWHP6047580" id="0" name="Example10 Test Suite" package="matlab" skipped="22" tests="100" timestamp="2017-08-11 17:49:49">
      <testcase name="Test Case #4" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 4&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #42" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 42</system-out>
      </testcase>
      <testcase name="Test Case #35" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 35&lt;40</system-err>
         <error type="error">Error runnining the test: 35&lt;40</error>
      </testcase>
      <testcase name="Test Case #70" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 70</system-out>
      </testcase>
      <testcase name="Test Case #36" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 36&lt;40</system-err>
         <error type="error">Error runnining the test: 36&lt;40</error>
      </testcase>
      <testcase name="Test Case #32" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 32&lt;40</system-err>
         <error type="error">Error runnining the test: 32&lt;40</error>
      </testcase>
      <testcase name="Test Case #68" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 68</system-out>
      </testcase>
      <testcase name="Test Case #58" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 58</system-out>
      </testcase>
      <testcase name="Test Case #24" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 24&lt;40</system-err>
         <error type="error">Error runnining the test: 24&lt;40</error>
      </testcase>
      <testcase name="Test Case #37" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 37&lt;40</system-err>
         <error type="error">Error runnining the test: 37&lt;40</error>
      </testcase>
      <testcase name="Test Case #20" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #26" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 26&lt;40</system-err>
         <error type="error">Error runnining the test: 26&lt;40</error>
      </testcase>
      <testcase name="Test Case #85" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 85</system-out>
      </testcase>
      <testcase name="Test Case #43" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 43</system-out>
      </testcase>
      <testcase name="Test Case #66" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 66</system-out>
      </testcase>
      <testcase name="Test Case #53" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 53</system-out>
      </testcase>
      <testcase name="Test Case #94" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 94</system-out>
      </testcase>
      <testcase name="Test Case #33" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 33&lt;40</system-err>
         <error type="error">Error runnining the test: 33&lt;40</error>
      </testcase>
      <testcase name="Test Case #36" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 36&lt;40</system-err>
         <error type="error">Error runnining the test: 36&lt;40</error>
      </testcase>
      <testcase name="Test Case #75" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 75</system-out>
      </testcase>
      <testcase name="Test Case #87" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 87</system-out>
      </testcase>
      <testcase name="Test Case #2" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 2&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #85" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 85</system-out>
      </testcase>
      <testcase name="Test Case #81" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 81</system-out>
      </testcase>
      <testcase name="Test Case #54" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 54</system-out>
      </testcase>
      <testcase name="Test Case #43" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 43</system-out>
      </testcase>
      <testcase name="Test Case #60" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 60</system-out>
      </testcase>
      <testcase name="Test Case #33" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 33&lt;40</system-err>
         <error type="error">Error runnining the test: 33&lt;40</error>
      </testcase>
      <testcase name="Test Case #78" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 78</system-out>
      </testcase>
      <testcase name="Test Case #9" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 9&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #45" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 45</system-out>
      </testcase>
      <testcase name="Test Case #76" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 76</system-out>
      </testcase>
      <testcase name="Test Case #63" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 63</system-out>
      </testcase>
      <testcase name="Test Case #0" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 0&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #1" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 1&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #46" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 46</system-out>
      </testcase>
      <testcase name="Test Case #22" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 22&lt;40</system-err>
         <error type="error">Error runnining the test: 22&lt;40</error>
      </testcase>
      <testcase name="Test Case #37" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 37&lt;40</system-err>
         <error type="error">Error runnining the test: 37&lt;40</error>
      </testcase>
      <testcase name="Test Case #44" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 44</system-out>
      </testcase>
      <testcase name="Test Case #6" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 6&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #36" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 36&lt;40</system-err>
         <error type="error">Error runnining the test: 36&lt;40</error>
      </testcase>
      <testcase name="Test Case #38" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 38&lt;40</system-err>
         <error type="error">Error runnining the test: 38&lt;40</error>
      </testcase>
      <testcase name="Test Case #30" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 30&lt;40</system-err>
         <error type="error">Error runnining the test: 30&lt;40</error>
      </testcase>
      <testcase name="Test Case #92" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 92</system-out>
      </testcase>
      <testcase name="Test Case #48" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 48</system-out>
      </testcase>
      <testcase name="Test Case #18" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #30" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 30&lt;40</system-err>
         <error type="error">Error runnining the test: 30&lt;40</error>
      </testcase>
      <testcase name="Test Case #83" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 83</system-out>
      </testcase>
      <testcase name="Test Case #62" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 62</system-out>
      </testcase>
      <testcase name="Test Case #67" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 67</system-out>
      </testcase>
      <testcase name="Test Case #62" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 62</system-out>
      </testcase>
      <testcase name="Test Case #26" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 26&lt;40</system-err>
         <error type="error">Error runnining the test: 26&lt;40</error>
      </testcase>
      <testcase name="Test Case #88" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 88</system-out>
      </testcase>
      <testcase name="Test Case #0" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 0&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #36" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 36&lt;40</system-err>
         <error type="error">Error runnining the test: 36&lt;40</error>
      </testcase>
      <testcase name="Test Case #59" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 59</system-out>
      </testcase>
      <testcase name="Test Case #93" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 93</system-out>
      </testcase>
      <testcase name="Test Case #46" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 46</system-out>
      </testcase>
      <testcase name="Test Case #42" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 42</system-out>
      </testcase>
      <testcase name="Test Case #66" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 66</system-out>
      </testcase>
      <testcase name="Test Case #77" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 77</system-out>
      </testcase>
      <testcase name="Test Case #38" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 38&lt;40</system-err>
         <error type="error">Error runnining the test: 38&lt;40</error>
      </testcase>
      <testcase name="Test Case #45" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 45</system-out>
      </testcase>
      <testcase name="Test Case #65" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 65</system-out>
      </testcase>
      <testcase name="Test Case #74" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 74</system-out>
      </testcase>
      <testcase name="Test Case #64" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 64</system-out>
      </testcase>
      <testcase name="Test Case #79" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 79</system-out>
      </testcase>
      <testcase name="Test Case #3" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 3&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #10" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 10&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #4" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 4&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #16" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #84" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 84</system-out>
      </testcase>
      <testcase name="Test Case #56" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 56</system-out>
      </testcase>
      <testcase name="Test Case #17" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #94" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 94</system-out>
      </testcase>
      <testcase name="Test Case #54" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 54</system-out>
      </testcase>
      <testcase name="Test Case #83" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 83</system-out>
      </testcase>
      <testcase name="Test Case #32" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 32&lt;40</system-err>
         <error type="error">Error runnining the test: 32&lt;40</error>
      </testcase>
      <testcase name="Test Case #23" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 23&lt;40</system-err>
         <error type="error">Error runnining the test: 23&lt;40</error>
      </testcase>
      <testcase name="Test Case #70" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 70</system-out>
      </testcase>
      <testcase name="Test Case #52" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 52</system-out>
      </testcase>
      <testcase name="Test Case #28" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 28&lt;40</system-err>
         <error type="error">Error runnining the test: 28&lt;40</error>
      </testcase>
      <testcase name="Test Case #2" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 2&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #15" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #79" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 79</system-out>
      </testcase>
      <testcase name="Test Case #52" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 52</system-out>
      </testcase>
      <testcase name="Test Case #42" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 42</system-out>
      </testcase>
      <testcase name="Test Case #10" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 10&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #63" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 63</system-out>
      </testcase>
      <testcase name="Test Case #12" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #58" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 58</system-out>
      </testcase>
      <testcase name="Test Case #18" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #49" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 49</system-out>
      </testcase>
      <testcase name="Test Case #33" time="0" timestamp="2017-08-11 17:49:48">
         <system-err>Error runnining the test: 33&lt;40</system-err>
         <error type="error">Error runnining the test: 33&lt;40</error>
      </testcase>
      <testcase name="Test Case #20" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #10" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 10&lt;10. Skipping Test</skipped>
      </testcase>
      <testcase name="Test Case #15" time="0" timestamp="2017-08-11 17:49:48">
         <skipped message="MATLAB:noSuchMethodOrField" type="skipped">No appropriate method, property, or field 'fail' for class 'junit.TestCase'.</skipped>
      </testcase>
      <testcase name="Test Case #88" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 88</system-out>
      </testcase>
      <testcase name="Test Case #73" time="0" timestamp="2017-08-11 17:49:48">
         <system-out>Test Success! 73</system-out>
      </testcase>
      <testcase name="Test Case #0" time="0" timestamp="2017-08-11 17:49:48">
         <skipped type="skipped">Catastrophic Failure 0&lt;10. Skipping Test</skipped>
      </testcase>
   </testsuite>
</testsuites>