フィルターのクリア

Error with MATLAB Programming Techniques 'Writing Test Function'

2 ビュー (過去 30 日間)
Pablo Horrillo Quintero
Pablo Horrillo Quintero 2022 年 2 月 7 日
回答済み: SAI SRUJAN 2023 年 12 月 27 日
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
Pablo Horrillo Quintero 2022 年 2 月 7 日
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 Center および File ExchangePower and Energy Systems についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by