Passing arguments to the class constructor in TEST_CASE_METHOD?

534 views
Skip to first unread message

Jake Vizzoni

unread,
Oct 6, 2016, 2:51:33 PM10/6/16
to CATCH
Is it possible to pass arguments to the class constructor when using the TEST_CASE_METHOD() macro?

For example:

TEST_CASE_METHOD(MyTestClass, "Test something", "[some tag]") {
// .. code body
}

Where MyTestClass takes one argument in its constructor:
class MyTestClass {
public:
   MyTestClass(int arg);
}

Thanks,
Jake

Jens Alfke

unread,
Oct 6, 2016, 6:08:42 PM10/6/16
to Jake Vizzoni, CATCH

On Oct 6, 2016, at 11:51 AM, Jake Vizzoni <jv.ass...@gmail.com> wrote:

Is it possible to pass arguments to the class constructor when using the TEST_CASE_METHOD() macro?

Nope, not directly.

Just make a trivial subclass of MyTestClass with a no-arg constructor that passes the desired arg to MyTestClass:

class MyTestSubclass : public MyTestClass {
public:
MyTestSubclass() :MyTestClass(42) { } // 42 is the desired argument
};

TEST_CASE_METHOD(MyTestSubclass, “test something”, “[some tag]”) {
}

—Jens

Jake Vizzoni

unread,
Oct 6, 2016, 8:06:18 PM10/6/16
to CATCH, jv.ass...@gmail.com
Thanks for the reply and confirmation Jens! :)

I've been doing something similar in the test code so far. My test code is quickly bloating because I'm writing many subclasses for this purpose. Guess it's time to rethink the design of my class to compensate for this limitation in Catch.

Jens Alfke

unread,
Oct 6, 2016, 8:09:12 PM10/6/16
to Jake Vizzoni, CATCH

On Oct 6, 2016, at 5:06 PM, Jake Vizzoni <jv.ass...@gmail.com> wrote:

I've been doing something similar in the test code so far. My test code is quickly bloating because I'm writing many subclasses for this purpose. Guess it's time to rethink the design of my class to compensate for this limitation in Catch.

What is it you’re trying to do, that requires a parameter to the constructor?

—Jens

Jake Vizzoni

unread,
Oct 7, 2016, 1:00:20 PM10/7/16
to CATCH, jv.ass...@gmail.com
I'm testing small functions that are part of a bigger algorithm. Each test opens two files: a file containing the input vectors, and the reference output .  I have a base class that handles the file open/close, reading, and error handling. The child classes use this base class by passing in the input/output reference files through the constructor. So for every set of files I need to create a new child of the file handling base class.

I've explored the possibility of just using a file open/close method as part of the base class but then you are essentially doing what that class is meant to do for you - in other words, you need to handle the error conditions and cleanup appropriately in the code instantiated an instance of the class.

Jens Alfke

unread,
Oct 8, 2016, 3:50:23 PM10/8/16
to Jake Vizzoni, CATCH

On Oct 7, 2016, at 10:00 AM, Jake Vizzoni <jv.ass...@gmail.com> wrote:

I've explored the possibility of just using a file open/close method as part of the base class but then you are essentially doing what that class is meant to do for you - in other words, you need to handle the error conditions and cleanup appropriately in the code instantiated an instance of the class.

Well, passing the file to the constructor doesn’t seem like the right approach for use with Catch. Instead, add an `open` method to the base class, and move the open-file code from the constructor to there.

I don’t understand what you mean by "but then you are essentially doing what that class is meant to do for you” — wouldn’t you just start your test case with one extra line like `open(“/path/to/file”);`? That doesn’t sound like much work.

—Jens
Reply all
Reply to author
Forward
0 new messages