使用共享脚手架编写测试
您可以使用 matlab.unittest.TestCase
类的 SharedTestFixtures
属性跨测试类共享测试脚手架。当您在一起运行的测试类之间共享一个脚手架时,测试框架为所有测试类设置一次脚手架,并在所有测试类运行后拆解该脚手架。如果您使用每个类的 TestClassSetup
methods
块来指定脚手架,测试框架会在运行每个测试类之前设置脚手架,并在运行后拆解该脚手架。
本示例显示如何使用共享脚手架创建测试。它说明如何共享脚手架来将一个包含源代码的文件夹添加到跨两个测试类的路径中。测试类使用此脚手架来访问测试所需的源代码。
打开示例,使源代码和测试代码在当前文件夹中可用。
openExample("matlab/WriteTestsUsingSharedTestFixturesExample")
DocPolynomTest
类定义
以下代码显示 DocPolynomTest
类定义文件的内容,此文件使用共享脚手架来访问定义 DocPolynom
类的文件夹。有关 DocPolynom
类的详细信息以及查看类代码,请参阅Representing Polynomials with Classes。
classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.PathFixture( ... fullfile("..","fixture_example_source"))}) ... DocPolynomTest < matlab.unittest.TestCase properties TextToDisplay = "Equation under test: " end methods (Test) function testConstructor(testCase) p = DocPolynom([1 0 1]); testCase.verifyClass(p,?DocPolynom) end function testAddition(testCase) p1 = DocPolynom([1 0 1]); p2 = DocPolynom([5 2]); actual = p1 + p2; expected = DocPolynom([1 5 3]); diagnostic = [testCase.TextToDisplay ... "(x^2 + 1) + (5*x + 2) = x^2 + 5*x + 3"]; testCase.verifyEqual(actual,expected,diagnostic) end function testMultiplication(testCase) p1 = DocPolynom([1 0 3]); p2 = DocPolynom([5 2]); actual = p1 * p2; expected = DocPolynom([5 2 15 6]); diagnostic = [testCase.TextToDisplay ... "(x^2 + 3) * (5*x + 2) = 5*x^3 + 2*x^2 + 15*x + 6"]; testCase.verifyEqual(actual,expected,diagnostic) end end end
BankAccountTest
类定义
以下代码显示 BankAccountTest
类定义文件的内容,此文件使用共享脚手架来访问定义 BankAccount
类的文件夹。有关 BankAccount
类的详细信息以及查看类代码,请参阅开发协同工作的类。
classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.PathFixture( ... fullfile("..","fixture_example_source"))}) ... BankAccountTest < matlab.unittest.TestCase methods (Test) function testConstructor(testCase) b = BankAccount(1234,100); testCase.verifyEqual(b.AccountNumber,1234, ... "Constructor must correctly set account number.") testCase.verifyEqual(b.AccountBalance,100, ... "Constructor must correctly set account balance.") end function testConstructorNotEnoughInputs(testCase) import matlab.unittest.constraints.Throws testCase.verifyThat(@()BankAccount,Throws("MATLAB:minrhs")) end function testDeposit(testCase) b = BankAccount(1234,100); b.deposit(25) testCase.verifyEqual(b.AccountBalance,125) end function testWithdraw(testCase) b = BankAccount(1234,100); b.withdraw(25) testCase.verifyEqual(b.AccountBalance,75) end function testNotifyInsufficientFunds(testCase) callbackExecuted = false; function testCallback(~,~) callbackExecuted = true; end b = BankAccount(1234, 100); b.addlistener("InsufficientFunds",@testCallback); b.withdraw(50) testCase.assertFalse(callbackExecuted, ... "The callback should not have executed yet.") b.withdraw(60) testCase.verifyTrue(callbackExecuted, ... "The listener callback should have fired.") end end end
运行测试
在当前文件夹及其子文件夹中运行测试。测试框架设置共享测试脚手架,运行 BankAccountTest
和 DocPolynomTest
类中的测试,并在运行测试后拆解脚手架。在此示例中,所有测试都通过。
runtests("IncludeSubfolders",true);
Setting up PathFixture Done setting up PathFixture: Added 'C:\work\WriteTestsUsingSharedTestFixturesExample\fixture_example_source' to the path. __________ Running BankAccountTest ..... Done BankAccountTest __________ Running DocPolynomTest ... Done DocPolynomTest __________ Tearing down PathFixture Done tearing down PathFixture: Restored the path to its original state. __________
另请参阅
matlab.unittest.TestCase
| matlab.unittest.fixtures.Fixture
| matlab.unittest.fixtures.PathFixture