dingo-d / wp-pest

A package that will add WordPress integration test suite with Pest
MIT License
119 stars 5 forks source link

Subdirectory support under tests/ #20

Open paulcgdev opened 1 year ago

paulcgdev commented 1 year ago

Describe your feature request

It seems that PEST gets initialized in the tests directory only. It would be nice if we can get an option to initialize it in a subdirectory under tests like tests/pest. The reason is that there can be other test files in the tests dir like:

- my-plugin
    - tests
        - cypress
        - jest
        - ...

Describe the solution you'd like

Allow initialization in a subsdirectory at least under tests dir. Probably use --test-directory option of PEST on init or something along those lines.

Please confirm that you have searched existing issues and discussions about this feature.

Yes

dingo-d commented 1 year ago

Hmmm, so I tried setting up pest using --test-directory option, but it always initialized in the tests directory. I think it's just a matter of specifying which directory you want to check inside your phpunit.xml file.

In the template file you can set the directory for the test suite to any directory you want:

<testsuites>
    <testsuite name="Unit Test Suite">
        <directory>./tests/Unit/</directory>
    </testsuite>
    <testsuite name="Integration Test Suite">
        <directory>./tests/Integration/</directory>
    </testsuite>
</testsuites>
paulcgdev commented 1 year ago

Manually moving things around to the desired directory structure as mentioned, here's what worked for me:

  1. Change test namespace to Tests\Pest\Integration instead of Tests\Integration
  2. phpunit.xml file:
    <?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="tests/pest/bootstrap.php"
         colors="true"
    >
    <testsuites>
        <testsuite name="Unit Test Suite">
            <directory>./tests/pest/Unit/</directory>
        </testsuite>
        <testsuite name="Integration Test Suite">
            <directory>./tests/pest/Integration/</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="TEST" value="true" force="true"/>
        <server name="DB_NAME" value="wp_pest_test_db"/>
        <server name="DB_USER" value="root"/>
        <server name="DB_PASSWORD" value=""/>
        <server name="DB_HOST" value="localhost"/>
    </php>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./src</directory>
        </include>
        <report>
            <clover outputFile="tests/pest/coverage/clover.xml"/>
            <html outputDirectory="tests/pest/coverage/html" lowUpperBound="50" highLowerBound="90"/>
        </report>
    </coverage>
    </phpunit>
  3. Directory structure:

pestphp-dir-structure

  1. Change all dirname() 2nd param (level) to 3 as in dirname( __FILE__, 3 ) in bootstrap.php file
  2. Change following lines in Pest.php file (basically, just adding pest/ dir):
    uses()->group( 'integration' )->in( 'pest/Integration' );
    uses()->group( 'unit' )->in( 'pest/Unit' );
  3. Done!
dingo-d commented 1 year ago

I'll try to see if I can add another parameter or option to specify the folder so that this is automated 👍🏼