Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit -setUp() - does it run before and after each test case?

I am still a bit confused with setup() in PHPUnit.

Does it run before and after each test case?

For intance, I want to clean up my article table before each test but I want to keep the test data that I already injected into the table. Because I only want to clean it until the next test.

My test,

namespace Test\Foo\Article;

use Test\SuiteTest;
use Foo\Article;

class ArticleTest extends SuiteTest
{
    protected static $Article;

    /**
     * Call this template method before each test method is run.
     */
    protected function setUp()
    {
        $this->truncateTables(
            [
                'article'
            ]
        );

        self::$Article = new Article(self::$PDO);
    }

    public function testFetchRow()
    {
        self::$Article->createRow(
            [
                ':title' => 'Hello World',
                ':description' => 'Hello World',
                ':content' => 'Hello World'
            ]
        );

        $result = self::$Article->fetchRow(
            [
                ':article_id' => self::$PDO->fetchLastInsertId()
            ]
        );

        $this->assertArrayHasKey('article_id', $result);

        $expected = 12; // 12 keys associate with values in the array
        $this->assertEquals($expected, count($result));
    }
}

I check my article table, there is no test data anymore, it seems that setup() has cleaned it up. Is it how it should work?

What about the tearDown() - does it mean to run after the each test case?

like image 931
Run Avatar asked Oct 09 '15 09:10

Run


People also ask

What is PHPUnit used for?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.

Which method is used to create a mock with PHPUnit?

PHPUnit provides methods that are used to automatically create objects that will replace the original object in our test. createMock($type) and getMockBuilder($type) methods are used to create mock object. The createMock method immediately returns a mock object of the specified type.

What is assert in PHPUnit?

The assertSame() function is a builtin function in PHPUnit and is used to assert whether the actually obtained value is the same as the expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.


1 Answers

setUp() runs before every single test method, tearDown() runs after each test method.

PHPUnit Manual - Chapter 4 Fixures:

Before a test method is run, a template method called setUp() is invoked

...

Once the test method has finished running, whether it succeeded or failed, another template method called tearDown() is invoked

See https://phpunit.de/manual/current/en/fixtures.html

like image 175
StoryTeller Avatar answered Nov 04 '22 00:11

StoryTeller