Unit tests are very important, and can be a way to test your code without ever instantiating Drupal. phpunit_example module should give people a good start towards using tests to write high-quality code.

Important stuff to cover:

  • PSR-0 for contrib test classes (Put them under /module/[maybe more directories here]/module_name/tests/Drupal/module_name/Tests)
  • Subclass Drupal\Tests\UnitTestCase.
  • PHPUnit annotations such as @expectedException and @dataProvider
  • How to run unit tests from the command line and in Drupal
  • Coding standards: #2057905: [policy, no patch] Discuss the standards for phpunit based tests
  • And more. :-)

This issue is immediately postponed, because currently Drupal 8's phpunit bootstrap doesn't autoload for contrib modules. See #2025883: Drupal's PHPUnit bootstrap.php does not register module namespaces out of /core and #2056607: /core/phpunit.xml.dist only finds top-level contrib modules

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mile23’s picture

Component: Simpletest Example » PHPUnit Example

Added PHPUnit Example to the list of categories.

Mile23’s picture

Status: Postponed » Needs review
Issue tags: +PHPUnit
FileSize
12.68 KB

The beginning of phpunit_example.

It will likely pass because the testbot won't run it's PHPUnit tests, and it doesn't have any SimpleTest tests.

To get PHPUnit to run the tests in this module, you'll need to apply the patches in the linked issues above.

Still to do:

  • Fix some doc standards problems.
  • Demonstrate how to mock classes and how to access private/protected methods with reflections.
Mile23’s picture

Issue summary: View changes

Added another @see issue link.

Mile23’s picture

Added reflection and stubbing demo.

Still needs: Mocking demo, doc cleanup (@ingroup, etc)

Status: Needs review » Needs work

The last submitted patch, 2032697-phpunit_example-3.patch, failed testing.

Wim Leers’s picture

Nice work! :)

Mile23’s picture

Status: Needs work » Needs review

Nice work except for the no tests part.

Mile23’s picture

Status: Needs review » Fixed

Clearly my spare time has run dry, so I'll commit this as-is.

Further features can have their own issues.

http://drupalcode.org/project/examples.git/commit/cadcdc9e8e20e6ff20f198...

marvil07’s picture

It seems like this additions has broken tests.

I'm not sure now about the real problem now.

I would like to revert this in order to test other to testbot to test other patches in the issue. Is that ok?
We can always re-include it ;-) (also it seems to be needing some changes, i.e. page_example strings).

Mile23’s picture

Status: Fixed » Postponed

Aha.

I should have left this as postponed.... The problem is that the PHPUnit configuration is smart enough to load the test, but the autoloader is not smart enough to load the test class. #2025883: Drupal's PHPUnit bootstrap.php does not register module namespaces out of /core

Yah, I'll yank it. Sob. http://drupalcode.org/project/examples.git/commit/784ccc2be317b27ca4eb8f...

Mile23’s picture

I made a github repo here, since I'll be presenting this in two weeks: https://github.com/paul-m/phpunit_example

marvil07’s picture

Status: Postponed » Needs review

#3: 2032697-phpunit_example-3.patch queued for re-testing.

marvil07’s picture

@testbot: Re-test last patch now that dependent patch in core has landed.

Status: Needs review » Needs work

The last submitted patch, 2032697-phpunit_example-3.patch, failed testing.

Mile23’s picture

A few doc tweaks...

Mile23’s picture

Status: Needs work » Needs review

The last submitted patch, 2032697_phpunit_example_14.patch, failed testing.

Xano’s picture

Status: Needs work » Needs review

#14: 2032697_phpunit_example_14.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +PHPUnit

The last submitted patch, 2032697_phpunit_example_14.patch, failed testing.

eliza411’s picture

FYI, I created a handbook page explaining where to put tests, and how the directory structure relates to namespace and class name: https://drupal.org/node/2116043 (I've had to explain this to a lot of developers who've never heard of PSR-0 in the context of Behat and the Drupal Extension)

There's definitely more to do and I'm not sure about the relationship between handbook pages and the Examples module, so I'm open to feedback for sure.

Mile23’s picture

Diggit. :-)

I think the more places the better, especially in this context.

There's also big caveats because of #1971198: [policy] Drupal and PSR-0/PSR-4 Class Loading.

Mile23’s picture

Status: Needs work » Needs review
Issue tags: -PHPUnit

#14: 2032697_phpunit_example_14.patch queued for re-testing.

Mile23’s picture

Issue summary: View changes

Added reference to coding standards discussion.

Mile23’s picture

Issue summary: View changes
FileSize
27.39 KB

Updated some documentation.

Status: Needs review » Needs work

The last submitted patch, 14: 2032697_phpunit_example_14.patch, failed testing.

Mile23’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 22: 2032697_phpunit_example_22.patch, failed testing.

Mile23’s picture

Status: Needs work » Needs review
FileSize
27.46 KB

Try, try again.

I had failing tests locally because I needed to delete /sites/default/files/php. Let's see if the testbot is similarly hobbled.

Status: Needs review » Needs work

The last submitted patch, 26: 2032697_26_phpunit_example.patch, failed testing.

Mile23’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 26: 2032697_26_phpunit_example.patch, failed testing.

Mile23’s picture

Status: Needs work » Needs review
Mile23’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.