Error with MATLAB Programming Techniques 'Writing Test Function'
3 views (last 30 days)
Show older comments
Hello,
I don´t know how to continue in section 7.5 Writing Test Function. I just copy the answer proposed in the help but it doesn´t work. Can someone help me?
Thank you so much.
3 Comments
DGM
on 7 Feb 2022
I don't have access to these courses, but I wonder if it's just as simple as:
function test = squareRootTest()
Either way should work, as you've discovered, but there may be differences with how the tool is actually trying to verify that the function definition is valid. That's just a wild guess.
This might be something you have to take up with support. I haven't taken any of these courses, but there should be some sort of support for them.
Answers (1)
SAI SRUJAN
on 27 Dec 2023
Hi Pablo,
I understand that you are facing an issue with writing test functions.
In MATLAB, a test file typically contains test functions that are written to validate the code you've written. You can write a test file using test functions with the MATLAB Unit Testing Framework.
Follow the given code snippet to proceed further,
function test = squareRootTest()
test = functiontests(localfunctions);
end
function posRootTest(testcase)
x = linspace(0,pi);
y = sqrt(x);
assert(isreal(y))
end
function negRootTest(testcase)
z = linspace(-pi,pi);
y = sqrt(z);
assert(~isreal(y))
end
function sqrtTest(testcase)
x = linspace(0,pi);
y = x + 2;
z = sqrt(y);
assert(all(y > z))
end
The 'squareRootTest' function generates a suite of test cases for a square root function using 'functiontests', which creates an array of tests from the local functions within the file. This array can be run by MATLAB's testing framework to validate the square root function.
To run the test, call the 'runtests' function with the name of the test file (without the .m extension), like this:
result = runtests("squareRootTest");
I hope this helps.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!