Create Tests for MATLAB Source Code - MATLAB & Simulink - MathWorks United Kingdom (2024)

Create Tests for MATLAB Source Code

MATLAB® Test™ enables you to interactively create tests for your scripts and functions in the Editor or Live Editor. The test file created for a script or function contains a test class to exercise that script or function. You can use this file as a starting point for writing your own tests.

This topic shows how to create a test for a simple function and then build on the generated test code.

Create Test for Function

In a file named quadraticSolver.m in your current folder, create the quadraticSolver function. The function takes as inputs the coefficients of a quadratic polynomial and returns the roots of that polynomial. To validate its inputs, the function uses an arguments block.

function r = quadraticSolver(a,b,c)% quadraticSolver returns solutions to the% quadratic equation a*x^2 + b*x + c = 0.arguments a {mustBeNumeric,mustBeNonzero} b {mustBeNumeric} c {mustBeNumeric}endr = sort([(-b + sqrt(b^2 - 4*a*c)) / (2*a); ... (-b - sqrt(b^2 - 4*a*c)) / (2*a)]);end

To create a test file that contains initial test code for the quadraticSolver function, right-click in the Editor and select Create Test.

Create Tests for MATLAB Source Code- MATLAB & Simulink- MathWorks United Kingdom (1)

This code provides the contents of the resulting test file. The generated test class:

  • Includes autogenerated inputs that satisfy the arguments block in the quadraticSolver function

  • Calls the quadraticSolver function to produce an output

  • Uses the verifyEqual qualification method to test the produced output

% This is an autogenerated sample test for file quadraticSolver.mclassdef testquadraticSolver < matlab.unittest.TestCase methods (Test) function test_quadraticSolver(testCase) a = -7162.2732; b = -1564.7743; c = 8314.7105; % Specify the expected output(s) of % quadraticSolver expected_r = ; % Exercise the function quadraticSolver actual_r = quadraticSolver(a, b, c); testCase.verifyEqual(actual_r, expected_r); end endend

A test file created for your script or function serves only as a starting point. For example, you must at least specify the expected output in the test code generated for the quadraticSolver function before running the test.

Note

MATLAB Test attempts to generate input argument values only if your function uses an arguments block for input argument validation. If your function does not include an arguments block, then the test class does not include autogenerated data to represent the input arguments.

Build on Generated Test Code

Build on the test code generated for the quadraticSolver function by first completing the code of the generated Test method and then adding another Test method using the code insertion options on the toolstrip. For more information on how to interactively add elements to a test class, see Insert Test Code Using Editor.

Complete Generated Test Method

The generated test for the quadraticSolver function requires you to specify the expected output of the function. To complete the test and make it more readable, follow these steps:

  1. Rename the test class as SolverTest and the Test method as solution. Then, remove the comments you no longer need from the file and save the file as SolverTest.m in your current folder.

  2. Specify the expected output of the function by calling the roots function with the autogenerated values.

  3. Because the verifyEqual method tests floating-point vectors, specify a tolerance for comparison.

classdef SolverTest < matlab.unittest.TestCase methods (Test) function solution(testCase) a = -7162.2732; b = -1564.7743; c = 8314.7105; expected_r = sort(roots([a b c])); actual_r = quadraticSolver(a,b,c); testCase.verifyEqual(actual_r,expected_r,AbsTol=eps) end endend

Add Another Test Method

To test the quadraticSolver function against invalid inputs, add another Test method to the class by clicking Create Tests for MATLAB Source Code- MATLAB & Simulink- MathWorks United Kingdom (2) in the Test section on the Editor tab.

Create Tests for MATLAB Source Code- MATLAB & Simulink- MathWorks United Kingdom (3)

Implement the added method by following these steps:

  1. Rename the method as nonnumericInput.

  2. Add code to the method to verify that the quadraticSolver function throws an error when it is called with inputs 1, '-3', and 2.

Save the file. This code provides the contents of the SolverTest class.

classdef SolverTest < matlab.unittest.TestCase methods (Test) function solution(testCase) a = -7162.2732; b = -1564.7743; c = 8314.7105; expected_r = sort(roots([a b c])); actual_r = quadraticSolver(a,b,c); testCase.verifyEqual(actual_r,expected_r,AbsTol=eps) end function nonnumericInput(testCase) testCase.verifyError(@()quadraticSolver(1,'-3',2), ... "MATLAB:validators:mustBeNumeric") end endend

Run Tests in Test Class

You can run the tests in the SolverTest class interactively in the Editor or in the Test Browser app. For example, with the test class code visible in the Editor, go to the Editor tab and in the Run section, click Create Tests for MATLAB Source Code- MATLAB & Simulink- MathWorks United Kingdom (4). In this example, both the tests pass.

Create Tests for MATLAB Source Code- MATLAB & Simulink- MathWorks United Kingdom (5)

For more information on how to run tests and customize your test run interactively, see Run Tests in Editor and Run Tests Using Test Browser.

See Also

Functions

  • verifyEqual

Classes

  • matlab.unittest.TestCase

Related Topics

  • Insert Test Code Using Editor
  • Function Argument Validation
Create Tests for MATLAB Source Code
- MATLAB & Simulink
- MathWorks United Kingdom (2024)

References

Top Articles
Alles wat je moet weten over het VIL-VCU certificaat
Tampa Bay Olympians: Meet the local, Florida athletes competing in 2024 Olympic Games
11 beste sites voor Word-labelsjablonen (2024) [GRATIS]
Spectrum Gdvr-2007
Genesis Parsippany
Best Big Jumpshot 2K23
80 For Brady Showtimes Near Marcus Point Cinema
Lighthouse Diner Taylorsville Menu
DL1678 (DAL1678) Delta Historial y rastreo de vuelos - FlightAware
CHESAPEAKE WV :: Topix, Craigslist Replacement
Weapons Storehouse Nyt Crossword
Slapstick Sound Effect Crossword
B67 Bus Time
414-290-5379
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
Ave Bradley, Global SVP of design and creative director at Kimpton Hotels & Restaurants | Hospitality Interiors
Detroit Lions 50 50
Morocco Forum Tripadvisor
Healing Guide Dragonflight 10.2.7 Wow Warring Dueling Guide
Walmart Windshield Wiper Blades
Me Cojo A Mama Borracha
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
Everything We Know About Gladiator 2
Craighead County Sheriff's Department
Kiddle Encyclopedia
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
Hannaford To-Go: Grocery Curbside Pickup
Cb2 South Coast Plaza
480-467-2273
Page 2383 – Christianity Today
Criterion Dryer Review
Rugged Gentleman Barber Shop Martinsburg Wv
Tomb Of The Mask Unblocked Games World
101 Lewman Way Jeffersonville In
1964 Impala For Sale Craigslist
Red Sox Starting Pitcher Tonight
Indiana Jones 5 Showtimes Near Jamaica Multiplex Cinemas
Housing Assistance Rental Assistance Program RAP
Angela Muto Ronnie's Mom
Fox And Friends Mega Morning Deals July 2022
Quality Tire Denver City Texas
B.k. Miller Chitterlings
Waffle House Gift Card Cvs
Srg Senior Living Yardi Elearning Login
FedEx Authorized ShipCenter - Edouard Pack And Ship at Cape Coral, FL - 2301 Del Prado Blvd Ste 690 33990
Greatpeople.me Login Schedule
Server Jobs Near
About us | DELTA Fiber
Rocket Bot Royale Unblocked Games 66
Sam's Club Fountain Valley Gas Prices
Anthony Weary Obituary Erie Pa
Guidance | GreenStar™ 3 2630 Display
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 6168

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.