Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to preserve instance variables between phpunit tests?

Tags:

I'm writing functional tests for a SOA system so need to test a backend subsystem from the frontend.

It's a standard CRUD system. I have a test that tests I can create an object, and it returns me the ID of the new object. In subsequent tests I want to edit and then delete this object, but phpunit seems to be re-instantiating the test class each time, so I lose my instance variables.

How can I achieve this? Running functional tests on each server in the architecture isn't an option.

like image 914
Paul J Avatar asked Jul 18 '11 16:07

Paul J


1 Answers

One way to pass stuff between tests is to use the @depends annotation

public function testCreate() {     //...     return $id; }  /**  * @depends testCreate  */ public function testDelete($id) {     // use $id } 

if you do it like that the "delete" test will be skipped if the creation doesn't work.

So you will only get one error if the service doesn't work at all instead of many failing tests.


If you don't want to do that or that doesn't match your case for whatever reason static class variables can also be an option, but you should be really sure that you know you absolutely need them :)

like image 80
edorian Avatar answered Oct 12 '22 13:10

edorian