$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: rogeeff (rogeeff_at_[hidden])
Date: 2001-12-21 12:24:25
--- 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."
> 
> 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();
}
> 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?
> 
> 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. 
  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.
> 
> Is there any (other) way to test a method associated with a given 
class
> *instance*?, or it can only test (conceptually static) class 
methods.
> 
> TIA,
> 
> Fernando Cacciola
> Sierra s.r.l.
> fcacciola_at_g...
> www.gosierra.com
> 
Gennadiy.