Question

I'm just starting with MSTest in VS2012. I'm testing native C++.

I made a mistake in my application, reading 'off the end' of a STL string...e.g.

wchar_t c = p_filename[p];

..in my case p was 20000 and p_filename is an empty string. Obviously a bug. Running in a debugger, this triggers:

"Debug assertion failed!! ..string subscript out of range... (press retry to debug the application)".

HOWEVER - Running this code in MSTest PASSES the test. No error reported. Why isn't MSTest picking up this runtime error? (or does MSTest not detect asserts statements in native c++ )?? Shouldn't MSTest report this serious bug?

No correct solution

OTHER TIPS

When the STL detects a problem in debug mode, it calls _CrtDbgBreak() which normally displays a dialog box allowing you to debug the issue. However running under MSTest, nothing happens. No dialog box, MSTest may report success. I fixed this by hooking into _CrtDbgBreak() and triggering an assert(), which uses a different mechanism, the __debugbreak intrinsic. This triggers the familiar "Press 'retry' to debug" dialog in these cases.

    int MyReportHook(int nRptType, char *szMsg, int *retVal)
{
    if( _CRT_ASSERT == nRptType )
    {
        assert(false);
    }
    return 0;
}

TEST_CLASS(UnitTest1)
{
public:
    TEST_METHOD_INITIALIZE(methodName) 
    {
        _CrtSetReportHook(MyReportHook); // hook STL asserts.
    }

    TEST_METHOD(TestMethod1)
    {
        Assert::IsTrue( StripExtension( L"" ) == L"" ); // test for handling empty strings.
        Assert::IsTrue( StripExtension( L"test.txt" ) == L"test" );
    }

};

This is due to the redirection of the asserts done in the CppUnitTest.h file.

            _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);

The intention here is that you would not want your automation to block on dialogs. Clearly not what you want here. But you can edit this header as per your needs and enable it back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top