I finally found a workaround to the message suppression that still allows the subdivision of the interval of integration $[0,3]$ at $x = 1, 2$. You have to protect the message-generating code sufficiently deep so that the symbolic processing takes care of the singularities. Then inside a ?NumericQ
protected function, turn on messages again by resetting $Messages
.
ClearAll[test, ftrue];
With[{$m = $Messages},
ftrue[x_?NumericQ] := Block[{$Messages = $m}, Message[Test::message]; x^2];
];
test[x_] := If[1 < x < 2, ftrue[x], x];
NIntegrate[test[x], {x, 0, 3}]
Message::msgl
: $MessageList
$MessageList
not a list; reset to {}
.
Test::message
: -- Message text not found --
Test::message
: -- Message text not found --
Test::message
: -- Message text not found --
General::stop
: Further output of Test::message
will be suppressed during this calculation.
Out[56]= 5.33333
The extra message
There's an extraneous Message::msgl
that is the fault of NIntegrate
. (It doesn't seem to matter or wasn't detected, since messages are turned off.) It seems easy enough just to ignore it, but if that's unacceptable, you could reset $MessageList
yourself, or Quiet
the message:
If[! ListQ[$MessageList], $MessageList = {}]; (* OR *)
Quiet[Message[Test::message], Message::msgl];
Using this definition of ftrue
, you can see the message generation, even though the messages themselves are not printed:
ftrue[x_?NumericQ] := (
If[Length@$MessageList < 4, Print[$MessageList]];
Message[Test::message]; x^2);
NIntegrate[test[x], {x, 0, 3}]
$MessageList
{Message::msgl,Test::message}
{Message::msgl,Test::message,Test::message}
Out[69]= 5.33333
A glimpse at the symbolic processing
It may be of interest to inspect how NIntegrate
breaks down the integrand.
With the OP's test[]
, it looks like the following. We get three integration regions
NIntegrate[test[x], {x, 0, 3}, IntegrationMonitor :> ((regions = #) &), MaxRecursion -> 0];
Column[regions /. r_NIntegrate`GeneralRule :> Short[r], Dividers -> All]

With my version of test[]
, the x^2
in the first region in the table (from 1
to 2
) is replaced with ftrue[x]
.
NIntegrate
? $\endgroup$