Reputation: 777
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.
Upvotes: 22
Views: 8692
Reputation: 29
Use setUp() method of TestCase class.
protected function setUp(): void
{
parent::setUp();
//init something
}
Upvotes: 0
Reputation: 26139
Since you want to run each testcase once, you can store the instance variables on class level for example in static variables, or in an external singleton/global store uses get_class($testCase)
for keys...
Upvotes: 5
Reputation: 38961
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 :)
Upvotes: 41
Reputation: 777
I guess I'll just run the 'create' test before I run the 'edit' or 'delete', and set the instance variable accordingly.
Upvotes: 0