How to write test dependencies in Yii

Hello, currently I am learning to write a test unit for my application.

I write a test function that depends on other test function. So it must running the first test and continue to second test.

I am trying to write like this


public function testConfirm(){

  ...

   $student = $this->student('first');

}


        /**

     * @depends testConfirm

     */

public function testUnConfirm(){

     ....

     $student->unconfirm(); //<= it doesn't recognise the $student.

}

but unfortunately it’s not working. Did I miss something?

Make student a private class member.

But as far as I know, PHPUnit provide annotation of @depends to make dependencies test

I read it from here : PHPUnit Documentation

Yes. test1 @depends test2 means “don’t even try to run test2 if test1 failed”.

I think you could do:




public function testConfirm(){

   $student = $this->student('first');

   //...

   return $student;

}


/**

 * @depends testConfirm

 */

public function testUnConfirm($student){

     $student->unconfirm();

}



Isn’t the whole point of unit tests that they are units, ie self-contained. ?

They shouldn’t depend on a previous test, or am I wrong?

Ahh… I see…

I’ve finally understood…

Thanks samdark & galymzhan…

@jacmoe,

well, I think in most case we need dependencies test. Supposed we want to test create and delete. It’s faster to test the create first, and get that result, to test in delete case.

Also see the notes here (Test Dependencies):

http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html

If you guys are using Netbeans and use the code snippet /* [Enter]

to create your comments it will do them like this:

/*

*/

If you add the @depends annotation it will look like this but will not work.

/*

 * @depends test


 */

This is because your freacking block comment needs the two stars at the top in order to recognize the annotations. This is a small detail but I was stuck here until I noticed the second star missing. Your code should look like this:

/**

 * @depends test


 */

You are right,I meet the same problem,and it works now