【问题标题】:How to generate method parameters based on class parameters for Matlab unit testsMatlab单元测试如何根据类参数生成方法参数
【发布时间】:2016-03-03 09:18:54
【问题描述】:

我正在开发的一个程序执行的计算涉及的对象只能有几组可能的值。这些参数集是从目录文件中读取的。

例如,对象代表汽车,目录包含每个模型的值集 {id: (name, color, power, etc.)}。然而,这些目录有很多。

我使用 Matlab 的 unittest 包来测试目录中列出的任何属性组合的计算是否失败。我想使用这个包,因为它提供了一个很好的失败条目列表。我已经有一个测试,它为(硬编码)目录文件生成一个包含所有 id 的元胞数组,并将其用于参数化测试。

现在我需要为每个目录文件创建一个新类。我想将目录文件名设置为类参数,并将其中的条目设置为方法参数(为所有类参数生成),但我找不到将当前类参数传递给本地方法以创建方法参数列表。

我怎样才能做到这一点?

如果它很重要:我使用的是 Matlab 2014a、2015b 或 2016a。

【问题讨论】:

  • 不能每个测试方法循环存储在测试类属性中的配置文件名吗?
  • 通常每当我在测试方法中循环代码时,我认为“这应该使用参数化”。我早上很忙,但我会在今天下午尝试重新连接以尝试回答这个问题。

标签: matlab unit-testing parameterized-unit-test


【解决方案1】:

我有几个想法。

  1. 简短的回答是不,目前不能这样做,因为 TestParameters 被定义为常量属性,因此不能在每个 ClassSetupParameter 值之间更改。

  2. 但是,对我来说,为每个目录创建一个单独的类似乎不是一个坏主意。您的工作流程故障转移在哪里?如果需要,您仍然可以通过将测试基类与您的内容和目录文件的抽象属性一起使用来跨这些文件共享代码。

    classdef CatalogueTest < matlab.unittest.TestCase
        properties(Abstract)
            Catalogue;
        end
        properties(Abstract, TestParameter)
             catalogueValue
        end
    
        methods(Static) 
            function cellOfValues = getValuesFor(catalog)
                % Takes a catalog and returns the values applicable to 
                % that catalog.
            end
        end
    
        methods(Test)
            function testSomething(testCase, catalogueValue)
                % do stuff with the catalogue value
            end
            function testAnotherThing(testCase, catalogueValue)
                % do more stuff with the catalogue value
            end
        end
    
    end
    
    
    
    classdef CarModel1Test < CatalogueTest
    
        properties
            % If the catalog is not needed elsewhere in the test then
            % maybe the Catalogue abstract property is not needed and you
            % only need the abstract TestParameter.
            Catalogue = 'Model1';
        end
        properties(TestParameter)
             % Note call a function that lives next to these tests
             catalogueValue = CatalogueTest.getValuesFor('Model1');
        end
    end
    

    这对你想做的事情有用吗?

  3. 当您说方法参数时,我假设您的意思是“TestParameters”而不是“MethodSetupParameters”,对吗?如果我正确阅读了您的问题,我不确定这是否适用于您的情况,但我想提一下,您可以通过在您的类上创建另一个属性来保存 Test 中的值,从而将 ClassSetupParameters/MethodSetupParameters 中的数据获取到您的测试方法中[方法|类]设置,然后在您的测试方法中引用这些值。 像这样:

    classdef TestMethodUsesSetupParamsTest < matlab.unittest.TestCase
        properties(ClassSetupParameter)
            classParam = {'data'};
        end
        properties
            ThisClassParam
        end
    
        methods(TestClassSetup)
            function storeClassSetupParam(testCase, classParam)
                testCase.ThisClassParam = classParam;       
            end
        end
    
        methods(Test)
            function testSomethingAgainstClassParam(testCase)
                testCase.ThisClassParam
            end
        end
    
    end
    

    当然,在本例中,您应该只使用 TestParameter,但在某些情况下这可能有用。不确定它在这里是否有用。

【讨论】:

  • 我确实为此使用了静态方法。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 2015-05-31
  • 2010-09-07
  • 2011-03-04
  • 1970-01-01
相关资源
最近更新 更多