Skip to content

Commit

Permalink
Merge pull request #63 from humanmade/backport-60-to-v3-branch
Browse files Browse the repository at this point in the history
[Backport v3-branch] Add altis.loaded_phpunit hook and docs
  • Loading branch information
roborourke authored Feb 13, 2020
2 parents b5615f1 + 3378f80 commit 378b23a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
30 changes: 30 additions & 0 deletions docs/testing-with-phpunit.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,36 @@ tests_add_filter( 'altis.loaded_autoloader', function () {
}, 0 );
```

### Extending `WP_UnitTestCase` Classes

In some cases you may wish to avoid repetitive code or add common helper methods to the standard `WP_UnitTestCase` class. Because the `WP_UnitTestCase` classes are loaded after the main bootstrap process you need to use the `altis.loaded_phpunit` action hook to ensure they're available.

In your `.config/tests-bootstrap.php` you would add:

```php
tests_add_filter( 'altis.loaded_phpunit', function () {
// Load custom test case classes here.
require_once dirname( __DIR__ ) . '/tests/class-custom-unit-test-case.php';
} );
```

In `tests/class-custom-unit-test-case.php` you could then add something like the following:

```php
<?php
class Custom_UnitTestCase extends WP_UnitTestCase {

public function wpSetUpBeforeClass( $factory ) {
// Common set up routine.
}

public function wpTearDownAfterClass( $factory ) {
// Common tear down routine.
}

}
```

## Using A Custom Configuration File

In order to run PHPUnit with your own XML config file you can pass the `--configuration` option like so:
Expand Down
6 changes: 6 additions & 0 deletions inc/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@

// Start up the WP testing environment.
require getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php';

/**
* Action runs after WP PHPUnit is fully loaded. Use this to load any
* custom test case classes that extend WP_UnitTestCase.
*/
do_action( 'altis.loaded_phpunit' );

0 comments on commit 378b23a

Please sign in to comment.