使用共享脚手架编写测试
本示例显示如何使用共享脚手架创建测试。您可以使用 TestCase
类的 SharedTestFixtures
属性跨测试类共享测试脚手架。为了举例说明此属性,请在您的当前工作文件夹的子目录中创建多个测试类。测试方法仅在高级别显示。
此示例中使用的两个测试类测试 DocPolynom
类和 BankAccount
类。您可以在 MATLAB® 中访问这两个类,但您必须将其添加到 MATLAB 路径。路径脚手架将目录添加到当前路径,运行测试并从该路径中删除该目录。因为两个类都需要添加到路径,所以这些测试使用共享脚手架。
为 DocPolynom 类创建测试
为 DocPolynom
类创建一个测试文件。通过为 TestCase
指定 SharedTestFixtures
属性并传入 PathFixture
来创建共享脚手架。
为 BankAccount 类创建测试
针对 BankAccount
类创建一个测试文件。通过指定 TestCase
的 SharedTestFixtures
属性并传入 PathFixture
来创建共享脚手架。
编译测试套件
类 DocPolynomTest.m
和 BankAccountTest.m
在您的工作目录中。从您的当前工作目录创建一个测试套件。如果您有其他测试,它们会在您使用 TestSuite.fromFolder
方法时包括在该套件中。在命令提示符下创建测试套件。
import matlab.unittest.TestSuite;
suiteFolder = TestSuite.fromFolder(pwd);
运行测试
在命令提示符下,运行测试套件中的测试。
result = run(suiteFolder);
Setting up PathFixture. Description: Adds 'C:\Program Files\MATLAB\R2013b\help\techdoc\matlab_oop\examples' to the path. __________ Running BankAccountTest ..... Done BankAccountTest __________ Running DocPolynomTest ... Done DocPolynomTest __________ Tearing down PathFixture. Description: Restores the path to its previous state. __________
测试框架设置测试脚手架,在每个文件中运行所有测试,然后拆解脚手架。如果使用 TestClassSetup
方法设置和拆解路径脚手架,将设置并拆解该脚手架两次 - 每个测试文件各一次。
另请参阅
matlab.unittest.TestCase
| matlab.unittest.fixtures
| matlab.unittest.fixtures.PathFixture