1

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?

vsht
  • 3,517
  • 13
  • 23
  • 5
  • 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? – vsht Jun 10 '15 at 20:55

1 Answers1

2

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

*)
ilian
  • 25,474
  • 4
  • 117
  • 186