Generate Unit Tests Using MATLAB Copilot
If you have a MATLAB® Copilot license, you can generate unit tests for a source code file using MATLAB Copilot. The generated tests execute the source code and test the output by using qualification methods. For more information about qualification methods, see Table of Verifications, Assertions, and Other Qualifications. You do not need to specify input or output values to run the tests.
Tip
MATLAB Copilot attempts to generate test code that qualifies the source code. However, the generated tests might contain errors or incorrect code. Validate the generated tests before use, and ensure that the tests exercise the functional behavior that you want to test. You can use MATLAB Copilot to explain the generated tests by selecting the test code in the Editor and, on the Editor tab, selecting Copilot > Explain Code.
You can also generate basic unit tests for the current file, generate unit tests from commands that you executed, and generate basic equivalence tests for the current function file. For more information, see Generate Tests for MATLAB Source Code.
Generate Tests
To generate a test, open a source code file in MATLAB. Then, on the Editor or Live Editor tab, in the Test section, select Generate Test > Generate test for the current file using Copilot. The test file that opens can contain one or more tests. You do not need to specify input or expected output values in the test.
For example, suppose that you have a function named
quadraticSolver
.
function r = quadraticSolver(a,b,c) % quadraticSolver returns solutions to the % quadratic equation a*x^2 + b*x + c = 0. arguments a (1,1) {mustBeNumeric,mustBeNonzero} b (1,1) {mustBeNumeric} c (1,1) {mustBeNumeric} end r = sort([(-b + sqrt(b^2 - 4*a*c)) / (2*a); ... (-b - sqrt(b^2 - 4*a*c)) / (2*a)]); end
Then, generate tests for the function using MATLAB Copilot. Save the test file.
% This test file was generated by Copilot. Validate the generated output before use. classdef TestQuadraticSolver < matlab.unittest.TestCase methods(Test) function testRealRoots(testCase) % Test case with real roots a = 1; b = -3; c = 2; % (x-1)(x-2) = 0 expected = [1; 2]; actual = quadraticSolver(a,b,c); testCase.verifyEqual(actual,expected,"AbsTol",1e-10); end function testComplexRoots(testCase) % Test case with complex roots a = 1; b = 2; c = 5; % x^2 + 2x + 5 = 0 expected = [-1 + 2i; -1 - 2i]; actual = quadraticSolver(a,b,c); testCase.verifyEqual(actual,expected,"AbsTol",1e-10); end function testDoubleRoot(testCase) % Test case with a double root a = 1; b = 2; c = 1; % (x+1)^2 = 0 expected = [-1; -1]; actual = quadraticSolver(a,b,c); testCase.verifyEqual(actual,expected,"AbsTol",1e-10); end function testInvalidInput(testCase) % Test case for invalid input (non-numeric) testCase.verifyError(@() quadraticSolver("a",2,1), ... "MATLAB:validators:mustBeNumeric"); end function testZeroCoefficientA(testCase) % Test case for zero coefficient a testCase.verifyError(@() quadraticSolver(0,2,1), ... "MATLAB:validators:mustBeNonzero"); end end end
Run Tests and Verify Results
To validate the generated tests and test the source code, run the tests and review the results.
For example, when you run the tests in the TestQuadraticSolver
file, the testComplexRoots
test fails. The test values in the failure
diagnostic indicate that the expected values do not match the order of the actual
values. To fix this test failure, replace expected = [-1 + 2i; -1 -
2i];
with expected = sort([-1 + 2i; -1 - 2i]);
. Save
the test file and run the tests again. The tests pass.
You can run the generated tests by using any of these options:
runtests
function at the MATLAB command lineRun Tests button in the MATLAB Editor toolstrip on the Editor tab
Run button
in the Test Browser
Run button
in the MATLAB Test Manager
Run button
in the Code Quality Dashboard
For more information, see Run MATLAB Tests.
See Also
Apps
Functions
Topics
- Generate Tests for MATLAB Source Code
- Set Up MATLAB Copilot (MATLAB Copilot)