Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#26 | 2032697_26_phpunit_example.patch | 27.46 KB | Mile23 |
#22 | 2032697_phpunit_example_22.patch | 27.39 KB | Mile23 |
#14 | 2032697_phpunit_example_14.patch | 26.91 KB | Mile23 |
#3 | 2032697-phpunit_example-3.patch | 19.98 KB | Mile23 |
#2 | 2032697-phpunit_example.patch | 12.68 KB | Mile23 |
Comments
Comment #1
Mile23Added PHPUnit Example to the list of categories.
Comment #2
Mile23The 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:
Comment #2.0
Mile23Added another @see issue link.
Comment #3
Mile23Added reflection and stubbing demo.
Still needs: Mocking demo, doc cleanup (@ingroup, etc)
Comment #5
Wim LeersNice work! :)
Comment #6
Mile23Nice work except for the no tests part.
Comment #7
Mile23Clearly 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...
Comment #8
marvil07 CreditAttribution: marvil07 commentedIt 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).
Comment #9
Mile23Aha.
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...
Comment #10
Mile23I made a github repo here, since I'll be presenting this in two weeks: https://github.com/paul-m/phpunit_example
Comment #11
marvil07 CreditAttribution: marvil07 commented#3: 2032697-phpunit_example-3.patch queued for re-testing.
Comment #12
marvil07 CreditAttribution: marvil07 commented@testbot: Re-test last patch now that dependent patch in core has landed.
Comment #14
Mile23A few doc tweaks...
Comment #15
Mile23Comment #17
Xano#14: 2032697_phpunit_example_14.patch queued for re-testing.
Comment #19
eliza411 CreditAttribution: eliza411 commentedFYI, 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.
Comment #20
Mile23Diggit. :-)
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.
Comment #21
Mile23#14: 2032697_phpunit_example_14.patch queued for re-testing.
Comment #21.0
Mile23Added reference to coding standards discussion.
Comment #22
Mile23Updated some documentation.
Comment #24
Mile2322: 2032697_phpunit_example_22.patch queued for re-testing.
Comment #26
Mile23Try, 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.
Comment #28
Mile2326: 2032697_26_phpunit_example.patch queued for re-testing.
Comment #30
Mile2326: 2032697_26_phpunit_example.patch queued for re-testing.
Comment #31
Mile23Finally. :-)
Committed: http://drupalcode.org/project/examples.git/commit/17160ed3140b3ed8226134...