Sunday, 29 September 2019

phpunit run class without "test" in the name

How to indicate to phpunit that the class is a test class?

The /** @test */ annotation seems to work only for methods that don't have test string appended in their name

So how to run a test class without appending the test string to its name?

Here's the class

<?php

namespace Tests\Feature;

use Tests\TestCase;
/** @test */
class RepoPost extends TestCase
{
    /** @test */
    public function postSave()
    {
        $this->assertTrue(true);
    }

    /** @test */
    public function anotherOne()
    {
        $this->assertTrue(true);
    }
}

Running

vendor/bin/phpunit --filter RepoPost

Outputs

No tests executed!

Update

Here's my phpunit.xml config

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="tests/bootstrap.php"
         colors="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Feature">
            <directory>./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <php>
        <server name="APP_ENV" value="testing"/>
    </php>
</phpunit>

While it's possible to run the class via full path as @Alister pointed out like this

vendor/bin/phpunit tests/Feature/RepoPost.php

It isn't convenient to do this over and over for every class, especially as part of the CI process
Ideally the class would run within the full test suite by

vendor/bin/phpunit


from phpunit run class without "test" in the name

No comments:

Post a Comment