Dynamic Parameterized Tests

2,068 views
Skip to first unread message

Michael Bailey

unread,
Nov 15, 2010, 11:38:18 AM11/15/10
to Google C++ Testing Framework
I am in the process of converting our current custom testing framework
into the gtest framework. Is there a way to have dynamic
parameterization for parameterized tests? We have a collection of
files in one directory which each need to be analyzed. I would love
to have a function which examines the directory and creates a test for
each file, but I don't know if that is possible. I recognize that I
can explicitly reference each file, and I may have to do that. But I
would much prefer a situation where the files are picked up
automatically. Any thoughts?

Pat J

unread,
Nov 15, 2010, 11:54:34 AM11/15/10
to Google C++ Testing Framework
I didn't find any similar functionality built in to the framework but
I was able to add it very easily. I didn't add it directly to the
framework because I think it's hard to generalize and can vary with
each testing need.

I created a base class that derives from ::testing::TestWithParam with
the param being a class I defined. Each instance of the param comes
from some data that I read in from an xml configuration file. That
could be adapted to what you are looking for.

Each parameterized test class derives from my base class and when I
call the INSTANTIATE_TEST_CASE_P macro, I provide a static function
that returns a vector of my param objects. The vector is constructed
by me reading the xml configuration file. You can do the same by
scanning files in some directory.

It's not elegant but I am finding it to be really flexible for our
testing needs.

- Pat

Zhanyong Wan (λx.x x)

unread,
Nov 15, 2010, 12:24:29 PM11/15/10
to Pat J, Google C++ Testing Framework, jinx...@gmail.com
On Mon, Nov 15, 2010 at 8:54 AM, Pat J <jen...@gmail.com> wrote:
> I didn't find any similar functionality built in to the framework but
> I was able to add it very easily.  I didn't add it directly to the
> framework because I think it's hard to generalize and can vary with
> each testing need.
>
> I created a base class that derives from ::testing::TestWithParam with
> the param being a class I defined.  Each instance of the param comes
> from some data that I read in from an xml configuration file.  That
> could be adapted to what you are looking for.
>
> Each parameterized test class derives from my base class and when I
> call the INSTANTIATE_TEST_CASE_P macro, I provide a static function
> that returns a vector of my param objects.  The vector is constructed
> by me reading the xml configuration file.  You can do the same by
> scanning files in some directory.
>
> It's not elegant but I am finding it to be really flexible for our
> testing needs.

This *is* the way it's supposed to work. I don't think we need to
"add" it to gtest, as it's meant to be a user extension.

See http://code.google.com/p/googletest/wiki/AdvancedGuide#Value_Parameterized_Tests
-- you can use the ValuesIn(container) expression to define the
parameters, where container can be an STL container (e.g. vector)
expression. For example, ValuesIn(GetFilesInDir("foo/bar")) (assuming
GetFilesInDir() returns a vector of file paths).

>
> - Pat
>
> On Nov 15, 11:38 am, Michael Bailey <jinxid...@gmail.com> wrote:
>> I am in the process of converting our current custom testing framework
>> into the gtest framework.  Is there a way to have dynamic
>> parameterization for parameterized tests?  We have a collection of
>> files in one directory which each need to be analyzed.  I would love
>> to have a function which examines the directory and creates a test for
>> each file, but I don't know if that is possible.  I recognize that I
>> can explicitly reference each file, and I may have to do that.  But I
>> would much prefer a situation where the files are picked up
>> automatically.  Any thoughts?
>

--
Zhanyong

Steve Jaffe

unread,
Nov 15, 2010, 12:00:44 PM11/15/10
to Pat J, Google C++ Testing Framework
I do the same: it works well and is easy to understand. I don't see any need to clutter the framework with this.


From: Pat J <jen...@gmail.com>
To: Google C++ Testing Framework <googletes...@googlegroups.com>
Sent: Mon, November 15, 2010 11:54:34 AM
Subject: [googletest] Re: Dynamic Parameterized Tests

Michael Bailey

unread,
Nov 15, 2010, 12:30:01 PM11/15/10
to Google C++ Testing Framework
Oh... very cool. Thank you. I looked around the documentation but
didn't catch that you could pass a function into ValuesIn(). I
figured it had to be static. Thanks a bunch.

On Nov 15, 10:24 am, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
> On Mon, Nov 15, 2010 at 8:54 AM, Pat J <jena...@gmail.com> wrote:
> > I didn't find any similar functionality built in to the framework but
> > I was able to add it very easily.  I didn't add it directly to the
> > framework because I think it's hard to generalize and can vary with
> > each testing need.
>
> > I created a base class that derives from ::testing::TestWithParam with
> > the param being a class I defined.  Each instance of the param comes
> > from some data that I read in from an xml configuration file.  That
> > could be adapted to what you are looking for.
>
> > Each parameterized test class derives from my base class and when I
> > call the INSTANTIATE_TEST_CASE_P macro, I provide a static function
> > that returns a vector of my param objects.  The vector is constructed
> > by me reading the xml configuration file.  You can do the same by
> > scanning files in some directory.
>
> > It's not elegant but I am finding it to be really flexible for our
> > testing needs.
>
> This *is* the way it's supposed to work.  I don't think we need to
> "add" it to gtest, as it's meant to be a user extension.
>
> Seehttp://code.google.com/p/googletest/wiki/AdvancedGuide#Value_Paramete...
Reply all
Reply to author
Forward
0 new messages