Error with MATLAB Programming Techniques 'Writing Test Function'

3 views (last 30 days)
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
Pablo Horrillo Quintero
Pablo Horrillo Quintero on 7 Feb 2022
Hola,
Muchas gracias por responder. . El código de error es Test Results:
Incorrect!
Were three tests run? OK
Did all tests pass? OK
Is the file a function? WRONG
En primer lugar, este es el codigo de run_squareRootTest y en squareRootTest, respectivamente
function test = squareRootTest
test = functiontests(localfunctions);
end
Positive roots are real
function posRootTest(testcase)
x = linspace(0,pi);
y = sqrt(x);
assert(isreal(y))
end
Negative roots are complex
function negRootTest(testcase)
z = linspace(-pi,pi);
y = sqrt(z);
assert(~isreal(y))
end
sqrt(x) is smaller than x (if x > 1)
function sqrtTest(testcase)
x = linspace(0,pi);
y = x + 2;
z = sqrt(y);
assert(all(y > z))
end
DGM
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.

Sign in to comment.

Answers (1)

SAI SRUJAN
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.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!