'In gtest, can you try/catch a function that calls "exit(1)"?
There's a function that calls exit(1) for specific cases, and I'm trying to run unit tests with gtest on that function.
I thought I'd be able to use try/catch for that case where the function calls exit(1), but it doesn't catch for me.
Is this normal behavior? If so, how else should I detect that exit(1) was called?
Solution 1:[1]
Unlike in, say, Python, the exit() function of C and C++ doesn't work by throwing an exception. Once it has done the C Runtime cleanup (e.g. calling registered atexit() handlers), it directly calls the kernel to terminate the process, and control never returns to the program.
The good news is that
Google Test does support testing for program exit:
ASSERT_EXIT(statement, predicate, regex);andEXPECT_EXIT(statement, predicate, regex);
statementexits with the given error and its exit code matchespredicate
I haven't checked, but I would guess that this is implemented by fork()ing the test, and waiting for the child to terminate.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hari |
