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

What is a Unit Test? #96

Closed
ghost opened this issue May 26, 2017 · 2 comments
Closed

What is a Unit Test? #96

ghost opened this issue May 26, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented May 26, 2017

WP_Mock is an API mocking framework, built and maintained by 10up for the purpose of making it possible to properly unit test within WordPress.

Sorry to bother you. But I've been doing some research while building out unit tests for a WordPress plugin I'm building and discovered a misnomer in the vernacular being used in the community with regard to what actually constitutes unit testing.

My understand is that, in using WP_Mock or similar, while creating WordPress plugins what was is doing is actually creating an "integration test" and not a "unit test".

To help justify my rationale I've set-up an application capable of both unit tests and integration testing. Here's what an example integration test looks like:

/**
 * Sample test case.
 */
class SampleTest extends WP_UnitTestCase {

	/**
	 * A single example test.
	 */
	function test_sample() {
		// Replace this with some actual testing code.
		$this->assertTrue( true );
	}
}

It's an integration test because it requires an understanding of and dependence on WordPress internals in order to execute.

Whereas here's what an actual unit test looks like:

namespace hyperdrive\spec;
use function hyperdrive\enter_hyperspace;

describe('hyperdrive', function () {
  describe('enter_hyperspace()', function () {
    it('echos empty script given empty string', function () {
      $closure = function () { enter_hyperspace(''); };
      expect($closure)->toEcho('<script></script>');
    });

    it('echos string in script given string', function () {
      $closure = function () { enter_hyperspace('Danger, Will Robinson!'); };
      expect($closure)->toEcho('<script>Danger, Will Robinson!</script>');
    });

    it('does not echo unexpected string', function () {
      $closure = function () { enter_hyperspace('Danger, Will Robinson!'); };
      expect($closure)->not->toEcho('<script>Warning! Warning!</script>');
    });
  });
});

Above snippet copied from hyperdrive.spec.php and licensed for reuse under AGPL.

Notice there is no reliance on WordPress in order to operate; code is operating on PHP source capable of being run in any system and independent WordPress internals.

Which begs the question, what is actually considered a unit test when working with WordPress, and should the community consider adjusting vernacular to help prevent misunderstanding and starting with adjusting the blockquote pulled from the WP_Mock README file copied above?

@johnpbloch
Copy link
Contributor

johnpbloch commented May 26, 2017 via email

@ghost
Copy link
Author

ghost commented May 26, 2017

Ah! I see where my logic failed me now. I saw the class-based approach and thought PHPUnit_Framework_TestCase was extending WP_UnitTestCase, which is not the case, just a similar OOD approach to unit testing looks like, and is not extending WordPress in any way from what I can see. Thanks for the info.

@ghost ghost closed this as completed May 26, 2017
This issue was closed.
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

1 participant