1

background:I am writing a Session table for incoming traffic. This table should hold all active UDP/TCP connections.

I am using googletest package to test my implementation. I prepare a parameterised test based on fixture in the following format:

class SessionTest - initialize all staff.

struct ConnectionInfo - holds set of connection parameters (IPs, ports, etc..)

class SessionTestPrepare : SessionTest , testing::WithParamInterface<ConnectionInfo> - initialization.

TEST_P(SessionTestPrepare, test) - holds the test cases and logic.

INSTANTIATE_TEST_CASE_P(default, SessionTestPrepare_ testing::Values(  
ConectionInfo{},  
ConectionInfo{},  
ConectionInfo{},  

)

I noticed that each time new parameters are tested, the SessionTest constructor and Setup function are called (and of course destructor and TearDown).
Note: my sessionTable is declared and initialized here.

  1. Is there a way to avoid calling to SetUp and TearDown after each set of parameter test?
  2. Is there a way to keep the state of my Session Table after each test without make it global (i.e. when testing the second connection parameters, the first is still in table)?
amigal
  • 517
  • 3
  • 8
  • 21

1 Answers1

4

To run set up and tear down only once in a test fixture, use SetUpTestCase and TearDownTestCase instead of SetUp and TearDown. And the shared resources can be stored in fixture with static member variables. For example:

class SessionTestPrepare : public ::testing::WithParamInterface<ConnectionInfo> //...
{
public:
    static void SetUpTestCase();
    static void TearDownTestCase();

    static ConnectionInfo * shared_data;
    //...
}

SetUpTestCase is called before the first parameter test begins and TearDownTestCase is called after the last parameter test ends. You can create/delete the shared resources in these functions.

mcchu
  • 3,309
  • 1
  • 20
  • 19