Write Tests Using Shared Fixtures
You can share test fixtures across test classes using the SharedTestFixtures
attribute of the matlab.unittest.TestCase
class. When you share a fixture across test classes that run together, the testing framework sets up the fixture once for all the test classes and tears it down after all the test classes run. If you specify the fixture using the TestClassSetup
methods
block of each class instead, the testing framework sets up the fixture before and tears it down after running each test class.
This example shows how to use shared fixtures when creating tests. It shows how to share a fixture for adding a folder containing source code to the path across two test classes. The test classes use this fixture to access the source code required by the tests.
Open the example to make the source and test code available in your current folder.
openExample("matlab/WriteTestsUsingSharedTestFixturesExample")
DocPolynomTest
Class Definition
This code shows the contents of the DocPolynomTest
class definition file, which uses a shared fixture to access the folder defining the DocPolynom
class. For more information about the DocPolynom
class and to view the class code, see 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
Class Definition
This code shows the contents of the BankAccountTest
class definition file, which uses a shared fixture to access the folder defining the BankAccount
class. For more information about the BankAccount
class and to view the class code, see Developing Classes That Work Together.
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
Run the Tests
Run the tests in your current folder and its subfolders. The testing framework sets up the shared test fixture, runs the tests in the BankAccountTest
and DocPolynomTest
classes, and tears down the fixture after running the tests. In this example, all of the tests pass.
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. __________
See Also
matlab.unittest.TestCase
| matlab.unittest.fixtures.Fixture
| matlab.unittest.fixtures.PathFixture