views:

230

answers:

6

I have a unit test project based on UnitTest++. I usually put a breakpoint to the last line of the code so that the I can inspect the console when one of the tests fails:

  n = UnitTest::RunAllTests();
  if ( n != 0 )
  {
  // place breakpoint here    
    return n;
  }
  return n;

But I have to reinsert it each time I check-out the code anew from SVN. Is it possible to somewhat place the breakpoint by the compiler?:

      n = UnitTest::RunAllTests();
      if ( n != 0 )
      {
      // place breakpoint here    
#ifdef __MSVC__
        @!!!$$$??___BREAKPOINT;
#endif
        return n;
      }
      return n;
A: 

How about using a Debug or Trace method to output the console info. That may be a better approach than relying on breakpoints.

Cody C
The UnitTest++ framework does the output. I insert statements such asCHECK(pi < 4); and if pi is 31 the framework prints the error. There are like 500 tests and the number is growing. They usually do not fail.
danatel
+11  A: 

Use the __debug_break() intrinsic(requires the inclusion of <intrin.h>).

Using __debug_break() is preferable to directly writing __asm { int 3 } since inline assembly is not permitted when compiling code for the x64 architecture.

And for the record, on Linux and Mac, with GCC, I'm using __builtin_trap().

Gregory Pakosz
A: 

How often do you check out the project from SVN? This is typically something I only do once per project, or when I rebuild my PC.

If you also checkin the project files, the breakpoints should be stored in the project files.

I think it is in the .suo file. You could also put that under SVN control if you wanted, though I prefer not to.

mrjoltcola
As often as possible to be sure that all new files have been added before commit and no dependecies are missing. It is also nice to keep old build directories on the disc to have quick access to the old versions of binaries.
danatel
Good point. We also do smoke test builds and that didn't occur to me.
mrjoltcola
+10  A: 
DebugBreak(void)

From Winbase.h.

MSDN

clintp
+4  A: 

You could use this in C or C++

__asm
{
    int 3
}
Indeera
only works for x86 though since the compiler doesn't let you write inline assembly for x64
Gregory Pakosz
+1 Brings back memories.
Robert
+1  A: 

If you are using VC6 (yes, outdated but still in use in some places/projects), DebugBreak() will work but you may end up in some obscure location deep withing Windows DLLs, from which you have to walk the stack up back into your code.

That's why I'm using ASSERT() in MFC or assert() in "standard" code.

Your example would work like this:

n = UnitTest::RunAllTests();
ASSERT(n == 0);
//assert(n == 0);
return n;

If you don't need a result and want it just for debugging, you can also do

if(0 != UnitTest::RunAllTests())
{
    ASSERT(FALSE);
    //assert(false);
}
mxp