This example shows how to use matlab.unittest.TestCase verifiable qualifications in a test method. Verifications produce and record failures without throwing an exception, meaning the currently running test runs to completion.
|On this page…|
DocPolynom Class Example
The DocPolynom class example, described in A Polynomial Class, implements a class to represent polynomials in the MATLAB® language. This example creates a test case to verify arithmetic operations on objects of the DocPolynom class.
DocPolynomTest Test Case
Refer to the following DocPolynomTest test case in the subsequent steps in this example, which highlight specific functions in the file.
To execute the MATLAB commands in this example, add the DocPolynomTest.m file to a folder on your MATLAB path.
Write Test to Verify Constructor
Create a function, testConstructor, using the verifyClass method to test the DocPolynom class constructor.
function testConstructor(testCase) p = DocPolynom([1, 0, 1]); testCase.verifyClass(p, ?DocPolynom); end
Write Tests to Verify Operations
In the testAddition function, use the verifyEqual method to test the equation (x^2 + 1) + (5*x + 2) = x^2 + 5*x + 3. The verifyEqual method includes this equation in the diagnostic argument.
function testAddition(testCase) p1 = DocPolynom([1, 0, 1]); p2 = DocPolynom([5, 2]); actual = p1 + p2; expected = DocPolynom([1, 5, 3]); msg = [testCase.msgEqn,... '(x^2 + 1) + (5*x + 2) = x^2 + 5*x + 3']; testCase.verifyEqual(actual, expected, msg); end
The function, testMultiplication, tests multiplication operations.
Run Test Case
Run the tests in the DocPolynomTest test case.
tc = DocPolynomTest; ts = matlab.unittest.TestSuite.fromClass(?DocPolynomTest); res = run(ts);
Running DocPolynomTest ... Done DocPolynomTest __________
All tests passed.