Lhaz
読み取り中…
検索中…
一致する文字列を見つけられません
マクロ定義
Test Construction

マクロ定義

#define BEGIN_TEST(x)   BEGIN_TEST_FUNC(x) BEGIN_TEST_BLOCK
 
#define END_TEST   END_TEST_BLOCK END_TEST_FUNC
 
#define BEGIN_TESTF(x, f)   BEGIN_TEST_FUNC(x) BEGIN_FIXTURE_BLOCK(f) BEGIN_TEST_BLOCK
 
#define END_TESTF   END_TEST_BLOCK END_FIXTURE_BLOCK END_TEST_FUNC
 
#define FIXTURE(x)   ¥
 
#define SETUP(x)   void FIXTURE_##x::Setup()
 
#define TEARDOWN(x)   void FIXTURE_##x::Teardown()
 

詳解

These are the basic macros you will use for creating tests and doing setup/teardown.

Example:
// Variables local to this file
//
namespace
{
int s_myNumber = 0;
}
FIXTURE(MyFixture); // declares a fixture
SETUP(MyFixture)
{
// Do some stuff here that you want done before a test starts. It can
// include accessing static variables. An example would be creating
// a file (and then deleting it in TEARDOWN).
s_myNumber = 5;
}
TEARDOWN(MyFixture)
{
// Undo whatever you did in the SETUP.
s_myNumber = 0;
}
// This test uses a fixture (the second argument). Setup and Teardown will
// be run before and after the body of the test, respectively.
BEGIN_TESTF(MyTestThatUsesAFixture, MyFixture)
{
// Do some tests. You can assume s_MyNumber is 5.
WIN_ASSERT_EQUAL(5, s_myNumber);
}
BEGIN_TEST(MyTestThatDoesNotUseFixtures)
{
// Do some tests. Do not rely on any variables set in the setup or
// teardown.
}
#define BEGIN_TESTF(x, f)
Definition WinUnit.h:169
#define END_TESTF
Definition WinUnit.h:173
#define TEARDOWN(x)
Definition WinUnit.h:380
#define FIXTURE(x)
Definition WinUnit.h:349
#define SETUP(x)
Definition WinUnit.h:369
#define BEGIN_TEST(x)
Definition WinUnit.h:152
#define END_TEST
Definition WinUnit.h:162
#define WIN_ASSERT_EQUAL(expected, actual,...)
Definition WinUnit.h:230

マクロ定義詳解

◆ BEGIN_TEST

#define BEGIN_TEST (   x)    BEGIN_TEST_FUNC(x) BEGIN_TEST_BLOCK

This macro indicates the beginning of a test. It writes the first part of a function named "x" ensuring you use the proper function prototype, and that the function name is exported (thereby readable by WinUnit.exe). Additionally it includes a try, which is closed in a catch by the END_TEST macro.

◆ BEGIN_TESTF

#define BEGIN_TESTF (   x,
 
)    BEGIN_TEST_FUNC(x) BEGIN_FIXTURE_BLOCK(f) BEGIN_TEST_BLOCK

This macro indicates the beginning of a test that uses a fixture. It needs an extra try/catch to handle the case where Teardown (i.e. the destructor of the "fixture object") throws exceptions (see documentation for terminate() function for why this could be a problem)

◆ END_TEST

#define END_TEST   END_TEST_BLOCK END_TEST_FUNC

This macro indicates the end of a test. It's necessary because it includes a catch block. Since these tests use C++ exceptions to indicate failure, and when thrown, they are crossing a DLL boundary, they end up being treated as an unhandled exception, and the stack doesn't get unwound. This results in unexpected behavior if you were counting on C++ scoping effects. The solution is to put a try/catch block around every exported function, and just do a throw in the catch block.

◆ END_TESTF

This macro indicates the end of a test that uses a fixture.

◆ FIXTURE

#define FIXTURE (   x)    ¥

This macro indicates a fixture of name x.

注釈
Although you can put more than one fixture in one .cpp file it is a good idea to only use one. Also, it is recommended that you use the fixture in all tests in the file, for consistency. If you declare a fixture and use it anywhere, you will need to also define SETUP and TEARDOWN for the fixture. ()

◆ SETUP

#define SETUP (   x)    void FIXTURE_##x::Setup()

This macro starts the setup function for fixture x. It's used like:

{
// Do stuff for setup (you can access file-static variables)
// This will be called before every test that uses fixture x.
}

()

◆ TEARDOWN

#define TEARDOWN (   x)    void FIXTURE_##x::Teardown()

This macro starts the teardown function for fixture x. It's used like:

{
// Do stuff that undoes whatever you did in setup.
// This will be called after every test that uses fixture x.
}

()