I have a PHPUnit test case class (consisting of some test functions). I would like to write a oneTimeSetUp()
function to be called once for all my tests in the class (unlike the standard setUp()
function which is called once for each test in the class). In other words, I'm looking for a PHPUnit equivalent to the JUnit @BeforeClass
annotation.
Same question with a oneTimeTearDown()
function.
Is it possible to do so in PHPUnit?
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:
A final option might be a test listener.
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.
The
bootstrap
option can be used on these cases.You can call it from the command line
Or put it in the XML file, like this:
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:It can't be used for
@after
, however, because there's no way of saying when the last test was called.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....
And in the the SystemSetupTest.php ....
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.