Test Exceptions and Mask Contraints within the TestManager
Réponses (2)
0 votes
3 commentaires
Hi @Kris Schweikert ,
Thanks for jumping in!
You're absolutely right—the custom criteria field allows you to validate specific diagnostics, like checking the exception identifier, but the overall test will still be marked as failed if an error is thrown during execution, even if it's expected and verified in your custom logic.
One workaround is to structure your test such that the error is anticipated and doesn’t cause the test to fail prematurely. Instead of relying on the simulation to fail naturally (which triggers a failure at the test level), you can run the simulation inside a function and explicitly catch the error using a `try-catch` block. Then you can validate the error details in your custom criteria or using `verifyEqual`, without the test itself being marked as failed by default behavior.
Here's an example of how you might do this:
function out = runSimulationWithExpectedError()
try
simOut = yourSimulationFunction(-5); % or however you call your model
out = simOut;
catch ME
out = ME; % Return the exception object for verification
end
end
Then in your test case:
testCase = sltest.TestCase.forInteractiveUse; err = runSimulationWithExpectedError(); testCase.verifyEqual(err.identifier, 'Simulink:Masking:ConstraintErrorMessageHeader');
This way, you're explicitly handling the error and verifying it, so the test framework won’t automatically mark it as a failure just because an error occurred.
Unfortunately, you're right that `TestResult` objects are read-only, so you can’t override the final status. But by avoiding unhandled exceptions, you can prevent the test from failing at the framework level.
Hope this helps!
Hi @Kris Schweikert,
That’s a great solution — I’m glad my response helped spark the idea! Using set_param directly in the custom criteria to inject an invalid value and then catching the expected error is a smart way to bypass simulation-level failures while still validating error handling. I like how you explicitly fail the test when no error is thrown — that makes the intent very clear.
As you pointed out, wrapping this in a broader test with other logical or temporal assessments ensures that the expected error doesn’t inadvertently fail the entire test run. It's a clean workaround for the limitations of TestResult mutability.
Thanks for sharing your approach — I’m sure others will find it helpful too!
Catégories
En savoir plus sur Outputs dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!