In MATLAB R2021b, various types of unit tests, such as script, function, or class-based tests, can be utilized to validate code, all of which fall under the Testing Frameworks in MATLAB. More detailed information is available here: https://www.mathworks.com/help/releases/R2021b/matlab/matlab-unit-test-framework.html.
- Script-based unit tests: These involve writing tests in separate sections of a script file to perform basic qualifications, access diagnostics, and customize test runs using a TestRunner object.
- Function-based unit tests: Tests are written as local functions in a test file, following the xUnit philosophy, and include advanced features like constraints, tolerances, and diagnostics.
- Class-based unit tests: Tests are written as methods in a class file, offering full framework access, shared fixtures, parameterized tests, and content reuse.

Here's how a function based unit test might look like for the first two tests in the query:
function tests = testAudioAnalysis
tests = functiontests(localfunctions);
end
function testSampleOccurrence(testCase)
Fs = 16000;
r = audioread('sample-15s.wav');
t = 0:1/Fs:(length(r)-1)/Fs;
% Verify that the 12000th sample occurs at 0.75 seconds
sampleIndex = 12000;
expectedTime = 0.75; % seconds
actualTime = t(sampleIndex);
testCase.verifyEqual(actualTime, expectedTime, 'AbsTol', 1e-4);
end
function testSpectralSpacing(testCase)
Fs = 16000;
r = audioread('sample-15s.wav');
n = length(r);
% Calculate frequency resolution
freqResolution = Fs / n;
% Verify spectral spacing
expectedSpacing = 0.0189;
testCase.verifyEqual(expectedSpacing, freqResolution, 'AbsTol', 1e-4);
end
For more details on these tests, the following pages can be referenced:
