constructor/destructor vs setUp/tearDown?

1,155 views
Skip to first unread message

Mikhail Balakhno

unread,
Aug 1, 2014, 11:27:43 AM8/1/14
to cxxtes...@googlegroups.com
I like CxxTest over almost all other unit test framework for it clean syntax and brevity. But one thing that I really can't get comfortable with, is the use of setUp/tearDown functions instead of test class constructur/destructor. It means that you HAVE to declare all members as pointers in order to get a clean state for each test.

That's seems a strange decision to me. I don't think it's a technical issue, because CxxTest employs Python preprocessing, and can produce any runner code.  So it's probably done so by design, but I can't think of any advantages this approach can give. 

Keeping state between tests, IMHO, is almost always a bad idea, because tests should be independent. Moreover, it implies certain order of test execution, which is not guaranteed.

Bill Hart

unread,
Sep 24, 2014, 12:42:27 AM9/24/14
to cxxtes...@googlegroups.com
Mikhail:

The use of setUp/tearDown follows a convention that is very standard.  These functions are called before and after every test is executed,
which has a different test semantics from what you suggest.

I think that you're imagining that the class gets constructed for each test, but the testing class is constructed once and the setUp/tearDown functions are used to manage the testing context.  There are also functions that can setup global context for all tests.

If we changed the logic to construct the testing class each time a test is run, then I think we'd lose the ability to control global context.

Does that make sense?

--Bill

Mikhail Balakhno

unread,
Nov 9, 2014, 4:07:33 AM11/9/14
to cxxtes...@googlegroups.com
Hello Bill!

Thank you for your response.

I understand that CxxTest only construct each test fixture class once. That, BTW, differs from behavior of Google Tests and MS Visual Studio Unit Tests (which, I think, pretty much drive industry standards in unit testing). Not that I think all unit test frameworks should behave the same way, but this decision was made for a reason in those frameworks.

And this reason, I think, is that you don't need (or want) to keep your fixture state between tests. If, as you say, you want to control global context, you should use global fixtures, which are also present in CxxTest. 

If you are using test class state for global state control between tests, then you are introducing some kind of ordering between tests (i.e., you expect that one test will be executed before the other), which is quite bad, IMHO.

I'm in no way making any demands here. Just trying to understand the best way to use CxxTests :)

--
You received this message because you are subscribed to a topic in the Google Groups "CxxTest Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cxxtest-forum/r7RGxJ3zOV4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cxxtest-foru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
С уважением,
Балахно Михаил
Reply all
Reply to author
Forward
0 new messages