Problem/Motivation

While I was trying to figure out #1791970: Optionally detect tar.* as extension instead of * on views file extension field handler I noticed the procedure to make views unit test is a little tricky, and some maintainers agreed with me about that.

So, what I think we should be documented:

  • It is recommmended to use a custom module to test views handlers.
  • Those modules should live on module's tests/modules directory.
  • It is recommmended to define new views instead of reusing the ones provided by views and change them at runtime, unless there is a good reason behind that.
  • To have the defined view loaded you need to:
    • add the view machine name on the $testViews data member declaration
    • store the exported view on test module's test_views directory with file name views.view.<machine_name>.yml
    • Implement setUp and call ViewTestData::importTestViews() passing the module containing the default test view.
  • It is recommended define the test calss on Drupal\file\Tests\Views namespace.
  • Mention how to use dataSet() and viewsData().
  • Maybe an basic example (an extension of Drupal\views\Tests\BasicTest?).

Proposed resolution

Let's document it!

Remaining tasks

  • Decide where to document it. Current propposals:
    • at Drupal\views\Tests\ViewUnitTestBase
    • at Drupal\views\Tests\BasicTest

User interface changes

None.

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Um. Probably the best place to document these types of things would actually be somewhere inside
http://drupal.org/simpletest

Then you could point to that documentation from the base class(es) for Views tests.

I don't think it's a good idea to put a bunch of tutorial documentation on how to write tests into the API documentation.

marvil07’s picture

Status: Active » Needs review
FileSize
1.82 KB

Just in case it makes sense to have it on code, here a start patch.

IMHO it's better to have it in code, since I would say most people writing views tests will be looking at code documentations instead of handbook pages.

dawehner’s picture

Great stuff, thank you!

What would be also very helpful: Provide a @see to an easy example.

marvil07’s picture

@dawehner: yes, that makes sense, I would say #1791970: Optionally detect tar.* as extension instead of * on views file extension field handler is simple enough to serve as example, but if there is another one already in core, I'll be happy to add it.

dawehner’s picture

That's a good idea.

jhodgdon’s picture

Status: Needs review » Needs work

That documentation does indeed look like a very useful addition to the API docs! +1. Is that really all you need to do for Views to detect your test view and make it available? If so, cool, and definitely a good idea to document it this way.

One typo I noticed:
+ * For new views unit tests tests defined on a external module:
The word "tests" is repeated. Also, what's an "external" module? Do you just mean "a module that isn't views itself"? If so, I think I would say "To make a Views unit test outside the Views module:" or something like that.

marvil07’s picture

Status: Needs work » Needs review
FileSize
1.27 KB
1.44 KB

@jhodgdon: Yes, that's all needed steps. Thanks for the correction, that's what I tried to said.

Here a new patch with that and the new @see.

jhodgdon’s picture

That looks pretty good to me! The only question I had was whether, technically, setUp() would be implementing something (from an interface) or overriding something (from a base class)? Other than that, assuming someone from the Views team can verify that the documentation is accurate, I think it's ready to go.

And maybe instead of saying "Define the test module view" you might say "Define any views needed in your tests"?

damiankloip’s picture

Looking pretty good!

+++ b/core/modules/views/lib/Drupal/views/Tests/BasicTest.phpundefined
@@ -9,6 +9,17 @@
+ *   tests/modules/<module_name>/test_views/views.view.<machine_name>.yml.

This should maybe also contain the prefix. You never know what people will assume in some cases.

+++ b/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.phpundefined
@@ -21,6 +21,7 @@
+ * @see \Drupal\views\Tests\BasicTest

Nice. BasicTest is a good example implementation.

jhodgdon’s picture

RE #9 - what prefix? Maybe the documentation should say something like "Choose a machine name for your view that is prefixed by your module's machine name"?

damiankloip’s picture

I mean that it should state that the tests directory lives in a modules directory. With a name like tests and also the module namespace, this might not be obvious to your average developer.

jhodgdon’s picture

Um... The documentation already starts out by saying "if you want to create a test for Views that is not inside the Views module", which implies it is in some other module? And I don't think we need to document how to do PSR-0 here... Maybe I'm missing something that you're trying to get across though?

jhodgdon’s picture

Status: Needs review » Needs work

I took a look at this again today... I think this spot could use a comma or the word "in" ("passing in") or something -- it's a bit hard to read:

+ * - Implement setUp() and call ViewTestData::importTestViews() passing the
+ *   module containing the default test view.

I am still not sure that we need any clarification to address #9, since we don't want to document how to make tests in general or how to do PSR-0 here. Thoughts?

jhodgdon’s picture

Issue summary: View changes

fix html

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Closed (won't fix)

There are now many views tests in core outside of the views module itself. These provide plenty of examples to use when writing a test. I am not aware of other documentation in core that explains how to write tests, it certainly was not added for the migration system.

As far as I know it is established practice to look at existing tests as examples when writing new tests. That has the advantage of being up to date, whereas the documentation can fall behind. Therefor, I am setting this as won't fix. If you disagree, re-open and explain.

Thanks