views:

116

answers:

1
class TestClass extends PHPUnit_Framework_TestCase {
 function testSomething() {
   $class = new Class();
   $this->assertTrue($class->someFunc(1));
 }

 function testSomethingAgain() {
  $class = new Class();
  $this->assertFalse($class->someFunc(0));
  }
}

Hi, do I really have to create $class for every test function I create? Or is there an unknown constructor like function that I have yet to discover, since constructors don't seem to work in PHPUnit.

Thanks

+2  A: 

You can use the setUp() and tearDown() methods with a private or protected variable. setUp() is called before each testXxx() method and tearDown() is called after. This gives you a clean slate to work with for each test.

class TestClass extends PHPUnit_Framework_TestCase {
 private $myClass;

 public function setUp() {
   $this->myClass = new MyClass();
 }

 public function tearDown() {
   $this->myClass = null;
 }

 public function testSomething() {
   $this->assertTrue($this->myClass->someFunc(1));
 }

 public function testSomethingAgain() {
   $this->assertFalse($this->myClass->someFunc(0));
 }
}
Asaph
Thank you! needs15characterstocommentwtf
lemon