【问题标题】:Matlab xUnit Framework test suite setupMatlab xUnit Framework 测试套件设置
【发布时间】:2013-07-10 05:37:06
【问题描述】:

如何为每个测试套件初始化一次变量,以便它们在每个测试中可见?例如,可以加载一些文件,每个测试都需要这些文件。

【问题讨论】:

    标签: unit-testing matlab xunit


    【解决方案1】:

    根据 Matlab xUnit 文档:您可以 1)从 TestCase 继承或 2)使用子函数。使用子函数的示例如下所示。您只能传递一个变量,因此您必须将它们加载到结构中,如下所示。您可以将其他子函数放在末尾,但请确保以“setup”、“test”或“teardown”开始或结束它们的名称

    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']);      
    

    【讨论】:

    • 设置函数在每次测试前被调用。我需要一种在所有测试之前加载一次并在所有测试之后进行清理的方法。
    • 全局变量有用吗?如果不是,那么我想您将不得不使用类框架并从 testSuite 继承并进行一些更改。
    • 也许persistent 变量会比global 更好。
    【解决方案2】:

    在 R2013a 中,MATLAB 包含一个功能齐全的测试框架。它包含的功能之一是能够为整个类定义设置/拆卸代码。

    http://www.mathworks.com/help/matlab/matlab-unit-test-framework.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多