Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestSuite ends up creating instances of the clases in the hiearchy of a test #57

Open
alejandro-rusell opened this issue Jul 28, 2018 · 0 comments

Comments

@alejandro-rusell
Copy link

Hi,

We're having an issue with the way classes are handled when building a TestSuite.

We have a hierarchy of test classes that eventually derive from testcase. Some of the classes in the hierarchy are abstract, but other aren't. When the TestSuite is built, the non-abstract classes are added to the TestSuite and an instance of them is constructed. Those classes don't have test methods, so only the constructor is run. The problem is that their constructors perform a long initialization, which is run multiple times (once for each 'parent' class created).

Our hierarchy of classes is something like:

abstract class BaseTest extends UnitTestCase {}
class CommonTest extends BaseTest {}
class TestWithDb extends CommonTest {}
class ATest extends TestWithDb {}

When we try to run ATest, three instances are created: one of ATest (as expected), but also one of TestWithDb and one of CommonTest.

My question is what is the rationale behind it? If the class of the test has a parent class, why is the latter instantiated on its own?

The sequence of the code is:

In SimpleFileLoader:

public function load($test_file)
{
    $existing_classes = get_declared_classes();
    $existing_globals = get_defined_vars();
    include_once($test_file);
    $new_globals = get_defined_vars();
    $this->makeFileVariablesGlobal($existing_globals, $new_globals);
    $new_classes = array_diff(get_declared_classes(), $existing_classes);
    if (empty($new_classes)) {
        $new_classes = $this->scrapeClassesFromFile($test_file);
    }
    $classes = $this->selectRunnableTests($new_classes);

    return $this->createSuiteFromClasses($test_file, $classes);

}

scrapeClassesFromFile(): finds all the classes of our hierarchy.
selectRunnableTests(): marks as ignored only abstract classes
createSuiteFromClasses(): ignores only the parent of ignored classes, creates one instance of the remaining ones.

Regards,

Alejandro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant