Fixture inheritance

2,919 views
Skip to first unread message

Philippe Langevin

unread,
Aug 28, 2013, 7:10:49 PM8/28/13
to googletes...@googlegroups.com
Hello there !

I know that fixture can be derived.  I already have fixtures like those :

class HeavyTestCase : public ::testing::Test
{
protected:
  static void SetUpTestCase()     {  /* Do HEAVY initialisation */  }
  static void TearDownTestCase()  {  /* Do HEAVY cleanup */  }
} ;


class BasicFixture1 : public HeavyTestCase
{
public:
  virtual void SetUp()     {  /* Small per test set up */  }
  virtual void TearDown()  {  /* Small per test cleanup */  }
} ;

class BasicFixture2 : public HeavyTestCase
{
public:
  virtual void SetUp()     {  /* Small per test set up */  }
  virtual void TearDown()  {  /* Small per test cleanup */  }
} ;


TEST_F( BasicFixture1, test1_1 )
{
  // Do tests
}
TEST_F( BasicFixture1, test1_2 )
{
  // Do tests
}
TEST_F( BasicFixture2, test2_1 )
{
  // Do tests
}

What I would need is that HeavyTestCase's static SetUptestCase/TearDownTestCase is only called once because BasicFixture1 and BasicFixture2 shares the same parent.

In other words, test "architecture" should be :

HeavyTestCase
  BasicFixture1
    test1_1
    test1_2
  BasicFixture2
    test2_1

Actually, a workaround could also do it bit I would really appreciate being able to call a test :
HeavyTestCase.BasicFixture1.test1_1

Thank you

Philippe Langevin

unread,
Sep 16, 2013, 10:33:23 PM9/16/13
to googletes...@googlegroups.com
Is that something possible? Or does some body has something différent to suggest?

Vlad Losev

unread,
Sep 20, 2013, 9:52:20 PM9/20/13
to Google C++ Testing Framework
I suggest putting the process-wide initialization and clean-up into an Environment.


--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Philippe Langevin

unread,
Oct 17, 2013, 4:02:53 PM10/17/13
to googletes...@googlegroups.com
For those who want similar feature, I finally found a clean and almost automated solution for such a behavior.
  1. I created a class that implements EmptyTestEventListener::OnTestStart and EmptyTestEventListener::OnTestEnd
  2. Those two methods checks if they must execute by comparing received test_info.name() with a list of required tests name
  3. I then registered my new listener to gtest listeners
Now my OnTestStart and OnTestEnd act as SetUp and TearDown methods.


Thank you

whem...@qiggroup.com

unread,
Nov 12, 2013, 11:14:37 AM11/12/13
to googletes...@googlegroups.com
If you really need per-test case initializations, you can use gtest introspection to track the test case name. For example, you can use the following snippet to discover the test  in your "Heavy" SetUp() and TearDown() methods to see whether the test case name has changed and (by using a class variable to remember the last test case name) return without doing anything if it has not changed.

const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();

string tcName = test_info->test_case_name();


We use the above code to generate test case-unique file names but it has many other potential uses.
Reply all
Reply to author
Forward
0 new messages