主要内容

创建基本参数化测试

这个例子展示了如何创建一个测试函数的输出参数化测试的价值,类,和大小。

创建功能测试

在当前文件夹,文件中创建一个函数sierpinski.m。这个函数返回一个代表Sierpinski地毯的图像分形矩阵。需要作为输入分形级和一个可选的数据类型。

函数地毯= sierpinski(水平,类名)如果输入参数个数classname = = = 1“单一”;结束msize = 3 ^水平;地毯的= (msize,类名);cutCarpet (1, - 1, msize水平)%开始递归函数cutCarpet (x, y s cl)如果cl党卫军= s / 3;%定义小尺寸lx = 0:2ly = 0:2如果lx = = 1 & & ly = = 1%去除中心广场地毯(x + ss: x + 2 * ss-1, y + ss: y + 2 * ss-1) = 0;其他的%递归cutCarpet (x + lx * ss, y + ly * ss党卫军,cl-1)结束结束结束结束结束结束

创建TestCarpet测试类

在一个文件在当前文件夹,创建TestCarpet类测试sierpinski函数。定义属性用于参数化测试属性块的TestParameter属性。

classdefTestCarpet < matlab.unittest.TestCase属性(TestParameter)类型= {“单一”,“双”,“uint16”};水平=结构(“小”2,“媒介”4“大”6);一边=结构(“小”9“媒介”,81,“大”,729);结束结束

类型属性包含您想要测试的不同数据类型。的水平属性包含您想要测试的不同分形水平。的一边属性包含的行数和列在Sierpinski地毯矩阵和对应水平财产。为每个参数,提供有意义的名字水平一边被定义为结构。

定义测试方法阻止

在一个方法块的测试属性,定义三种测试方法:

  • testRemainPixels方法测试的输出sierpinski函数通过验证非零像素的数量是相同的为一个特定的预期水平。该方法使用水平属性,因此在三个测试结果为每个值的元素水平

  • testClass测试输出的类方法sierpinski函数的每个组合类型水平参数值(即,详尽的参数组合)。结果在9个测试元素的方法。

  • testDefaultL1Output方法不使用TestParameter因此,财产和参数化。方法验证一级矩阵是否包含预期的值。因为测试方法没有参数化,结果在一个测试元素。

classdefTestCarpet < matlab.unittest.TestCase属性(TestParameter)类型= {“单一”,“双”,“uint16”};水平=结构(“小”2,“媒介”4“大”6);一边=结构(“小”9“媒介”,81,“大”,729);结束方法(测试)函数testRemainPixels (testCase,级别)expPixelCount = 8 ^水平;actPixels =找到(sierpinski(级));testCase.verifyNumElements (actPixels expPixelCount)结束函数testClass (testCase、类型、级别)testCase.verifyClass (sierpinski(级别、类型),类型)结束函数testDefaultL1Output (testCase) exp =单([1 1 1;1 0 1;1 1 1]);testCase.verifyEqual (sierpinski(1)、实验)结束结束结束

定义测试方法与ParameterCombination块属性

定义testNumel方法以确保返回的矩阵sierpinski函数的正确数量的元素。设置ParameterCombination属性的方法“顺序”。因为水平一边每个属性指定三个参数值,testNumel方法调用三次,一次为每个“小”,“媒介”,“大”值。

classdefTestCarpet < matlab.unittest.TestCase属性(TestParameter)类型= {“单一”,“双”,“uint16”};水平=结构(“小”2,“媒介”4“大”6);一边=结构(“小”9“媒介”,81,“大”,729);结束方法(测试)函数testRemainPixels (testCase,级别)expPixelCount = 8 ^水平;actPixels =找到(sierpinski(级));testCase.verifyNumElements (actPixels expPixelCount)结束函数testClass (testCase、类型、级别)testCase.verifyClass (sierpinski(级别、类型),类型)结束函数testDefaultL1Output (testCase) exp =单([1 1 1;1 0 1;1 1 1]);testCase.verifyEqual (sierpinski(1)、实验)结束结束方法(测试,ParameterCombination =“顺序”)函数testNumel (testCase、水平、侧)导入matlab.unittest.constraints.HasElementCounttestCase.verifyThat (sierpinski(高度),HasElementCount(边^ 2))结束结束结束

运行所有测试

在命令提示符处,创建一套TestCarpet.m。套件有16个测试元素。MATLAB®包括参数化信息在套件的名称的元素。

套件= matlab.unittest.TestSuite.fromFile (“TestCarpet.m”);{suite.Name}’
ans = 16×1单元阵列{' TestCarpet / testNumel(水平= =一面小)}{' TestCarpet / testNumel(=中等水平,一边=媒介)}{' TestCarpet / testNumel(水平=大,一边=大)}{' TestCarpet / testRemainPixels(要求等级=小)'}{' TestCarpet / testRemainPixels(=中等水平)}{' TestCarpet / testRemainPixels(水平=大)}{' TestCarpet / testClass (type =单身,级别=小)'}{' TestCarpet / testClass (type =单一、水平=媒介)}{' TestCarpet / testClass (type =单一、水平=大)}{' TestCarpet / testClass (type =双级=小)'}{' TestCarpet / testClass (type =双级=媒介)}{' TestCarpet / testClass (type =双级=大)}{' TestCarpet / testClass (type = uint16水平=小)}{' TestCarpet / testClass (type = uint16水平=媒介)}{' TestCarpet / testClass (type = uint16水平=大)}{' TestCarpet / testDefaultL1Output '}

运行测试。

suite.run
运行TestCarpet ..........……完成TestCarpet __________ ans = 1×16 TestResult数组属性:名称传递失败的完整细节总数持续时间:16了,失败了,0不完整,测试时间2.459秒。

运行测试与级别属性命名为“小”

使用selectIf的方法TestSuite选择使用一个特定的参数化的测试元素。选择所有测试使用参数名的元素“小”水平参数化属性列表。过滤套件有五个元素。

s1 = suite.selectIf (“ParameterName”,“小”);{s1.Name}’
ans = 5×1单元阵列{' TestCarpet / testNumel(水平= =一面小)}{' TestCarpet / testRemainPixels(要求等级=小)'}{' TestCarpet / testClass (type =单身,级别=小)'}{' TestCarpet / testClass (type =双级=小)'}{' TestCarpet / testClass (type = uint16水平=小)}

过滤测试套件运行。

s1.run;
运行TestCarpet .....完成TestCarpet __________

或者,您可以创建相同的测试套件中直接使用fromFile的方法TestSuite

进口matlab.unittest.selectors.HasParameters1 = matlab.unittest.TestSuite.fromFile (“TestCarpet.m”,HasParameter (“名字”,“小”));

另请参阅

||

相关的话题