MATLAB 单元测试:共享夹具、自定义夹具与参数化测试
1. 使用共享夹具编写测试
在创建测试时,我们可以使用共享夹具来提高测试效率。通过 TestCase 类的 SharedTestFixtures 属性,我们可以在不同的测试类之间共享测试夹具。以下是具体的操作步骤:
- 创建测试类 :在当前工作文件夹的子目录中创建多个测试类。这里我们以测试
DocPolynom类和BankAccount类为例。由于这两个类都需要将相同的目录添加到 MATLAB 路径中,所以我们可以使用共享夹具。 - 为
DocPolynom类创建测试 :
classdef (SharedTestFixtures={matlab.unittest.fixtures.PathFixture( ...
fullfile(matlabroot,'help','techdoc','matlab_oop','examples'))}) ...
DocPolynomTest < matlab.unittest.TestCase
% Tests the DocPolynom class.
properties
msgEqn = 'Equation under test: ';
end
methods (Te
超级会员免费看
订阅专栏 解锁全文
52

被折叠的 条评论
为什么被折叠?



