Problem/Motivation

Examples currently doesn't demo how to set up test types other than Simpletest and unit tests.

Proposed resolution

Add an example module called testing_example, which will demo the different types of frameworks available from Drupal 8.

This won't include simpletest.

Find tutorials in https://www.drupal.org/docs/8/phpunit and support them.

Document how to run the various testing tools as part of the content of this module's description, or even on another page.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

rfay created an issue. See original summary.

rfay’s picture

Status: Active » Needs review
StatusFileSize
new12.58 KB

Here's the patch adding those two and updating the docs displayed on visiting the module page.

Status: Needs review » Needs work

The last submitted patch, 2: examples.add_kernel_and-2821403-2.patch, failed testing.

rfay’s picture

Status: Needs work » Needs review
StatusFileSize
new12.73 KB

Added @group annotations.

ifreeman’s picture

StatusFileSize
new13.84 KB

This patch shows an alternate way to create a node via a new test, which I think will be more appealing. Some minor code reformatting is also done.

rfay’s picture

Thanks @noobish - great thinking.

This patch just makes minor changes to what you did, and also ports the actual simpletest test, which I had forgotten about.

mile23’s picture

Issue summary: View changes
mile23’s picture

Thanks for this. We really need this expansion, and I'm OK with the changes.

+++ b/phpunit_example/tests/src/Functional/ExampleFunctionalTest.php
@@ -0,0 +1,134 @@
+ * vendor/phpunit/phpunit/phpunit -c core/phpunit.xml --printer '\Drupal\Tests\Listeners\HtmlOutputPrinter' modules/examples/phpunit_example/tests/src/Functional

There are so many caveats to add here, such as SIMPLETEST_DB and... Ugh. D8 testing is a bit of a complicated game which is unfortunate. But we should document as much as we can.

Also, you can do the same thing this way: phpunit -c core/ --printer '[stuff]' --testsuite functional --group phpunit_example

There are a bunch of coding standards problems here. Consult STANDARDS.md for how to check them: http://cgit.drupalcode.org/examples/tree/STANDARDS.md

Coding standards errors:

FILE: ...s/phpunit_example/tests/src/Functional/ExampleFunctionalTest.php
----------------------------------------------------------------------
FOUND 12 ERRORS AND 5 WARNINGS AFFECTING 17 LINES
----------------------------------------------------------------------
   5 | WARNING | [x] Unused use statement
     |         |     (Drupal.Classes.UnusedUseStatement.UnusedUse)
   9 | ERROR   | [x] Doc comment short description must end with a full
     |         |     stop (Drupal.Commenting.DocComment.ShortFullStop)
  11 | WARNING | [ ] Line exceeds 80 characters; contains 82
     |         |     characters (Drupal.Files.LineLength.TooLong)
  14 | WARNING | [ ] Line exceeds 80 characters; contains 161
     |         |     characters (Drupal.Files.LineLength.TooLong)
  19 | ERROR   | [x] Expected 1 space before opening brace; found 2
     |         |     (Drupal.Classes.ClassDeclaration.SpaceBeforeBrace)
  24 | ERROR   | [ ] If the line declaring an array spans longer than 80
     |         |     characters, each element should be broken into its own
     |         |     line (Drupal.Array.Array.LongLineDeclaration)
  31 | ERROR   | [x] Doc comment short description must start with a
     |         |     capital letter
     |         |     (Drupal.Commenting.DocComment.ShortNotCapital)
  42 | WARNING | [x] A comma should follow the last multiline array item.
     |         |     Found: 'administer content types'
     |         |     (Drupal.Array.Array.CommaLastItem)
  69 | ERROR   | [x] TRUE, FALSE and NULL must be uppercase; expected
     |         |     "NULL" but found "null"
     |         |     (Generic.PHP.UpperCaseConstant.Found)
  85 | ERROR   | [x] Inline comments must end in full-stops, exclamation
     |         |     marks, colons, question marks, or closing
     |         |     parentheses
     |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
  93 | ERROR   | [x] Doc comment short description must end with a full
     |         |     stop (Drupal.Commenting.DocComment.ShortFullStop)
 102 | ERROR   | [x] Inline comments must end in full-stops, exclamation
     |         |     marks, colons, question marks, or closing
     |         |     parentheses
     |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 104 | ERROR   | [x] Inline comments must end in full-stops, exclamation
     |         |     marks, colons, question marks, or closing
     |         |     parentheses
     |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 110 | WARNING | [x] A comma should follow the last multiline array item.
     |         |     Found: ] (Drupal.Array.Array.CommaLastItem)
 116 | ERROR   | [x] Inline comments must end in full-stops, exclamation
     |         |     marks, colons, question marks, or closing
     |         |     parentheses
     |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 128 | ERROR   | [x] Inline comments must end in full-stops, exclamation
     |         |     marks, colons, question marks, or closing
     |         |     parentheses
     |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 134 | ERROR   | [x] The closing brace for the class must have an empty
     |         |     line before it
     |         |     (Drupal.Classes.ClassDeclaration.CloseBraceAfterBody)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 14 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: ...amples/phpunit_example/tests/src/Kernel/SimpleKernelNodeTest.php
----------------------------------------------------------------------
FOUND 11 ERRORS AND 4 WARNINGS AFFECTING 14 LINES
----------------------------------------------------------------------
  6 | WARNING | [x] Unused use statement
    |         |     (Drupal.Classes.UnusedUseStatement.UnusedUse)
  9 | WARNING | [x] Unused use statement
    |         |     (Drupal.Classes.UnusedUseStatement.UnusedUse)
 12 | ERROR   | [x] Doc comment short description must end with a full
    |         |     stop (Drupal.Commenting.DocComment.ShortFullStop)
 19 | ERROR   | [x] Expected 1 space before opening brace; found 2
    |         |     (Drupal.Classes.ClassDeclaration.SpaceBeforeBrace)
 21 | ERROR   | [x] Inline comments must end in full-stops, exclamation
    |         |     marks, colons, question marks, or closing parentheses
    |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 29 | ERROR   | [x] The open comment tag must be the only content on the
    |         |     line (Drupal.Commenting.DocComment.ContentAfterOpen)
 33 | ERROR   | [x] Doc comment short description must start with a
    |         |     capital letter
    |         |     (Drupal.Commenting.DocComment.ShortNotCapital)
 44 | WARNING | [ ] Line exceeds 80 characters; contains 83
    |         |     characters (Drupal.Files.LineLength.TooLong)
 46 | WARNING | [x] There must be no blank line following an inline
    |         |     comment
    |         |     (Drupal.Commenting.InlineComment.SpacingAfter)
 48 | ERROR   | [x] Inline comments must start with a capital letter
    |         |     (Drupal.Commenting.InlineComment.NotCapital)
 48 | ERROR   | [x] Inline comments must end in full-stops, exclamation
    |         |     marks, colons, question marks, or closing parentheses
    |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 51 | ERROR   | [x] Inline comments must end in full-stops, exclamation
    |         |     marks, colons, question marks, or closing parentheses
    |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 58 | ERROR   | [x] Functions must not contain multiple empty lines in a
    |         |     row; found 2 empty lines
    |         |     (Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines)
 81 | ERROR   | [x] Functions must not contain multiple empty lines in a
    |         |     row; found 2 empty lines
    |         |     (Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines)
 87 | ERROR   | [x] The closing brace for the class must have an empty
    |         |     line before it
    |         |     (Drupal.Classes.ClassDeclaration.CloseBraceAfterBody)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 14 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: ...amples/phpunit_example/tests/src/Kernel/SimpleKernelUserTest.php
----------------------------------------------------------------------
FOUND 9 ERRORS AND 3 WARNINGS AFFECTING 11 LINES
----------------------------------------------------------------------
  7 | WARNING | [x] Unused use statement
    |         |     (Drupal.Classes.UnusedUseStatement.UnusedUse)
 10 | ERROR   | [x] Doc comment short description must end with a full
    |         |     stop (Drupal.Commenting.DocComment.ShortFullStop)
 24 | ERROR   | [x] Expected 1 space before opening brace; found 2
    |         |     (Drupal.Classes.ClassDeclaration.SpaceBeforeBrace)
 26 | ERROR   | [x] Inline comments must end in full-stops, exclamation
    |         |     marks, colons, question marks, or closing parentheses
    |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 33 | ERROR   | [x] The open comment tag must be the only content on the
    |         |     line (Drupal.Commenting.DocComment.ContentAfterOpen)
 37 | ERROR   | [x] Doc comment short description must start with a
    |         |     capital letter
    |         |     (Drupal.Commenting.DocComment.ShortNotCapital)
 48 | WARNING | [ ] Line exceeds 80 characters; contains 83
    |         |     characters (Drupal.Files.LineLength.TooLong)
 50 | WARNING | [x] There must be no blank line following an inline
    |         |     comment
    |         |     (Drupal.Commenting.InlineComment.SpacingAfter)
 52 | ERROR   | [x] Inline comments must start with a capital letter
    |         |     (Drupal.Commenting.InlineComment.NotCapital)
 52 | ERROR   | [x] Inline comments must end in full-stops, exclamation
    |         |     marks, colons, question marks, or closing parentheses
    |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 55 | ERROR   | [x] Inline comments must end in full-stops, exclamation
    |         |     marks, colons, question marks, or closing parentheses
    |         |     (Drupal.Commenting.InlineComment.InvalidEndChar)
 79 | ERROR   | [x] The closing brace for the class must have an empty
    |         |     line before it
    |         |     (Drupal.Classes.ClassDeclaration.CloseBraceAfterBody)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 11 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
mile23’s picture

Status: Needs review » Needs work
mile23’s picture

Title: Add kernel and functional tests to phpunit_example » Add testing_example with kernel, functional, functional-js tests
Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new17.45 KB

Let's switch it up here.

Let's make an example module called testing_example. This will be clearer to anyone who doesn't understand the distinction between simpletest and phpunit.

We'll leave simpletest_example and phpunit_example for now. We might fold them in later, but probably not.

For now, phpunit_example can demonstrate generic phpunit techniques such as mocking and data providers.

testing_example will show you how to do more Drupal-y stuff involving having a fixture site (or fixture kernel) to manipulate.

simpletest_example will stay mostly the same and probably won't see much maintenance. We can just remove it for D9.

Here's a patch that moves all the additions here to testing_example, with some updates and edits.

mile23’s picture

Issue summary: View changes
mile23’s picture

StatusFileSize
new17.23 KB
new15.98 KB

Fixed a lot of CS errors.

Renamed tests with Simple in the name to Example. We're trying not to confuse people who are used to simpletest. :-) We could probably just rename them NodeTest and UserTest.

Updated some docblocks and the module's real functional test.

I don't think we need ExampleKernelUserTest (formerly SimpleKernelUserTest) because it's almost identical to the node test, and it doesn't test anything other than that the test framework is functioning properly.

Status: Needs review » Needs work

The last submitted patch, 13: 2821403_12.patch, failed testing. View results

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new16.46 KB
new10.04 KB

Looking at the last test, it had a testbot failure after all the tests passed. https://www.drupal.org/pift-ci-job/732363

Consolidating the kernel tests into ExampleFixtureManagementTest, since they demonstrate how to set up fixture content and users.

Some tweaks and comment updates to ExampleFunctionalTest.

  • Mile23 committed 1e75e9d on 8.x-1.x authored by rfay
    Issue #2821403 by Mile23, rfay, ifreeman: Add testing_example with...
mile23’s picture

Component: PHPUnit Example » Testing Example
Status: Needs review » Fixed

Committed and pushed. Thanks folks!

Anyone wishing to add a feature request, please file a new issue against the Testing Example component. Thanks!

Status: Fixed » Closed (fixed)

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