The macros ASSERT/EXPECT_EXIT(statement, predicate, regex)
are satisified
when:-
- Execution of
statement
will cause the test-case process to invoke exit
- The return code of
exit
satisifies predicate
regex
matches the stderr
output resulting from statement
The standard function exit(N)
causes the calling program to terminate normally with return code N
: hence the documentation:
::testing::ExitedWithCode(exit_code)
This expression is true if the program exited normally with the given exit code.
The author of the answer that you want to make "more straightforward"
has sketched a test-case that essentially asserts:-
- If the test-case process launches a
boost::process
c
and then waits for c
to finish,
then c
will be found to have invoked exit(0)
.
Which may well be true.
You have written a test case that asserts:-
- If the test-case process launches a
boost::process
c
and then terminates c
, with
c.terminate()
, then terminating c
with c.terminate()
will cause the test-case process to invoke exit(0)
.
Which is certainly false.
Futhermore, c.terminate()
does not give c
the opportunity to eat a last meal, smoke a last cigarette or terminate normally by calling exit
. It just shoots c
in the head.
So even if your test case did test whether c
called exit(0)
as a result of c.terminate()
, you would find that it doesn't.
Best go with the answer you linked to, as it stands.
Contd. for OP's comments
You're hoping to hear how it is possible to apply googletest death-test
macros to death-tests of processes other than the test-case process.
That is not possible. The documentation of death-tests is all here.
To death-test processes other than the test-case process you'll
have to use the general-purpose ASSERT/EXPECT
macros, in the way indicated
by the answer you linked to.
Going that way, if you wish to test s.status()
for any specific terminating signal
conditions then you will need to get the relevant information out of s.status()
in an OS-specific manner and ASSERT/EXPECT
that it what it should
be. If your OS is Posix-conforming (e.g. Linux), then see, for example,
boost::process::posix_status
If your target program contains functions or classes whose behaviour
you would like to subject to death-tests then you always have the option
of using googletest to do this in the intended manner, i.e.
by factoring those functions or classes into a library that is linked
by your target program and then death-testing that library with
the death-test macros.