Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in MATLAB and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic MATLAB:

% File: tests/AdderTest.m
classdef AdderTest < matlab.unittest.TestCase
    methods (Test)
        function addsPositives(tc)
            tc.verifyEqual(2 + 3, 5);
        end
        function tolerantFloat(tc)
            tc.verifyEqual(0.1+0.2, 0.3, "AbsTol", 1e-12);
        end
    end
end
% run with:  runtests('tests')

MATLAB note: Use verifyEqual with an explicit AbsTol/RelTol for floating-point comparisons, and prefer verify* (logs and continues) over assert* (aborts the test) so one method can report multiple failures.

Try it yourself

Exercise: In MATLAB, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in MATLAB. Mark this lesson complete and continue to the next one.