PHPUnit: 如何为类中的所有测试创建一个调用一次的函数?

我有一个 PHPUnit 测试用例类(由一些测试函数组成)。我想编写一个 oneTimeSetUp()函数,对类中的所有测试调用一次(不像标准的 setUp()函数,对类中的每个测试调用一次)。换句话说,我在寻找一个等效于 JUnit @BeforeClass注释的 PHPUnit。

oneTimeTearDown()函数也有同样的问题。

可以在 PHPUnit 中这样做吗?

65042 次浏览

Take a look at setUpBeforeClass() from section 6 of the PHPUnit documentation.

For the one time tearDown you should use tearDownAfterClass();.

Both this methods should be defined in your class as static methods.

I came to this page with the same question, however the accepted answer is ran on all classes, and for me was not the correct answer.

If you are like me, your first "Integration test" is to clear out the DB, and run migrations. This gets yourself at a database baseline for all test. I am constantly changing migration files at this point, so setting up the baseline is truly part of all tests.

The migration takes a while, so I do not want it run on all tests.

Then I needed to build up the database testing each piece. I need to write an order test, but first I need to create some products and test that, then I need to test an import fuction.

So, what I did is SUPER easy, but not explained extremely well on the internet. I created a simple test to setup the database. Then in your phpspec.xml file add a testsuite....

<testsuite name="Products">
<file>tests/in/SystemSetupTest.php</file>
<file>tests/in/ProductTest.php</file>
<file>tests/in/ProductImportTest.php</file>
</testsuite>

And in the the SystemSetupTest.php ....

class SystemSetupTest extends ApiTester
{


/** @test */
function system_init()
{
fwrite(STDOUT, __METHOD__ . "\n");
self::createEM(); //this has all the code to init the system...
}
}

Then execute it like:

phpunit --testsuite Products

In the end, its a ton easier. It will allow me to build up my system correctly.

Additionally I am using laravel 5. When using setUpBeforeClass() I end up with bootstrap issues, which I am sure I can fix, but the method I use above works perfect.

The bootstrap option can be used on these cases.

You can call it from the command line

phpunit --bootstrap myBootstrap.php

Or put it in the XML file, like this:

<phpunit bootstrap="myBootstrap.php">
...
</phpunit>

Expanding on accepted answer:

None of the setUpBeforeClass(), tearDownAfterClass(), @beforeClass, @afterClass runs in the object context (static methods). You can work around that restriction by guarding any @before code with a static property instead, like so:

class MyTest extends PHPUnit\Framework\TestCase
{
private static $ready = false;


/**
* @before
*/
protected function firstSetUp()
{
if (static::$ready))
return;


/* your one time setUp here */


static::$ready = true;
}
}

It can't be used for @after, however, because there's no way of saying when the last test was called.

setUpBeforeClass() is the way to do this if all of your tests are literally contained within a single class.

However, your question sort of implies that you may be using your test class as a base class for multiple test classes. In that case setUpBeforeClass will be run before each one. If you only want to run it once you could guard it with a static variable:

abstract class TestBase extends TestCase {


protected static $initialized = FALSE;
  

public function setUp() {
    

parent::setUp();


if (!self::$initialized) {
// Do something once here for _all_ test subclasses.
self::$initialized = TRUE;
}
}


}

A final option might be a test listener.