28 września 2012

Konstruktor a SetUp (Google Test)

Chciałem się dowiedzieć jak właściwie działa metoda SetUp(), dla klas testów w konfrontacji z konstruktorem takiej klasy.
#include <gmock/gmock.h>
#include <stdio.h>

using namespace ::testing;

class SimpleTest : public Test
{
public:
    SimpleTest() {
        std::cout << "Constructor()" << std::endl;
    }

    void SetUp() {
        std::cout << "SetUp()" << std::endl;
    }

    void TearDown() {
         std::cout << "TearDown()" << std::endl;
    }

    ~SimpleTest() {
        std::cout << "Destructor()" << std::endl;
    }
};

TEST_F(SimpleTest, test1)
{
    std::cout << "test1 " << std::endl;
}

TEST_F(SimpleTest, test2)
{
    std::cout << "test2 " << std::endl;
}

int main(int argc, char *argv[])
{
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}
Najwyraźniej każde makro TEST_F, tworzy klasę dziedziczącą z naszej klasy testującej. SetUp() oraz Constructor() wołane są za każdym razem przed rozpoczęciem testu. Odpowiednio TearDown() oraz Destructor(), także wołane są na końcu każdego testu.
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from SimpleTest
[ RUN      ] SimpleTest.test1
Constructor()
SetUp()
test1 
TearDown()
Destructor()
[       OK ] SimpleTest.test1 (0 ms)
[ RUN      ] SimpleTest.test2
Constructor()
SetUp()
test2 
TearDown()
Destructor()
[       OK ] SimpleTest.test2 (0 ms)
[----------] 2 tests from SimpleTest (1 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (4 ms total)
[  PASSED  ] 2 tests.

Brak komentarzy:

Prześlij komentarz