marcio
marcio

Reputation: 10532

PHPUnit setup and tearDown for test cases

PHPUnit has setup and tearDown events that run, respectively, before and after each test within a test case. In my specific scenario, I also want to run something like a testCaseSetup and testCaseTearDown. Is that possible?

Current solution looks like this:

<?php

class MyTestCase extends \PHPUnit_Framework_TestCase
{

    public function __construct($name = NULL, array $data = array(), $dataName = '')
    {
        // My test case setup logic
        parent::__construct($name, $data, $dataName);
    }

    public function __destruct()
    {
        // My test case tear down logic
    }
}

But it seems far from optimal for the following reasons:

I would like to know if there are better solutions. Any ideas?

Upvotes: 5

Views: 5779

Answers (1)

Cyprian
Cyprian

Reputation: 11374

Yes, there are special methods for that purpose: setUpBeforeClass and tearDownAfterClass.

class TemplateMethodsTest extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        // do sth before the first test
    } 

    public static function tearDownAfterClass()
    {
        // do sth after the last test
    } 

Upvotes: 10

Related Questions