1
$\begingroup$

I would like to create unit tests, where the function is expected to generate $Aborted, e.g. because it was given wrong input. However, I don't see a way how to make MUnit accept such an outcome. In Workbench, what I get is

Test Failure Message: Aborted during evaluation

A simple example for the frontend would be

ClearAll[Foo];
Foo::msg = "Error! `1` must be an integer!";
Foo[x_] := 
   If[ Head[x] =!= Integer,
       Message[Foo::msg, x];
       Abort[],
       x^2
   ];

Then I tried the following, which apparently doesn't work

<< MUnit`
TestRun[{Test[Foo[1], 1], Test[Foo[2], 4], Test[Foo["d"], $Aborted]}]

since I get

Starting test run "Automatic"
..
Stopping test run on TestTerminate: Aborted during evaluation
!
** Aborted during evaluation **

Tests run: 3
Failures: 0
Messages Failures: 0
Skipped Tests: 0
Errors: 1
Fatal: False

Is there a way to tell MUnit that I want the function to return $Aborted, so that the last test actually succeeds and not fails?

$\endgroup$
2
  • 5
    $\begingroup$ See CheckAbort $\endgroup$
    – ilian
    Jun 10, 2015 at 17:35
  • $\begingroup$ Thanks, CheckAbort is exaclty what I needed. Now I can use Test[CheckAbort[Foo["d"], "Aborted"], "Aborted", Foo::msg] and it all works fine. Can you please write your comment as an answer, so that I can accept it? $\endgroup$
    – vsht
    Jun 10, 2015 at 20:55

1 Answer 1

2
$\begingroup$

CheckAbort could be used, for example

<< MUnit`
TestRun[{Test[Foo[1], 1], Test[Foo[2], 4], 
   Test[CheckAbort[Foo["d"], $Aborted], $Aborted, Foo::msg]}]

(*

Starting test run "Automatic"
...
Tests run: 3
Failures: 0
Messages Failures: 0
Skipped Tests: 0 
Errors: 0
Fatal: False

*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.