Error with MATLAB Programming Techniques 'Writing Test Function'

2 次查看(过去 30 天)
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 个评论
Pablo Horrillo Quintero
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 2022-2-7
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.

请先登录,再进行评论。

回答(1 个)

SAI SRUJAN
SAI SRUJAN 2023-12-27
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.

类别

Help CenterFile Exchange 中查找有关 Power and Energy Systems 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by