簡體   English   中英

提高測試之間的測試夾具對象清除

[英]Boost Test Fixture object clearing between tests

我在升壓單元測試中遇到問題。 基本上,我創建了一個夾具,它是對資源緩存進行單元測試的套件的一部分。 我的主要問題是在兩次測試之間,資源緩存變得空了。 因此,第一個測試緩存的測試通過,然后第二個測試將失敗,因為第一個測試插入緩存的數據不再存在。 為了解決這個問題,我不得不為第二次測試重新插入數據。 這是故意的還是我做錯了什么? 這是代碼。 最后兩個測試是問題所在。


#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(FontGeneratorTest)
{
    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()

它的目的是。 單元測試的關鍵原則之一是每個測試都是獨立運行 應該為它提供一個可以在其中運行的干凈環境,然后應再次清理該環境,以便測試不會相互依賴。

使用Boost.Test,您可以從命令行指定要運行的測試,因此您不必運行整個套件。 如果您的測試相互依賴,或者取決於它們執行的順序,那么這將導致測試失敗。

夾具用於設置運行測試所需的環境。 如果您需要在測試運行之前創建資源,則夾具應創建它們,然后再對其進行清理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM