Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing data between two test cases in google test framework

Tags:

c++

googletest

I'm testing a particular implementation of a algorithm with random input data using google test framework. I want to use the same random input data for another implementation of the same algorithm. I'm testing the two implementation using two separate test cases. Is there a way to share the random input data from one test case to another.

like image 596
Praveen Avatar asked Dec 15 '22 04:12

Praveen


1 Answers

Look at this section "Sharing Resources Between Tests in the Same Test Case" in https://github.com/google/googletest/blob/master/docs/advanced.md#sharing-resources-between-tests-in-the-same-test-suite

Following static methods in your test fixture class :

static void SetUpTestCase()

static void TearDownTestCase()

serve for acquisition and release of shared resources, respectively.

Of course the member variables representing that shared objects must be defined as static members of your test fixture class (static methods have access only to static members). Don't forget global variable like declaration of those members (compiler throws error if you do not declare them like that).

SetUpTestCase() is called by Google Test Framework before execution of the first test and TearDownTestCase() after execution of the last one.

Everything I mentioned is comprehensively described in above reference. There is also example there.

like image 162
Marek Honek Avatar answered Feb 20 '23 04:02

Marek Honek