C++ assert Test

880 views
Skip to first unread message

pod...@gmail.com

unread,
Apr 22, 2009, 4:55:16 AM4/22/09
to Google C++ Testing Framework
The program I have to test contains some C++ assert statement from
<cassert>.
I have something like that to test :

void Foo(int x, int y)
{
assert(x > y);
}

TEST(tassert, testAssert)
{
Foo(2, 1); //true
Foo(1, 2); //false
(...some ASSERT_*...)
}

When assert(false) is executed, not only the test crash, but all the
tests program crash.
How can I do to continue the execution of the test program with this
test marked as "failed" ?
I maybe forget something about deathtest or an execution flag or
something else..

Zhanyong Wan (λx.x x)

unread,
Apr 22, 2009, 3:34:50 PM4/22/09
to pod...@gmail.com, Google C++ Testing Framework
On Wed, Apr 22, 2009 at 1:55 AM, pod...@gmail.com <pod...@gmail.com> wrote:
>
> The program I have to test contains some C++ assert statement from
> <cassert>.
> I have something like that to test :
>
> void Foo(int x, int y)
> {
>        assert(x > y);
> }
>
> TEST(tassert, testAssert)
> {
>        Foo(2, 1); //true
>        Foo(1, 2); //false
>        (...some ASSERT_*...)
> }

{
Foo(2, 1);
EXPECT_DEATH(Foo(1, 2), "");
...
}

>
> When assert(false) is executed, not only the test crash, but all the
> tests program crash.
> How can I do to continue the execution of the test program with this
> test marked as "failed" ?
> I maybe forget something about deathtest or an execution flag or
> something else..
>

--
Zhanyong

pod...@gmail.com

unread,
Apr 22, 2009, 4:01:58 PM4/22/09
to Google C++ Testing Framework
I don't know when it fail (I don't know the values of x and y) so if I
put EXPECT_DEATH and it don't fail, the test is marked as "fail". I
just want the test program not crash.
I think the assertion I need is "!EXPECT_DEATH".
(I don't have my code here, I hope I'm right..)

On 22 avr, 21:34, Zhanyong Wan (λx.x x) <w...@google.com> wrote:

Zhanyong Wan (λx.x x)

unread,
Apr 22, 2009, 4:07:34 PM4/22/09
to pod...@gmail.com, Google C++ Testing Framework
You can use EXPECT_EXIT(statement, ExitedWithCode(0), "") then.

--
Zhanyong

podidoo

unread,
Apr 22, 2009, 4:17:59 PM4/22/09
to Zhanyong Wan (λx.x x), Google C++ Testing Framework
Ok thanks, I'll try it.
But, is there any flag or something to lauch every test in its own thread?
(I use googletest to make large/fonctional tests not unit test, so I don't have control on everything. There are many "assert" in the code, so I can't put a death test on every statement)

2009/4/22 Zhanyong Wan (λx.x x) <w...@google.com>

Zhanyong Wan (λx.x x)

unread,
Apr 22, 2009, 4:54:54 PM4/22/09
to podidoo, Google C++ Testing Framework
2009/4/22 podidoo <pod...@gmail.com>:

> Ok thanks, I'll try it.
> But, is there any flag or something to lauch every test in its own thread?

No, we don't have such a flag. (BTW, I think you mean process instead
of thread.)

> (I use googletest to make large/fonctional tests not unit test, so I don't
> have control on everything. There are many "assert" in the code, so I can't
> put a death test on every statement)

I'd say don't worry about it unless it's a real problem. It would be
nice if the assert doesn't crash the entire test program, but how
often does it happen? If it happens a lot, you probably have a bigger
problem to worry about.

--
Zhanyong

Reply all
Reply to author
Forward
0 new messages