Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-12-21 12:40:07


----- Original Message -----
From: rogeeff <rogeeff_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, December 21, 2001 2:24 PM
Subject: [boost] Re: Unit Test Framework: Class Test Cases

> --- In boost_at_y..., "Fernando Cacciola" <fcacciola_at_g...> wrote:
> > Gennady,
> >
> > I'm using the Unit Test Framework for testing my own code.
> >
> > I've found that you can add a test case being a class member
> function:
> >
> > class A
> > {
> > void foo() ;
> > } ;
> > ...
> > test->add( BOOST_TEST_CASE( &A::foo) );
> >
> > First, when I saw this sort of example in the documentation, I
> figured that
> > there was a mistake, and that
> > foo() is a static method, because there isn't any instance of A
> created (as
> > shown by the example)
> >
> > Then I realize, that actually, the test case internally creates
> a 'new'
> > A::A() by itself.
> >
> > This should be properly documented.
>
> >From the documentation:
>
> "The class_test_case is responsible for the creation and the
> destroying of the user's test class instance. It's recommended to use
> the class_test_case *only* if you can't implement a test logic in a
> free function."
>
I see.
Then this paragraph should be referenced in

>
> >
> > Second: in my particular case, I NEED to test A::foo() but upon a
> properly
> > constructed A instance.
>
> use free_function and do whatever you need. For example:
>
> void test_A()
> {
> A a(...)
> a.foo();
> }
>
>
See below...

> > It makes no sense at all to test A::foo() on a default constructed
> A.
> >
> > That is, I need something like:
> >
> > class A
> > {
> > A ( ... ) ;
> > void foo() ;
> > } ;
> >
> > ...
> > A a(....);
> > test->add( BOOST_TEST_CASE( make_closure(a, &A::foo) ) );
>
> First of all you will be hit by lifetime issues again. But what is
> more important: I think free function implementation should suit for
> you. Is it?
>
See below....

>
> >
> > Adding this functionality to unit_test_suite.hpp wouldn't be so
> difficult,
> > but I encountered a problem:
> >
> > What's the rational of test_case::init()/destroy().
> >
> > It *creates* and *destroys* test cases (in this cases 'A's) on
> behalf of the
> > user.
> >
> > In the example case above, if the test_suite() destroys 'a' and
> then creates
> > another 'A'by default, the 'foo' method being tested will behave
> > unexpectedly.
> >
> > So, is the automatic init()/destroy() scheme really needed? What's
> its
> > purpose?
>
> In reality usage of class_test_case is limited to the situations
> where you for any reason need to share information between test
> cases. The instance of user class is just a storage for this
> information. As an example let say you are testing very heavy class
> A. So you have test_construction test case and test_access test case.
> You could want to use preconstructed object in a test_access and
> construct it in test_construction.
>
Could you give a concrete example?
I can't see how, since the test classes are constructed/destructed on behalf
of the user, test cases can share
information.

> There could be some other rare cases when you need class_test_case.
> Another remark from doc:
> "Due to usage of templates in an implementation of the
> class_test_case, a compilation can be longer would you use the
> function_test_case."
> So use it *only* if you can't implement the same logic in a free
> function.
>
>
The problem is that I need to transfer information from the command line
into the test case.
How can I do that with a free function?

(of course I could use gobal static data, but I was hoping that no to be
necessary)

Thanks,

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk