PHPUnit placeholder for empty tests

I like to have empty functions on occasion for placeholders (primarily empty constructors as it helps avoid accidental duplication of constructors, since my team knows there should always be somewhere).

I also like to have at least one test per class method (mainly because it's a simple rule to keep my team against).

My question is simple: why do we need to introduce these empty test methods to prevent the "no tests" warning.

We could just do $ this-> assertTrue (true), which I know will work fine. However, I was wondering if there is anything more formal and correct (preferably something that makes it such that the method does not count towards the number of tests run, artificially inflating it a bit).

Thank.

+3


source to share


2 answers


try this:



/**
 * @covers Controllers\AdminController::authenticate
 * @todo   Implement testAuthenticate().
 */
public function testAuthenticate()
{
    // Remove the following lines when you implement this test.
    $this->markTestIncomplete(
      'This test has not been implemented yet.'
    );
}

      

+4


source


You try the reflection class in the function and make sure the method exists. Then the test may simply be that the method exists (empty or not) that will pass without warning.



class MethodExistsTest extends \PHPUnit_Framework_TestCase
{
    protected $FOO;

    protected function setUp()
    {
        $this->FOO = new \FOO();
    }

    /**
     * @covers \FOO::Bar
     */
    public function testEmptyMethodBarExists()
    {
        $ReflectionObject = new \ReflectionObject($this->FOO);
        $this->assertTrue($ReflectionObject->getMethod('Bar'));
    }

    /**
     * @covers \FOO::__construct
     */
    public function testConstructorExists()
    {
        $ReflectionObject = new \ReflectionObject($this->FOO);
        $this->assertNotNull($ReflectionObject->getConstructor());
    }
}

      

0


source







All Articles