2009-10-02 14 views
10
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)); 
    } 
} 

Ciao, devo davvero creare $ classe per ogni funzione di test che creo? O c'è una funzione simile al costruttore sconosciuta che devo ancora scoprire, poiché i costruttori non sembrano funzionare in PHPUnit.Come impostare o costruire un test dell'unità PHP

Grazie

risposta

25

È possibile utilizzare il setup() e tearDown() i metodi con una variabile privata o protetta. setUp() viene chiamato prima di ogni metodo testXxx() e tearDown() viene chiamato dopo. Questo ti dà una lavagna pulita con cui lavorare per ogni 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)); 
} 
}
+13

Grazie! needs15characterstocommentwtf – lemon

Problemi correlati