2016-03-03 32 views
3

我正在处理的程序执行的计算涉及只能有几组可能的值的对象。这些参数集是从目录文件中读取的。如何基于Matlab单元测试的类参数生成方法参数

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

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

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

我该如何做这项工作?

万一它很重要:我使用Matlab 2014a,2015b或2016a。

+0

每个测试方法都不能遍历存储在测试类属性中的配置文件名吗? – Jonas

+0

通常,任何时候我循环测试方法内的代码,我认为“这应该使用参数化。”我有一个忙碌的早晨,但我会尝试在今天下午连线回答这个问题。 –

回答

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”正确吗?如果我正在阅读你的问题,我不确定这适用于你的情况,但我想提到你可以从你的ClassSetupParameters/MethodSetupParameters中获取数据到你的测试方法中,方法是在你的类上创建另一个属性来保存Test中的值[Method | Class]安装并在Test方法中引用这些值。 像这样:

    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,但可能会有某些情况下,这可能是有用的。不知道它是否有用。

+0

我对此使用了静态方法。非常感谢你! – Tim

相关问题