SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SimpleTest problem with factory methods

    I've run into a problem trying to run the same tests against a bunch of different classes.

    A shared test case contains the test methods which I want to run against all the classes. Each class has its own test case which extends the shared test case (which in turn extends the UnitTestCase, of course ). Any additional tests for a particular class can be added here.

    The shared test case uses factory methods implemented by each of the derived test cases so that different objects can be supplied to the tests - including objects of each class to be tested.

    This works fine when I run the tests one at a time but not when I add them all to a group test. In the group test, the factory methods have not been implemented, objects aren't being created, and the group test will not run.

    Is this something to do with the way group tests are combined?

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I lost the plot of that story in the telling. Any chance of a seeing code for a simplified example which exhibits the problem?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some simple code with the same problem. Stick all the files in the same dir if you want to try it out.

    Php4 and SimpleTest 1.0.

    PHP Code:
              
              
    // file: Foo.php
              // the class to test:
              
    class Foo
              
    {
                  function 
    isValid()
                  {
                      return 
    true;
                  }
              }
              
              
    // file: TestOfFoo.php
              
    require_once('SharedTestCase.php');
              require_once(
    'Foo.php');
              class 
    TestOfFoo extends SharedTestCase
              
    {
                  function 
    TestOfFoo() 
                  {
                         
    Parent::SharedTestCase();
                  }
                  function 
    setUp() 
                  {
                      
    Parent::_setUp();
                  }
                  function 
    tearDown() 
                  {
                      
    Parent::_tearDown();
                  }
                  function &
    _Factory() 
                  {
                      return new 
    Foo;
                  }
                  function 
    testSomething() 
                  {
                  }
                  function 
    testSomethingElse() 
                  {
                  }
                  
    // etc
              
    }
              
              
    // file: SharedTestCase.php
              
    class SharedTestCase extends UnitTestCase
              
    {
                  function 
    SharedTestCase() 
                  {
                      
    $this->UnitTestCase();
                  }
                  function &
    _Factory() 
                  {
                      
    #die(__FILE__ . ' at line: ' . __LINE__ . '<br />');
                  
    }
                  function 
    _setUp() 
                  {
                  }
                  function 
    _tearDown() 
                  {
                  }
                  function 
    test() 
                  {
                      
    $object =& $this->_Factory();
                      
    $this->assertTrue($object->isValid());
                  }
                  function 
    testSomeMore()
                  {
                  }
                  
    // etc
              
    }
              
              
    // file: file_runner.php
              // this works fine
              
    define('SIMPLE_TEST''your/path/to/simpletest/');
              require_once(
    SIMPLE_TEST 'unit_tester.php');
              require_once(
    SIMPLE_TEST 'reporter.php');
              require_once(
    SIMPLE_TEST 'mock_objects.php');
              require_once(
    'TestOfFoo.php');
              
              
    $test =& new TestOfFoo();
              
    $test->run(new HtmlReporter());
              
              
              
    // file: group_runner.php
              // .. but the group runner doesn't:
              
    define('SIMPLE_TEST''your/path/to/simpletest/');
              require_once(
    SIMPLE_TEST 'unit_tester.php');
              require_once(
    SIMPLE_TEST 'reporter.php');
              require_once(
    SIMPLE_TEST 'mock_objects.php');
              
    $test =& new GroupTest('All FOO Tests');
              
    $test->addTestFile('TestOfFoo.php');
              
    $test->run(new HtmlReporter()); 
    By uncommenting the die() line in the SharedTestCase, you can see that (with the group runner) the _Factory() method hasn't been over-ridden. All works fine with the file runner though.

    I'm going to kick myself if I've missed something really simple. It's been a long night.

    Thanks for your help.

    PS: the reason for all this is to test different objects which implement the same interface without having to copy paste lots of test code. I've been messing around with a db abstraction layer and I planned to use factory methods to instantiate different data access objects, objects to load and destroy sample data in different types of db, db error phrase books, and... I think that's it. The data access objects will have a lot of tests in common and occasionally one or two unique to each class.

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you have to instruct SimpleTest to ignore SharedTestCase, otherwise it runs it as another test.

    Never used it before, but I think the code to do it is:
    PHP Code:
    SimpleTestOptions::ignore('SharedTestCase'); 

  5. #5
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sweatje - that did the trick


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •