navidbakhtiary's avatar

how to test specific file in package

Hello. I have a package that has a tests directory. The tests feature directory includes LabelTest.php and TaskLabelTest.php. Both LabelTest and TaskLabelTest run when vendor\bin\phpunit --filter LabelTest is run. Because of the "LabelTest" pattern in their names. how to run just one of them by writing its specific name? I tried different solutions but I did not succeed. Please show me a way. thanks for your help

phpunit.xml in main directory of laravel project

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>

        <testsuite name="NavidBakhtiary\ToDo\Tests\Feature">
            <directory suffix="Test.php">./packages/navidbakhtiary/todo/tests/Feature</directory>
        </testsuite>

        <testsuite name="NavidBakhtiary\ToDo\Tests\Unit">
            <directory suffix="Test.php">./packages/navidbakhtiary/todo/tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
</phpunit>

structure of package directories

laravel project
		|---app
		|---packages
		|		|---navidbakhtiary
		|				|---todo
		|						|---src
		|						|---tests
		|								|---Feature
		|										|---LabelTest.php
		|										|---TaskLabelTest.php
		|								|---Unit
		|---phpunit.xml
0 likes
6 replies
splatEric's avatar
Level 2

vendor/bin/phpunit ./packages/navidbakhtiary/todo/tests/Feature/LabelTest.php will do it

or you could pass in the full namespace qualified class name to the --filter option

1 like
navidbakhtiary's avatar

Thank you @splatEric. vendor/bin/phpunit ./packages/navidbakhtiary/todo/tests/Feature/LabelTest.php works fine. but use vendor\bin\phpunit --filter NavidBakhtiary/ToDo/Tests/Feature/LabelTest.php or vendor\bin\phpunit --filter NavidBakhtiary/ToDo/Tests/Feature/LabelTest or replacing slashes with backslashes says No tests executed!. can you write a example command?

splatEric's avatar

Backslashes for namespacing … you may need double to escape it, I can’t recall off the top of my head.

1 like
navidbakhtiary's avatar

i found it with your guidance. vendor\bin\phpunit --filter "NavidBakhtiary\\ToDo\\Tests\\Feature\\LabelTest" works fine. thank you @splateric

Please or to participate in this conversation.