开发者

Boost Test Fixture object clearing between tests

I am having a issue with boost unit testing. Basically I create a fixture which is part of a suite to unit test a Resource cache. My main issue is between tests the Resource cache is becoming empty. So the first test that tests the cache passes then the second one will fail because the data the first test inserted into the cache is no longer there. To solve this I had to re insert the data for the second test. Is this intended or is it something I am doing wrong? Here is the code. The last 2 tests is where the issue lies.


#include "UnitTestIncludes.hpp"
#include "ResourceCache.hpp"
#include <SFML/Graphics.hpp>

struct ResourceCacheFixture
{
    ResourceCacheFixture()
    {
        BOOST_TEST_MESSAGE("Setup Fixture...");
        key = "graysqr";
        imgpath = "../images/graysqr.png";
    }

    ResourceCache<sf::Image, ImageGenerator> imgCache;
    std::string key;
    std::string imgpath;
};

// Start of Test Suite

BOOST_FIXTURE_TEST_SUITE(ResourceCacheTestSuite, ResourceCacheFixture)

// Start of tests

BOOST_AUTO_TEST_CASE(ImageGeneratorTest)
{
    ImageGenerator imgGen;
    BOOST_REQUIRE(imgGen("../images/graysqr.png"));

}

BOOST_AUTO_TEST_CASE(FontGene开发者_StackOverflow社区ratorTest)
{
    FontGenerator fntGen;
    BOOST_REQUIRE(fntGen("../fonts/arial.ttf"));
}

// This is where the issue is.  The data inserted in this test is lost for when I do
// the GetResourceTest.  It is fixed here by reinserting the data.
BOOST_AUTO_TEST_CASE(LoadResourceTest)
{
    bool result = imgCache.load_resource(key, imgpath);
    BOOST_REQUIRE(result);
}

BOOST_AUTO_TEST_CASE(GetResourceTest)
{
    imgCache.load_resource(key, imgpath);
    BOOST_REQUIRE(imgCache.get_resource(key));
}

// End of Tests

// End of Test Suite
BOOST_AUTO_TEST_SUITE_END()


It is intended. One of the key principles of unit testing is that every test is run in isolation. It should be given a clean environment in which to run, and that environment should be cleaned up again afterwards, so that tests do not depend on each others.

With Boost.Test, you can specify which tests to run from the commandline, so you don't have to run the entire suite. If your tests depend on each others, or on the order in which they're executed, then this would cause tests to fail.

Fixtures are intended to set up the environment you need to run the test. If you need resources to be created before the test runs, the fixture should create them, and clean them up again afterwards.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜