This example shows how to use matlab.unittest.TestCase assertable qualifications to test for preconditions.
|On this page…|
Write Test for DocPolynom Class Example
Use the DocPolynom class example, described in A Polynomial Class, to create a test case to write a polynomial to a MAT-file.
Create DocPolynomSaveLoadTest Test Case
Refer to the following DocPolynomSaveLoadTest test case in the subsequent steps in this example. The steps highlight specific code in the testSaveLoad function; the code statements are not intended to be executed outside the context of the class definition file.
To execute the MATLAB® commands in "Run DocPolynomSaveLoadTest Test Case", add the DocPolynomSaveLoadTest.m file to a folder on your MATLAB path.
Structure of testSaveLoad Test
The testSaveLoad function consists of the following phases:
Phase 1: Setup — Create and verify precondition code.
Phase 2: Exercise — Create a DocPolynom object and save it to a MAT-file.
Phase 3: Verify — Test that object was successfully saved.
Phase 4: Teardown — Execute teardown code.
Define Phase 1 Precondition
For this test, use a temporary folder for creating a DocPolynom object. The precondition for continuing with this test is that the following commands execute successfully.
tempFolder = tempname; [success, message] = mkdir(tempFolder);
Test Results of mkdir Function
Use the assertTrue method to test the mkdir success argument for errors. If an assertion occurs, the remainder of the testSaveLoad test method is invalid, and the test is marked Incomplete.
testCase.assertTrue(success, ... Diagnostic.join('Could not create the temporary folder.', ... message));
If the mkdir function fails, MATLAB displays the diagnostic message, Could not create the temporary folder, as well as the contents of the mkdir message argument.
Add Teardown Fixture Code
Creating a temporary folder is setup code, which requires a corresponding call to the rmdir function to restore MATLAB to the original state. Use the addTeardown method to ensure the teardown code executes even when an exception is thrown in the middle of the test method. This makes the test Exception Safe.
Place Teardown Code in Helper Function
Although the addTeardown statement occurs in the same code block as the mkdir setup statement, the cleanUpTemporaryFolder code is executed in phase 4 of the test method.
In the DocPolynomSaveLoadTest test case, the helper function, cleanUpTemporaryFolder, executes the rmdir function. For more information about the cleanUpTemporaryFolder function, see Write Helper Function Using Fatal Assertions.
Define Precondition for Creating Valid MAT-File
A precondition for verifying that the DocPolynom object was correctly saved and loaded is that the MAT-file, DocPolynomFile.mat, was successfully created. The following code in the Phase 2: Exercise block tests this condition. If an assertion occurs, the remainder of the testSaveLoad test method is invalid, and the test is marked Failed and Incomplete.
testCase.assertEqual(exist('DocPolynomFile.mat','file'), 2, ... Diagnostic.join('The mat file was not saved correctly.', ... @() dir(pwd)));
If the file was not created, MATLAB displays the diagnostic message, The mat file was not saved correctly, as well as the contents of the temporary folder.
Run DocPolynomSaveLoadTest Test Case
tc = DocPolynomSaveLoadTest; run(tc);
Running DocPolynomSaveLoadTest . Done DocPolynomSaveLoadTest __________