如何根据Matlab单元测试的class参数生成方法参数
How to generate method parameters based on class parameters for Matlab unit tests
我正在处理的一个程序执行的计算涉及的对象只能有几组可能的值。这些参数集是从目录文件中读取的。
举个例子,对象代表汽车,目录包含每个模型的值集 {id:(名称、颜色、功率等)}。不过这样的目录很多。
我使用 Matlab 的 unittest 包来测试目录中列出的任何 属性 组合的计算是否失败。我想使用这个包,因为它提供了一个很好的失败条目列表。我已经有一个测试可以为(硬编码)目录文件生成一个包含所有 ID 的元胞数组,并将其用于参数化测试。
现在我需要为每个目录文件创建一个新的 class。我想将目录文件名设置为 class 参数,并将其中的条目设置为方法参数(为所有 class 参数生成),但我找不到传递当前 class 将参数传给本地方法,用于创建方法参数列表。
我怎样才能完成这项工作?
以防重要:我使用的是 Matlab 2014a、2015b 或 2016a。
我有一些想法。
简而言之,目前无法完成此操作,因为 TestParameters 被定义为常量属性,因此无法在每个 ClassSetupParameter 值之间进行更改。
但是,对我来说,为每个目录创建一个单独的 class 似乎不是一个坏主意。该工作流在哪里失败?如果需要,您仍然可以通过对您的内容使用测试基础 class 和目录文件的摘要 属性 来在这些文件之间共享代码。
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
这对您尝试做的事情有用吗?
当你说方法参数时,我假设你的意思是 "TestParameters" 而不是 "MethodSetupParameters" 对吗?如果我没看错你的问题,我不确定这是否适用于你的情况,但我想提一下,你可以通过在 属性 上创建另一个 属性 来将数据从 ClassSetupParameters/MethodSetupParameters =35=] 来保存 Test[Method|Class]Setup 中的值,然后在 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,但在某些情况下这可能会有用。不知道在这里有没有用。
我正在处理的一个程序执行的计算涉及的对象只能有几组可能的值。这些参数集是从目录文件中读取的。
举个例子,对象代表汽车,目录包含每个模型的值集 {id:(名称、颜色、功率等)}。不过这样的目录很多。
我使用 Matlab 的 unittest 包来测试目录中列出的任何 属性 组合的计算是否失败。我想使用这个包,因为它提供了一个很好的失败条目列表。我已经有一个测试可以为(硬编码)目录文件生成一个包含所有 ID 的元胞数组,并将其用于参数化测试。
现在我需要为每个目录文件创建一个新的 class。我想将目录文件名设置为 class 参数,并将其中的条目设置为方法参数(为所有 class 参数生成),但我找不到传递当前 class 将参数传给本地方法,用于创建方法参数列表。
我怎样才能完成这项工作?
以防重要:我使用的是 Matlab 2014a、2015b 或 2016a。
我有一些想法。
简而言之,目前无法完成此操作,因为 TestParameters 被定义为常量属性,因此无法在每个 ClassSetupParameter 值之间进行更改。
但是,对我来说,为每个目录创建一个单独的 class 似乎不是一个坏主意。该工作流在哪里失败?如果需要,您仍然可以通过对您的内容使用测试基础 class 和目录文件的摘要 属性 来在这些文件之间共享代码。
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
这对您尝试做的事情有用吗?
当你说方法参数时,我假设你的意思是 "TestParameters" 而不是 "MethodSetupParameters" 对吗?如果我没看错你的问题,我不确定这是否适用于你的情况,但我想提一下,你可以通过在 属性 上创建另一个 属性 来将数据从 ClassSetupParameters/MethodSetupParameters =35=] 来保存 Test[Method|Class]Setup 中的值,然后在 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,但在某些情况下这可能会有用。不知道在这里有没有用。