MATLAB Examples

Example 04

Generate test cases and add them to a test suite in a loop.

test_suite = junit.TestSuite();
test_suite.name='Example 04';
% Determine test result randomly.
for r = rand(1, 10)
    test_case_name = matlab_ipsum('Sentences', 1, 'Paragraphs', 1, 'Words', 2, 'WordsStd', 1);
    test_case = junit.TestCase(test_case_name);
    % Filler messages.
    message = matlab_ipsum('Sentences', 1, 'Paragraphs', 1, 'Words', 2, 'WordsStd', 1);
    out = matlab_ipsum('Sentences', 2, 'Paragraphs', 1);
    if r<0.5 % Passes

    elseif r<0.6 % Failure
        test_case.failure(sprintf('Failure: %s!', message), sprintf('!! Error!!\n'));
    elseif r<0.8 % Error
        test_case.error(sprintf('Error: %s!', message), sprintf('!! Error!!\n'));
    else % Skipped
        test_case.skipped(sprintf('Skipped: %s!', message), sprintf('!! Error!!\n'));
    end
    % Random elapsed time.
    test_case.time=r*100+10;
    % Filler stdout.
    test_case.stdout = matlab_ipsum('Sentences', 2, 'Paragraphs', 1);
    test_suite.append(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="2" failures="0" skipped="2" tests="10" time="525.25">
   <testsuite errors="2" failures="0" hostname="USGALWHP6047580" id="0" name="Example 04" package="matlab" skipped="2" tests="10" timestamp="2017-08-11 17:49:49">
      <testcase name="Tepmaarm ndreao." time="82.9978" timestamp="2017-08-11 17:49:48">
         <system-out>Nlpdi ea uasuahw ebiary.  Ylwniu swfnepeeo oofh aethedu ssitat hcngyin eedwn.  Oeetoe gkoeiho enetliet easelis liaeeei eee idurhh enrnilt.</system-out>
         <error message="Error: Alr ahhlihs.!" type="error">!! Error!!
</error>
      </testcase>
      <testcase name="Auwe orw." time="44.6375" timestamp="2017-08-11 17:49:48">
         <system-out>Laotiavt uehea haesir aiy bmeoeeh dtbstw.</system-out>
      </testcase>
      <testcase name="Sirnot.  Hrthad." time="75.2205" timestamp="2017-08-11 17:49:48">
         <system-out>Bseaet linbo soibrmnu lene aeeeoo cscsp o eachpoao u staeuotk pray dotpioto.  Asairn rete neoyefo eeiii iaaa wsetcr aeyema uhlee uhsnu hfol urhaioga.</system-out>
         <error message="Error: Doe reruaatd.!" type="error">!! Error!!
</error>
      </testcase>
      <testcase name="U iupenaig hrctpr." time="58.1757" timestamp="2017-08-11 17:49:48">
         <system-out>Irf oltapon aierdo nb dcnse eusratlia eciro.  O, yaeitriyg ccfeg ia mtsi eetcre oodr airh.</system-out>
      </testcase>
      <testcase name="Emeses." time="48.2594" timestamp="2017-08-11 17:49:48">
         <system-out>A ncserrwiic rogu thttornf hvdnete ctleo.</system-out>
      </testcase>
      <testcase name="Tdoeltu ereop oaoeit." time="42.6886" timestamp="2017-08-11 17:49:48">
         <system-out>Hao bnrhaa dutaefas eriaene oiept iuon rmesei irnusre.  Eeclltct aeeac onassao aeeem los gtnyon.  Cerrts oeaf iab inbee a ei opntyeee ooi otopgsn atamn euohh.</system-out>
      </testcase>
      <testcase name="Huyn?" time="20.304" timestamp="2017-08-11 17:49:48">
         <system-out>Euwbhtnl it siekoe, eaaimeg qyn iwg niotudsosf?</system-out>
      </testcase>
      <testcase name="Oiesaar aaa wiaa gouuea.  Eniiipety." time="47.0412" timestamp="2017-08-11 17:49:48">
         <system-out>Notna urdua eodalia asnih enm ehatrytoeam icuwstoa.  Nea eisieo duhole idinyeoonaoi eitehoe, iheccl ttvssy sef.</system-out>
      </testcase>
      <testcase name="Ionryna ndariadtce." time="47.7442" timestamp="2017-08-11 17:49:48">
         <system-out>Oahha serimera thfio shlaol aterotu ewsk drgh tehsg.</system-out>
      </testcase>
      <testcase name="Lmblw oniarmfi." time="58.1798" timestamp="2017-08-11 17:49:48">
         <system-out>Anehlato aotih, yoithfaa castzl, oeaves yirntorh tesvide idubh oegre.  Du doog soaeitn enaoaa vouetst su amlr asstlhogr ilidae.  Abgohw aaam dlturila sgsrtmr.</system-out>
      </testcase>
   </testsuite>
</testsuites>