Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 have to redeclare PHPUnit_Framework_TestCase construct and redirect any arguments. IF PHPUnit constructor is changed on a version update, my test case will stop.
  • Probably PHPUnit_Framework_TestCase was not declared to be used like this.

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

like image 787
marcio Avatar asked Dec 10 '13 23:12

marcio


1 Answers

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
    } 
like image 130
Cyprian Avatar answered Oct 02 '22 03:10

Cyprian