开发者

Boost Unit testing memory reuse causing tests that should fail to pass

开发者 https://www.devze.com 2022-12-24 23:19 出处:网络
We have started using the boost unit testing library for a large existing code base, and I have run into some trouble with unit tests incorrectly passing, seemingly due to the reuse of memory on the s

We have started using the boost unit testing library for a large existing code base, and I have run into some trouble with unit tests incorrectly passing, seemingly due to the reuse of memory on the stack.

Here is my situation:

BOOST_AUTO_TEST_CASE(test_select_base_instantiation_default)  
{
    SelectBase selectBase();
    BOOST_CHECK_EQUAL( selectBase.getSelectType(), false);
    BOOST_CHECK_EQUAL( selectBase.getTypeName(_T(""));
    BOOST_CHECK_EQUAL( selectBase.getEntityType(), -1);
    BOOST_CHECK_EQUAL( selectBase.getDataPos(), -1);
}

BOOST_AUTO_TEST_CASE(test_select_base_instantiation_parameterized)  
{  
    SelectBase selectBase(true, _T("abc"));  
    BOOST_CHECK_EQUAL( selectBase.getSelectType(), false);  
    BOOST_CHECK_EQUAL( selectBase.getTypeName(_T("abc"));
    BOOST_CHECK_EQUAL( selectBase.getEntityType(), -1);
    BOOST_CHECK_EQUAL( selectBase.getDataPos(), -1);
}

The first test passed correctly, initializing all the variables.

The constructor in the second unit test did not correctly set EntityType or DataPosition, but the unit test passed. I was able to get it to fail by placing some variables on the stack in the second test, like so:

BOOST_AUTO_TEST_CASE(test_select_base_instantiation_parameterized)  
{  
    int a, b;
    SelectBase selectBase(true, _T("abc"));  
    BOOST_CHECK_EQUAL( selectBase.getSelectType(), false);  
    BOOST_CHECK_EQUAL( selectBase.getTypeName(_T("abc"));
    BOOST_CHECK_EQUAL( selectBase.getEntityType(), -1);
    BOOST_CHECK_EQUAL( selectBase.getDataPos(), -1);
}

If there is only one int, only the EntityType CHECK_EQUAL fails, but if there are two, both EntityType and DataPos fail, so it seems pretty clear that this is an issue with the variables being created on the same stack memory or some such.

Is there a good way to clear the memory betw开发者_高级运维een each unit test, or am I potentially using the library incorrectly or writing bad tests? Any help would be appreciated.

Update:

Select base is a simple class, with only bool, int, and CString member variables. It is a base class to handle state for more complex implementations, so it doesn't access any global variables or global state.

What I need is a way to set the memory between calls to something like 0xdeadf00d, so that if a member variable is left uninitialized the unit test can catch it. Otherwise only the first unit test does any good.

I updated to the boost 1.41, but the issue wasn't resolved. It did affect the issue somewhat in some of the test cases, but not to the point that all the test would fail properly.


Don't reuse the test case name for different tests.

Instead, call them something like:

test_select_base_instantiation_default_1

test_select_base_instantiation_default_2


Try BOOST_TEST_RANDOM if you want ++paranoia.


Then there is nothing wrong with the code presented.

It seems you are accessing uninitialized data in SelectBase or any of its dependencies, if it has any. Either you are accessing global state in SelectBase or it instantiates dependencies on its own. Both are discouraged.


I ended up writing a function to set the object to an invalid state after each test case:

void unsetSelectBase(SelectBase selectBase) 
{
    selectBase.setSelectType(true);
    selectBase.getTypeName(_T("InvalidName"));
    selectBase.getEntityType(42);
    selectBase.getDataPos(2718);
}

BOOST_AUTO_TEST_CASE(test_select_base_instantiation_default)  
{
    SelectBase selectBase();
    BOOST_CHECK_EQUAL( selectBase.getSelectType(), false);
    BOOST_CHECK_EQUAL( selectBase.getTypeName(_T(""));
    BOOST_CHECK_EQUAL( selectBase.getEntityType(), -1);
    BOOST_CHECK_EQUAL( selectBase.getDataPos(), -1);
    unsetSelectBase(selectBase);
}


BOOST_AUTO_TEST_CASE(test_select_base_instantiation_parameterized)  
{  
    SelectBase selectBase(true, _T("abc"));  
    BOOST_CHECK_EQUAL( selectBase.getSelectType(), false);  
    BOOST_CHECK_EQUAL( selectBase.getTypeName(_T("abc"));
    BOOST_CHECK_EQUAL( selectBase.getEntityType(), -1);
    BOOST_CHECK_EQUAL( selectBase.getDataPos(), -1);
    unsetSelectBase(selectBase);
}

This causes all the test cases to fail appropriately when any member variables are not initialized. I guess this is just another manual setup/tear down type task that is necessary, since the framework doesn't clear the memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消