Contents

Create axesPropertiesTest File

Create a file containing the main function that tests figure axes properties and include two test functions. One function verifies that the x-axis limits are correct, and the other one verifies that the face color of a surface is correct.

In a folder on your MATLAB path, create axesPropertiesTest.m. In the main function of this file, have functiontests create an array of tests from each local function in axesPropertiesTest.m with a call to the localfunctions function.

Create File Fixture Functions

File fixture functions are setup and teardown code that runs a single time in your test file. These fixtures are shared across the test file. In this example, the file fixture functions create a temporary folder and set it as the current working folder. They also create and save a new figure for testing. After tests are complete, the framework reinstates the original working folder and deletes the temporary folder and saved figure.

In this example, a helper function creates a simple figure — a red cylinder. In a more realistic scenario, this code is part of the product under test and is computationally expensive, thus motivating the intent to create the figure only once and to load independent copies of the result for each test function. For this example, however, you want to create this helper function as a local function to axesPropertiesTest. Note that the test array does not include the function because its name does not start or end with ‘test’.

Write a helper function that creates a simple red cylinder and add it as a local function to axesPropertiesTest.

You must name the setup and teardown functions of a file test fixture setupOnce and teardownOnce, respectively. These functions take a single input argument, testCase, into which the test framework automatically passes a function test case object. This test case object contains a TestData structure that allows data to pass between setup, test, and teardown functions. In this example, the TestData structure uses assigned fields to store the original path, the temporary folder name, and the figure file name.

Create the setup and teardown functions as a local functions to axesPropertiesTest.

Create Fresh Fixture Functions

Fresh fixtures are function level setup and teardown code that runs before and after each test function in your file. In this example, the functions open the saved figure and find the handles. After testing, the framework closes the figure.

You must name fresh fixture functions setup and teardown, respectively. Similar to the file fixture functions, these functions take a single input argument, testCase. In this example, these functions create a new field in the TestData structure that includes handles to the figure and to the axes. This allows information to pass between setup, test, and teardown functions.

Create the setup and teardown functions as a local functions to axesPropertiesTest. Open the saved figure for each test to ensure test independence.

In addition to custom setup and teardown code, the Unit Testing Framework provides some classes for creating fixtures. For more information see matlab.unittest.fixtures.

Create Test Functions

Each test is a local function that follows the naming convention of having ‘test’ at the beginning or end of the function name. The test array does not include local functions that do not follow this convention. Similar to setup and teardown functions, individual test functions must accept a single input argument, testCase. Use this test case object for verifications, assertions, assumptions, and fatal assertions functions.

The testDefaultXLim function test verifies that the x-axis limits are large enough to display the cylinder. The lower limit needs to be less than -10, and the upper limit needs to be greater than 10. These values come from the figure generated in the helper function — a cylinder with a 10 unit radius centered on the origin. This test function opens the figure created and saved in the setupOnce function, queries the axes limit, and verifies the limits are correct. The qualification functions, verifyLessThanOrEqual and verifyGreaterThanOrEqual, takes the test case, the actual value, the expected value, and optional diagnostic information to display in the case of failure as inputs.

Create the testDefaultXLim function as local function to axesPropertiesTest.

The surfaceColorTest function accesses the figure that you created and saved in the setupOnce function. surfaceColorTest queries the face color of the cylinder and verifies that it is red. The color red has an RGB value of [1 0 0]. The qualification function, verifyEqual, takes as inputs the test case, the actual value, the expected value, and optional diagnostic information to display in the case of failure. Typically when using verifyEqual on floating point-values, you specify a tolerance for the comparison. For more information, see matlab.unittest.constraints.

Create the surfaceColorTest function as local function to axesPropertiesTest.

MATLAB and Simulink are registered trademarks of The MathWorks, Inc. Please see www.mathworks.com/trademarks for a list of other trademarks owned by The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective owners.