Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subdirectory support under tests/ #20

Open
paulcgdev opened this issue Sep 15, 2022 · 3 comments
Open

Subdirectory support under tests/ #20

paulcgdev opened this issue Sep 15, 2022 · 3 comments
Assignees

Comments

@paulcgdev
Copy link

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
Copy link
Owner

dingo-d commented Sep 16, 2022

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
Copy link
Author

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>
  1. 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
Copy link
Owner

dingo-d commented Sep 17, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants