Main Content

Write Test Using Live Script

This example shows how to test a function that you create by writing a live script, 'TestRightTriLiveScriptExample.mlx'. The example function computes the angles of a right triangle, and you create a live-script-based unit test to test the function.

A live-script-based test must adhere to the following conventions:

  • The name of the test file must start or end with the word 'test', which is case-insensitive. If the file name does not start or end with the word 'test', the tests in the file might be ignored in certain cases.

  • Place each unit test into a separate section of the live-script file. If a section has a heading in the Heading 1 style, the heading becomes the name of the test element. Otherwise, MATLAB assigns a name to the test.

  • Consider how you are running your live-script-based test. If you run the test using the Run buttons in the Live Editor and MATLAB encounters a test failure, then it stops execution of the script and does not run any remaining tests. If you run the live script using the unit testing framework, such as with the runtests function, then if MATLAB encounters a test failure, it still runs remaining tests.

  • When a live script runs as a test, variables defined in one test are not accessible within other tests. Similarly, variables defined in other workspaces are not accessible to the tests.

Outside of this example, in your current MATLAB folder, create a function in a file, rightTri.m. This function takes lengths of two sides of a triangle as input and returns the three angles of the corresponding right triangle. The input sides are the two shorter edges of the triangle, not the hypotenuse.

type rightTri.m
function angles = rightTri(sides)

A = atand(sides(1)/sides(2));
B = atand(sides(2)/sides(1));
hypotenuse = sides(1)/sind(A);
C = asind(hypotenuse*sind(A)/sides(1));

angles = [A B C];

end

Test: Small angle approximation

You can include equations and images in your live script to help document the test. Create the following test for the small angle approximation. Typically, when you compare floating-point values, you specify a tolerance for the comparison.

The rightTri function should return values consistent with the small angle approximation, such that sin(θ)θ.

angles = rightTri([1 1500]);
smallAngleInRadians = (pi/180)*angles(1); % convert to radians
approx = sin(smallAngleInRadians);
assert(abs(approx-smallAngleInRadians) <= 1e-10, 'Problem with small angle approximation')

Test: Sum of Angles

kak=180

You can have multiple assert statements in the same test. However, if the first assertion fails, the MATLAB does not evaluate the remaining statements.

The sum of all angles of the resulting right triangle should always be 180 degrees.

angles = rightTri([7 9]);
assert(sum(angles) == 180)
 
angles = rightTri([4 4]);
assert(sum(angles) == 180)
 
angles = rightTri([2 2*sqrt(3)]);
assert(sum(angles) == 180)

Test: 30-60-90 triangle

Test that the sides of the triangle reduce to 1 and 3. In which case, the angles are 30, 60, and 90.

tol = 1e-10;
angles = rightTri([2 2*sqrt(3)]);
assert(abs(angles(1)-30) <= tol)
assert(abs(angles(2)-60) <= tol)
assert(abs(angles(3)-90) <= tol)

Test: Isosceles Triangles

For isosceles triangles, both of the non-right angles must be 45 degrees; otherwise assert throws an error.

Test that two sides of the triangle are equal. In which case, the corresponding angles are equal.

angles = rightTri([4 4]);
assert(angles(1) == 45)
assert(angles(1) == angles(2))

To run your tests, best practice is to use the testing framework via the runtests function instead of the Run button in the Live Editor. The testing framework provides additional diagnostic information. In the event of a test failure, the framework runs subsequent tests but the Run button in the Live Editor does not. For example, to run this test at the MATLAB command prompt, type result = runtests('TestRightTriLiveScriptExample'). For more information, see runtests.

See Also

|

Related Topics