unit-testing - Matlab xUnit 框架测试套件设置

标签 unit-testing matlab xunit

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

最佳答案

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

关于unit-testing - Matlab xUnit 框架测试套件设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9650753/

相关文章:

javascript - 用 Angular Testing 服务的理想方法是什么?

unit-testing - 用于测试的内存文件

matlab - 创建自定义且可重用的用户界面控件

matlab - 如何将变量附加到 .mat 文件?

regex - 如何使用Matlab/Octave regexprep(正则表达式替换)在文件名扩展名前添加后缀

c# - 使用 xUnit 引用作为 NuGet 包部署项目

jenkins - Jenkins xUnit 插件的控制参数

javascript - 如何通过管道将代码和单元测试传递给 npm test?

c# - StoryQ When() 调用任务 C#

c# - 无法使用 Xunit 测试端点 - StatusCode : 400, ReasonPhrase: 'Bad Request'