首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matlab xUnit框架测试集设置

Matlab xUnit框架测试集设置
EN

Stack Overflow用户
提问于 2012-03-11 06:30:49
回答 2查看 1K关注 0票数 2

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

EN

回答 2

Stack Overflow用户

发布于 2012-03-11 06:48:40

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

代码语言:javascript
复制
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']);      
票数 1
EN

Stack Overflow用户

发布于 2013-07-10 05:37:06

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9650753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档