2012-03-10 74 views

回答

1

根據Matlab xUnit文檔:您可以1)從TestCase繼承或2)使用子功能。使用子功能的示例如下所示。您只能傳遞一個變量,因此您必須將它們加載到結構中,如下所示。你可以把更多的子功能,在年底,但要確保在啓動或使用「設置」,「測試」,或「拆除」

function test_suite = testjkcmInputParser  
    initTestSuite; 

    function d = setup 
    d.file='garbagelog.log'; 
    d.fid = fopen(d.file, 'w'); 
    d.o = jkcmInputParser(d.fid); 

    function teardown(d) 
    delete(d.o); 
    fclose(d.fid); 
    delete(d.file); 

    function testConstructorNoInput(d) 
    %constructor without fid 
    delete(d.o); 
    d.o = jkcmInputParser();  
    assertEqual(isa(d.o,'jkcmInputParser'), true, 'not a jkcmInputParser'); 
    assertEqual(isa(d.o,'inputParser'), true, 'not an inputParser'); 

    function testConstructorWithInput(d) 
    %constructor with fid  
    assertEqual(isa(d.o,'jkcmInputParser'), true, 'not a jkcmInputParser'); 
    assertEqual(isa(d.o,'inputParser'), true, 'not an inputParser'); 
    initializejkcmParser(d.o);  
    s = d.o.printHelp();  
    assertEqual(s, correctPrintHelp(), 'output of printHelp does not match expected.'); 

    function outP = initializejkcmParser(o) 
    %setup jkcmInputParser 
    o.addRequired('val1_noComment', @isnumeric); 
    o.addRequired('val2', @isnumeric, 'comment');  
    o.addOptional('val3_noComment',3, @isnumeric); 
    o.addOptional('val4',15, @isnumeric, 'another great comment!');  
    o.addParamValue('val5_noComment', 45, @isnumeric); 
    o.addParamValue('val6', 45, @isnumeric, 'This is the greatest comment'); 
    outP = o; 

    function outP = correctPrintHelp() 
    outP = sprintf(... 
     ['val1_noComment: Req : \n',... 
     'val2: Req : comment\n',... 
     'val3_noComment: Opt : \n',... 
     'val4: Opt : another great comment!\n',... 
     'val5_noComment: Param : \n',... 
     'val6: Param : This is the greatest comment\n']);  
+0

設置功能在每個測試之前被調用。我需要一種方法在所有測試之前加載一次,並在ALL測試後清理。 – 2012-03-10 23:05:49

+0

全局變量會起作用嗎?如果不是,那麼我想你將不得不使用類框架並從testSuite繼承並進行一些更改。 – PopcornKing 2012-03-11 00:21:33

+0

也許'持久性'變量會比'global'好。 – 2012-03-12 10:30:13