Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Latest commit

 

History

History
85 lines (61 loc) · 1.87 KB

unit-tests.rst

File metadata and controls

85 lines (61 loc) · 1.87 KB

Writing XUnit-Style Tests

XUnit-style tests are methods inside a class.

Convention over Configuration & Inheritance

Rather than forcing you to extend a TestCase base class demeanor favors a naming convention for test classes: they must end with the suffix Test.

<?php

// will be treated as a container for test cases
class SomeTest
{
    // ...
}

// demeanor will ignore this class
class Something
{
    // ...
}

Additionally all test methods must start with test and be public.

<?php

class SomeTest
{
    // will be turned into a test case
    public function testSomeObjectDoesStuff()
    {
        // ...
    }

    // not a test
    public function someObjectDoesOtherStuff()
    {

    }

    // also not a test
    private function testPrivateMethodsAreIgnored()
    {

    }
}

Using Counterpart Assertions

Starting with Counterpart 1.4, Counterpart\Assert and Counterpart\Matchers are traits. You can embed them in your test classes.

<?php

class SomeOtherTest
{
    use \Counterpart\Assert;
    use \Counterpart\Matchers;

    public function testSomething()
    {
        $this->assertTrue(true);
        // instead of Assert::assertTrue(true)

        $this->assertThat($this->arrayHasKey('one'), ['one' => true]);
        // instead of Assert::assertThat(Matchers::arrayHasKey('one'), ['one' => true]);
    }
}

Annotations

The :doc:`/annotations` documentation has a ton of information about using annotations to modify and change the behavior of unit tests.