How to unit test several different functions that take the same inp... (2024)

68 views (last 30 days)

Show older comments

Louis on 13 Nov 2020

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently

Commented: Jon on 13 Nov 2020

Open in MATLAB Online

This is my first time writing unit tests, so advice from experienced users will be greatly appreciated.

I have several compute functions from which many of them take in the same input arguments, and I would like to write efficient unit tests with minimum code duplication.

I have a following working test class:

%% Test Class Definition

classdef ComputeFunction1Test < matlab.unittest.TestCase

%% Test Method Block

methods (Test)

% includes unit test functions

%% Test Function

function testFunction1FirstOutput(testCase)

% Actual solution - AB contribution

load('test_data.mat') % variables input1, input2, and input3

[act, ~] = computeFunction1(input1, input2, input3);

% Verify using expected AB contribution

load('function1ExpectedOut1.mat'); % variable exp contains expected output

testCase.verifyEqual(act, exp);

end

function testFunction1SecondOutput(testCase)

% Actual solution - normalization factor for AB

load('test_data.mat')

[~, act] = computeFunction1(input1, input2, input3);

% Verify using expected normalization factor

load('function1ExpectedOut2.mat'); % variable exp contains expected output

testCase.verifyEqual(act, exp);

end

end

end

I have several more different functions that take the same input arguments and produce output in the same format as computeFunction1.m, which above test class tests. (say computeFunction2.m, computeFunction3.m, and computeFunction3.m, ...)

I could copy the above test class and change the function name to test other functions that take the same input arguments, but this seems inefficient.

Is there a way to handle this situation more efficiently?

Note that I do have other compute functions that require different input arguments as well that need to be tested too. (say specialComputeFunction1 and specialComputeFunction2.m)

2 Comments

Show NoneHide None

Jon on 13 Nov 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131923

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131923

Maybe you could do it in a loop. Put all of the expected results in a single matrix, with a column for each argument

then just evalute the function once returning all of the output arguments and have a loop to compare each argument with appropriate column in expected results matrix. Return vector of pass fail logicals with an element for each output argument. In your example, just two elements, but it could be any number

Jon on 13 Nov 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131988

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131988

Sorry, I should have looked more deeply into your question. I hadn't realized that you were using a built in unit test class provided by MATLAB. I would then steer you to looking at the examples and documentation for that. I'm sure there is a standard approach for what you are trying to do as it seems like a typical usecase. I will look into this more myself, would probably be helpful in my work to use this built in testing functionality

Sign in to comment.

Sign in to answer this question.

Answers (3)

Sean de Wolski on 13 Nov 2020

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#answer_543823

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#answer_543823

Edited: Sean de Wolski on 13 Nov 2020

Open in MATLAB Online

Look into using TestParameters. You can use an array of function handles as the parameter values to traverse different functions and pass the results as parameters as well. You'll need to run them with ParameterCombination=sequential. Here's a functional example:

classdef tFcnParameter < matlab.unittest.TestCase

properties (TestParameter)

fcn = struct('plus',@plus,'minus',@minus)

result = {5 1}

end

methods (Test, ParameterCombination = 'sequential')

function shouldApplyFcn(testCase, fcn, result)

r = fcn(3, 2);

testCase.verifyEqual(r, result);

end

end

end

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Steven Lord on 13 Nov 2020

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#answer_543828

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#answer_543828

Open in MATLAB Online

%% Test Class Definition

classdef ComputeFunction1Test < matlab.unittest.TestCase

%% Test Method Block

methods (Test)

% includes unit test functions

%% Test Function

% Actual solution - AB contribution

load('test_data.mat') % variables input1, input2, and input3

[act, ~] = computeFunction1(input1, input2, input3);

% Verify using expected AB contribution

load('function1ExpectedOut1.mat'); % variable exp contains expected output

testCase.verifyEqual(act, exp);

% *snip rest of test file*

You don't want to do this. The identifier exp already has a meaning in MATLAB, and there's no indication that it should be a variable when MATLAB parses the function. If you must load data in a function I strongly encourage you to call load with an output argument so you don't "poof" variables into the workspace.

Rather than using MAT-files, if the output arguments are "small" consider hard-coding them. Or consider using the definition of the operation that computeFunction1 performs to determine a way to validate the results without hard-coding them. For instance, if I were validating the svd function (accepts A and computes U, S, and V such that U*S*V' effectively equals A) a valid verification could be "is U*S*V' 'close enough' to A?" This avoids having to create hard-coded copies of expected U, S, and V matrices.

[U, S, V] = svd(A);

testCase.verifyEqual(U*S*V', A, 'AbsTol', someTolerance)

You can also have multiple verify* calls in the same method. I've labeled each portion of the test with which of the four phases they implement.

classdef testBounds < matlab.unittest.TestCase

methods(Test)

function simpleTest1(testCase)

% Setup phase

x = 1:10;

% Exercise phase

[minValue, maxValue] = bounds(x);

% Verify phase

testCase.verifyEqual(minValue, 1);

testCase.verifyEqual(maxValue, 11); % Whoops!

% Teardown phase

%

% x, minValue, and maxValue will automatically be destroyed when the function exits

% so there's no need for explicit teardown for this test.

%

% If the test had opened a figure window (for example) this is where you'd want to

% close that figure window. Actually in that case I would have used addTeardown

% right after I created the figure in the Setup phase so it would be closed

% even if the Exercise or Verify phases threw a hard error. But here is where that

% teardown added by addTeardown would trigger under normal execution.

end

end

end

If you run this, the test failure message will indicate the problem is on the line I've commented "Whoops!"

Be wary of trying to test too much in any individual test method, but in this case testing both outputs from a single function call doesn't strike me as too much.

That being said, if you have a collection of inputs and the corresponding outputs you could write a parameterized test. See the testNumel test method on that page for an example.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Jon on 13 Nov 2020

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#answer_543718

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#answer_543718

Edited: Jon on 13 Nov 2020

Here are a couple of possibilities

You can pass function handles, and then make the function you are testing be one of the arguments to your test program https://www.mathworks.com/help/matlab/matlab_prog/pass-a-function-to-another-function.html

You can also use the MATLAB function eval to evaluate expressions that are written as strings however this seems clumsy, I would prefer using function handles

Also in your example above, why not call the function once, evaluate both output arguments, and then test them rather than calling the function twice, and having all that cut and paste code.

3 Comments

Show 1 older commentHide 1 older comment

Louis on 13 Nov 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131748

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1131748

Open in MATLAB Online

Thank you - I will take a look at the document you referenced.

First output is a list of double whereas the second output is just a single double value, and I wasn't able to find compelling example in the document to evaluate both argument at once.

Could you provide an example for testing both?

I suppose I could just append the second output to the first output and test them together:

testCase.verifyEqual([act1; act2], [exp1; exp2]);

But wouldn't this make it hard to tell where the mismatch is exactly coming from in case this test fails?

Jon on 13 Nov 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1132008

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1132008

Maybe you could do it in a loop. Put all of the expected results in a single matrix, with a column for each argument

then just evalute the function once returning all of the output arguments and have a loop to compare each argument with appropriate column in expected results matrix. Return vector of pass fail logicals with an element for each output argument. In your example, just two elements, but it could be any number

Jon on 13 Nov 2020

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1132013

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/646903-how-to-unit-test-several-different-functions-that-take-the-same-input-arguments-efficiently#comment_1132013

Sorry, I should have looked more deeply into your question. I hadn't realized that you were using a built in unit test class provided by MATLAB. I would then steer you to looking at the examples and documentation for that. I'm sure there is a standard approach for what you are trying to do as it seems like a typical usecase. I will look into this more myself, would probably be helpful in my work to use this built in testing functionality

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsData TypesNumeric TypesLogical

Find more on Logical in Help Center and File Exchange

Tags

  • unittest
  • unit test.
  • oop

Products

  • MATLAB

Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to unit test several different functions that take the same inp... (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to unit test several different functions that take the same inp... (2024)

FAQs

Should unit tests test multiple functions? ›

For unit tests to be effective and manageable, each test should have only one test case. That is, the test should have only one assertion. It sometimes appears that to properly test a feature, you need several assertions. The unit test might check each of these assertions, and if all of them pass, the test will pass.

Should a unit test be repeatable? ›

Unit tests should be repeatable and yield the same results each time they're run. If a test passes one time and fails another time without any changes in the code, it becomes impossible to rely on such tests.

How do you test a function in a unit test? ›

Note that in order to test something, we use one of the assert* methods provided by the TestCase base class. If the test fails, an exception will be raised with an explanatory message, and unittest will identify the test case as a failure. Any other exceptions will be treated as errors.

What is the easiest method to write a unit test? ›

Keep tests small and focused on making them easier to maintain and quickly identify the source of any issues. Write tests before code: Writing tests before writing code helps you to think about the desired functionality and to clarify requirements. This also makes it easier to write testable code.

Why is it best not to perform multiple t tests on the same data? ›

Why not compare groups with multiple t-tests? Every time you conduct a t-test there is a chance that you will make a Type I error. This error is usually 5%. By running two t-tests on the same data you will have increased your chance of "making a mistake" to 10%.

Should a unit test have multiple asserts? ›

There's nothing wrong with multiple assertions in a single test. The above example illustrates the benefits. A single test case can have multiple outcomes that should all be verified.

What should not be included in unit tests? ›

Points of integration to other systems should not be included in unit tests. For example… Testing points of integration to other sub systems is slow and we want to keep the speed of our unit tests fast.

Is there such a thing as too many unit tests? ›

Excessive Unit Tests often mean you're adding tests (more moving parts to your software) which really give you nothing but more lines of code. They do not improve the quality of the software but instead add bloat which is just more stuff to trawl through / manage / deal with in your day-to-day development.

What is the repeat test procedure? ›

Repeat test means laboratory procedures to verify only an abnormal result reported on the initial or second screen and performed on a specimen of blood.

What errors are commonly found during unit testing? ›

Unit testing considerations What errors are commonly found during Unit Testing? (1) Misunderstood or incorrect arithmetic precedence, (2) Mixed mode operations, (3) Incorrect initialization, (4) Precision inaccuracy, (5) Incorrect symbolic representation of an expression.

What is the difference between unit tests and functional tests? ›

Unit testing focuses on testing individual elements of the application, such as functions, methods, or classes, in isolation. Functional testing is concerned with the external aspects of the application, while unit testing is concerned with the internal aspects.

How do I get better at unit tests? ›

Unit Testing Best Practices
  1. Write Readable, Simple Tests. Unit testing helps ensure your code works as intended. ...
  2. Write Deterministic Tests. ...
  3. Test One Scenario Per Test. ...
  4. Unit Tests Should Be Automated. ...
  5. Write Isolated Tests. ...
  6. Avoid Test Interdependence. ...
  7. Avoid Active API Calls. ...
  8. Combine Unit and Integration Testing.
Jun 20, 2022

Is unit testing hard? ›

Unit testing itself is rather easy once you understand how to do it. Even test driven or behavior driven development is easy one mastered… at least for the ideal scenario. What is the ideal scenario then? It is a unit test where the class under test has no external dependencies.

Should unit tests only test one method? ›

There isn't really a one-to-one relationship between classes/methods and unit tests/assertions. But in general, you should try to do something like this: Each unit test covers one case of one method, using a single set of inputs. It uses a single assertion.

Should you test multiple variables at once? ›

As long as the experiment is repeated a sufficient number of times, it does not matter how many variables are used. Yes, an experiment should test only one variable at a time. This ensures that the experimental outcome is clearly due to one identifiable factor.

Should you write unit tests for all methods? ›

Unit tests should test one method only. This allows you to easily identify what failed if the test fails. Unit tests should not be coupled together, therefore one unit test CANNOT rely on another unit test having completed first.

How many separate unit tests should be created to test a method? ›

I write at least one test per method, and somtimes more if the method requires some different setUp to test the good cases and the bad cases. But you should NEVER test more than one method in one unit test.

Top Articles
Academic Advisor I in Gainesville, Florida, United States
Figgy Pudding Recipe | Dirty Dishes Messy Kisses
Can Banks Take Your Money To Pay Off Debts? StepChange
Rachel Sheherazade Nua
Ffxiv Ixali Lightwing
Triple A Flat Tire Repair Cost
Academic Calendar Biola
Editado Como Google Translate
Saydel Botanica
Taterz Salad
FREE Houses! All You Have to Do Is Move Them. - CIRCA Old Houses
What Auto Parts Stores Are Open
Milk And Mocha Bear Gifs
Buhl Park Summer Concert Series 2023 Schedule
Localhotguy
Spicy Korean Gochujang Tofu (Vegan)
Short Swords Resource Pack (1.21.1, 1.20.1) - Texture Pack
Dangerous Cartoons Act - Backlash
Sophia Turner Derek Deso Instagram
Violent Night Showtimes Near The Riviera Cinema
2024 Coachella Predictions
Xsammybearxox
San Diego Terminal 2 Parking Promo Code
8 30 Eastern Standard Time
Pay Vgli
512-872-5079
Jen Chapin Gossip Bakery
Tnt Tony Superfantastic
Antique Wedding Favors
Work with us | Pirelli
Theater X Orange Heights Florida
Utexas Baseball Schedule 2023
Hinterlands Landmarks
Intoxalock Calibration Locations Near Me
Gunblood Unblocked 66
Daftpo
Orylieys
Rs3 Bis Perks
Filmy4 Web Xyz.com
Patriot Ledger Obits Today
Mercy Baggot Street Mypay
Bfads 2022 Walmart
What Is TAA Trade Agreements Act Compliance Trade Agreement Act Certification
What Happened To Daniel From Rebecca Zamolo
Smartmove Internet Provider
1 Filmy4Wap In
Uncg Directions
Leslie Pool Supply Simi Valley
Craigslist Pets Olympia
Siôn Parry: The Welshman in the red of Canada
Craigslist Apartments For Rent Imperial Valley
Two Soyjaks Pointing Png
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 6113

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.