필터 지우기
필터 지우기

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 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