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

setUp and setUpBeforeClass run before filters/groups applied #261

Closed
banks opened this issue May 31, 2011 · 9 comments
Closed

setUp and setUpBeforeClass run before filters/groups applied #261

banks opened this issue May 31, 2011 · 9 comments

Comments

@banks
Copy link

banks commented May 31, 2011

I'm not sure if this is intentional but I don't see the rationale if it is. I also couldn't find documentation or other issues to suggest this is a known/discussed topic.

Basically: I user the filter and/or groups options to limit the tests run by the test runner as documented.

I've realised though that since the code that calls setUp() and setUpBeforeClass() methods is before the filtering is applied, (code link) every test in my suite will have setUp() run and every test case will have setUpBeforeClass(). This means any expensive setup work done is run for the whole suite even when the tests that require the work are not being run.

I'm aware that you can specify specific test case classes to run and thus avoid this but in a very large test suite, the flexibility of filter and groups is very useful. It comes with a huge cost of running potentially thousands of setup routines though just to run a single simple test.

If this is intentional behaviour, it would be great to understand the rationale behind it. If not, it appears a relatively simple fix and I am can fork and make the required changes.

Thanks for you time and effort on this PHPUnit!

@banks
Copy link
Author

banks commented Aug 9, 2011

Am I the only person who thinks this is an issue? Certainly all other developers I work with agree that this appears to be totally unexpected and unnecessary behaviour?

I am totally willing to fix this myself and contribute the patch but I can't believe no one else could care about something that, in my experience, massively impacts on the usability of PHPUnit to construct and run large test suites.

Perhaps I'm missing something here. Please, anyone, share your thoughts on why this is written as it is and whether there is any reason not to simple move the setup calls to be after the filtering check.

@BRMatt
Copy link
Contributor

BRMatt commented Aug 9, 2011

I'd say this is a big issue. This bit me recently when a data provider relied on functionality that wasn't available. Even though the tests that used the provider had been excluded with groups the provider was still being invoked.

@dharkness
Copy link

I agree that this seems to be unintentional. I can think of no reason to run those methods if none of the tests in the test case will be run.

@naderman
Copy link
Contributor

Just ran into this problem as well. Any news on this?

@tarjei
Copy link

tarjei commented Oct 25, 2011

Hi, the workaround is to add a static variable + a check in setup() :

if (self::$processManager === null) {
        self::$processManager = new ProcessManager();
        self::$processManager->startBackgroundJob(__DIR__ . "/../../../../../mongodb/run-mongo.php");
}

And then:

public static function tearDownAfterClass()
{
    if (self::$processManager !== null) {
        echo "\n\nKilling all pids!\n";
        self::$processManager->killAll();
    }
}

@banks
Copy link
Author

banks commented Nov 17, 2011

@tarjei not sure how that helps?

The problem is not that the constructor runs multiple times. Given PHPunit's runner that is not surprising in some circumstances. The issue I'm complaining about is that the setup is run at all in the case where you are not actually running any tests from the class in question. In my case I don't see multiple setupBeforeClass runs for a single class, but I do see all of the setupBeforeClass methods for my entire testsuite run even though I used filter/groups to select only one specific test case.

@edorian
Copy link
Contributor

edorian commented Nov 18, 2011

Yes is this an issue related to #10.

With the current way of running the tests the filtering isn't applied before TestSuite.php executes the setup.

With a big test suite and a phpunit.xml what should work is using directory based filtering with phpunit tests/module/stuff but for example for the phpbb3 test suite that doesn't work out so there are cases where --filter is the only option.

The only idea i have to work around this is to lazy initialize your stuff in the first call to setUp but thats rather hackish.

I'm quite sure that this will be taken care of by #10 but if you need this sooner and the patch is rather small/manageable this might get into PHPUnit 3.7 (Not 3.6.x as it breaks bc).

The workaround by tarjei works as the code is in setUp and the self:: could just as well be self::$setupBeforeFirstTestCase

@banks
Copy link
Author

banks commented Nov 22, 2011

The workaround by tarjei works as the code is in setUp and the self:: could just as well be self::$setupBeforeFirstTestCase

My apologies @tarjei, I misunderstood the intention.

@edorian, thanks for the comment yes #10 should fix this issue, I guess we'll have to wait and see.

@redgeoff
Copy link

redgeoff commented Nov 3, 2015

Here is a post on SO that may help others who are still having problems with the filtering not preventing the fixtures from being run: PHPUnit filtering not working with setUpBeforeClass() and tearDownAfterClass()

I'm sorry if there is a better way that I have just missed in the docs.

axiac pushed a commit to axiac/phpunit that referenced this issue May 8, 2018
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

8 participants