| Project: | Drupal core |
| Version: | 8.x-dev |
| Component: | documentation |
| Category: | task |
| Priority: | normal |
| Assigned: | jhodgdon |
| Status: | needs work |
Issue Summary
Problem/Motivation
The test case methods getInfo(), setUp(), and tearDown() are undocumented currently.
- Per coding standards, all methods are required to have documentation blocks.
- Per test writing guidelines, the methods should not be documented.
Proposed resolution
1. Add getInfo() to DrupalWebTestCase and DrupalUnitTestCase
2. Update existing test methods to use "Overrides" phpDoc blocks.
3. Update test writing guidelines to conform to coding standards.
How to generate this patch
To run the script (linux command line with Bash/Perl available is assumed):
- Download the run script from comment #16 (http://drupal.org/files/doall.sh_.txt), and the perl script from comment #19 (http://drupal.org/files/fixtests.pl__0.txt).
- Name them doall.sh and fixtests.pl
- Put them in the same directory (Drupal root, or core or core/modules).
- Make them executable.
- Run them from that directory.
API changes
Coding standards for tests conform to general coding standards now.
Original report by drewish
#276280: Tests needed: file.inc pointed out that Implementation of... was removed for setUp(), getInfo() or tearDown(). Here's a mega patch to remove them from all the tests.
| Attachment | Size | Status | Test result | Operations |
|---|---|---|---|---|
| tests_no_impl_docs.patch | 43.92 KB | Idle | Passed: 7440 passes, 0 fails, 0 exceptions | View details | Re-test |
Comments
#1
This looks great. The rationale for this is that we adopted our Test writers guidelines, and the consensus was that overridden methods do not have to be documented.
#2
Committed to CVS HEAD. Thanks.
#3
Automatically closed -- issue fixed for two weeks with no activity.
#4
I'm reopening this issue. It was never tagged "coding standards", and the standard that was adopted for the PHP docs for the tests is not consistent with the rest of our class-docs standards:
http://drupal.org/node/1354#classes
I would like to propose that we:
a) Add getInfo() to the DrupalWebTestCase and DrupalUnitTestCase base classes (it is not on any of the test case classes now that I can see).
b) Make the standard for docs of getInfo() and setUp() consistent with our class docs guidelines. Specifically, they should have an abbreviated documentation header saying:
/*** Overrides DrupalWebTestCase::setUp().
*/
function setUp() {
}
(similar for getInfo().
#5
Moving to documentation... But I'm not particular. It's just not AJAX :-)
#6
Sorry about that - must have clicked the wrong component. :)
#7
Not your fault. Whenever a component has been removed or renames, it defaults to AJAX when the issue is updated. I get one of these every couple of days :-)
#8
I think #4 sounds reasonable, and it dovetails nicely with the current sprint. It seems strange to have a standard where we require docblocks everywhere, except also explicitly require not having them in three small cases. That's confusing. I'd prefer the short-form docblocks we use for overridden methods generally. Less to remember. :)
Plus, having them improves code readability and makes it more accessible to novices, I think.
#9
Well, I see two votes for #4, and so far no one else has weighed in. We probably would need a patch for this... (a) is quick. (b) should be scriptable. Sounds like a fun project for this morning... :)
#10
I agree with both @xjm and @jhodgdon that we should be consistent with our documentation standards where ever possible. Other than the nuisance of writing
/*** Overrides DrupalWebTestCase::setUp().
*/
function setUp() {
}
at the top of each test class, I do not understand why this is not done. I would encourage this change.
Perhaps it would be even better if we created templates for best-of-breed UnitTest and WebTest class documentation that people could grab when they start to work on a new set of tests? Once this change is resolved, I can try to help with that.
#11
I would also like to edit http://drupal.org/node/325974 (Guidelines page) so that the recommendations there are in accordance with the rest of the docs guildelines on http://drupal.org/node/1354. Besides the class method problems:
- Class docblocks should start with a verb.
- The suggested @file description should have "the" in it ("Tests for the Aggregator module.")
- The sample test functions' docblock columns are mis-aligned, and it would be better if they started with a verb as a better model.
But I'll wait until this guideline is adopted. And another note: it should apply to tearDown() as well as getInfo() and setUp().
#12
Here's a patch that fixes up the docs for setup/teardown, and adds an overrideable function for getinfo to both Unit and Web test cases. I'm working on a script to add the "Overrides" lines to all existing core test classes too.
#13
Sending it to testbot. I think this could help make test writing less of a WTF for novices.
#14
The patch is not done. I'm working on the rest (updating the test files).
#15
Oops, sorry. :)
#16
OK. Here's a patch that includes #12 and also adds docblocks to all of the getInfo(), setUp(), and tearDown() methods.
I generated the docblock stuff with a Perl script and a bash Shell script (I know, PHP here but it's so much easier to do this stuff in Perl...). The script checks to see whether a class extends DrupalUnitTestCase, and if not, assumes it the methods are overridden from DrupalWebTestCase. So there could be a couple of cases where the method doc that is added is saying it's overriding a different method... probably not too many, hopefully?
Scripts attached too, in case anyone is interested. I added .txt to their extensions so they could be attached.
#17
Note: If you want to run the scripts:
- Name them doall.sh and fixtests.pl
- Put them in the same directory.
- Make them executable.
- Run them from that directory (Drupal root, or core or core/modules).
#18
OK, I can already see it didn't catch one DrupalUnitTestCase... will need to try again. What do people think about the concept?
#19
Fixed that problem - here's a new patch and a new script.
#20
Since none of these methods have docblocks currently, this patch should not collide with other patches. Sending to testbot to find out for sure. :)
#19: 338403-19-simpletest-cleanup-with-methods.patch queued for re-testing.
#21
The last submitted patch, 338403-19-simpletest-cleanup-with-methods.patch, failed testing.
#22
I'll wait on rebuilding this until it has a chance of being committed (critical count down etc.). Any comments on the idea or methodology? Someone else can also feel free to rerun the scripts. Instructions #17, one script #19, other script #16. I guess I'll go edit the issue summary now.
#23
I just edited the issue summary.
#24
Title which reflects the issue's current scope.
#25
.
#26
+++ b/core/modules/file/tests/file.test
@@ -1098,6 +1122,9 @@ class FileTokenReplaceTestCase extends FileFieldTestCase {
class FilePrivateTestCase extends FileFieldTestCase {
+ /**
+ * Overrides DrupalWebTestCase::getInfo().
+ */
public static function getInfo() {
@@ -1106,6 +1133,9 @@ class FilePrivateTestCase extends FileFieldTestCase {
+ /**
+ * Overrides DrupalWebTestCase::setUp().
+ */
function setUp() {
Well, I guess this would make sense if it would actually be correct. ;)
1) DrupalWebTestCase itself does not define getInfo(). As such, test cases can only implement getInfo(). (In turn, and given that a defined getInfo() method is actually required and tested against, and a test case without is straight-out ignored/invalid, DrupalWebTestCase should actually be declared as abstract.)
2) In this case, not DrupalWebTestCase::setUp() but FileFieldTestCase::setUp() is overridden, so the phpDoc here is actually invalid.
20 days to next Drupal core point release.
#27
+++ b/core/modules/simpletest/drupal_web_test_case.php
@@ -646,7 +646,27 @@ class DrupalUnitTestCase extends DrupalTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Sample functionality',
+ 'description' => 'Longer description of the tested functionality',
+ 'group' => 'Sample group',
+ );
+ }
@@ -1218,6 +1241,28 @@ class DrupalWebTestCase extends DrupalTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Sample functionality',
+ 'description' => 'Longer description of the tested functionality',
+ 'group' => 'Sample group',
+ );
+ }
Sorry, only after rewriting the summary I actually noticed that this patch attempts to add getInfo() methods to the base classes. That's wrong and won't fly.
20 days to next Drupal core point release.
#28
You should reach out to some of the original people who made the decision not to document these—http://groups.drupal.org/node/7731#comment-53456
#29
I'm confused; that seems to be advocating exactly the docblocks we're suggesting here?
Edit: Sorry, I needed to scroll down.
#30
I really don't have time/energy to fight this, but I'm strongly -1 to "documenting" all of this for all the reasons I wrote at that g.d.o post webchick linked to. These comments add no value, generate more visual noise, are wrong most of the time, and are a waste of everyone's time. Just like cut and paste coding is bad, cut and paste comments aren't any better. If we need to replicate an identical comment 100s (1000s?) of times in our codebase, something is wrong.
Furthermore, we don't do this kind of thing, either:
<?php// Add two numbers
$i = 1 + 2;
// Print the results
echo $i;
// Increment the value again.
$i++;
// See if the value is bigger than 6
if ($i > 6) {
// Do something.
something();
}
...
?>
We have to assume that people reading Drupal PHP code (at some level) understand PHP. If you don't understand how class inheritance works, these comments aren't going to help you. You need other resources, not our code comments, to gain that understanding.
Finally, most of these comments are technically wrong, since they're documenting methods that don't override the base class, but some other child of the base. So, instead of a script to puke the same comment everywhere, for this to be at all useful, we should document which parent's setUp() every child is actually overriding. But that's a mess, since if you add another class in the hierarchy, you just potentially invalidated a whole bunch of comments further down the tree and you have to go fix all of those. Trying (and inevitably failing) to keep all this in sync via manual effort of updating the comments is nuts. IDEs and other automated methods can tell you what method you're inheriting or overriding. We have vastly more important things to spend human effort on.
I'd move this back to "remove pointless comments on inherited methods" and set it to closed (fixed), but it's not up to me to make that decision unilaterally.
#31
I agree with the points @dww outlined. Based on that, we rather need to clarify the documentation standards on class inheritance, so that this case is unambiguously covered.
Regarding getInfo(), I've created #1339054: DrupalTestCaseInterface to properly require getInfo()
#32
I can confirm that figuring out what's overriding or implementing whom is time-consuming and frustrating at times. However, that's precisely why I feel that having an (accurate) docblock for these and other child class methods generally is important. Not everyone uses an IDE, and the short docblock is really, really helpful when reading unfamiliar code. It may take one person some time to add them, but how many dozens of other people then have saved that time? They make the code easier to understand, which makes Drupal more accessible. I also don't think they're anything near an
// Increment $i.;)For me, a docblock at each function or method also makes the code easier to scan, especially since that's what we have everywhere else.
#33
Well, we had a huge discussion a while back on documenting class methods that are overrides, and decided every function and method should have a docblock. We didn't have a discussion about documentation standards for tests (at least, a discussion that involved documentation folks previously)...
So I don't get it... Are sun/dww advocating for removing all inherited method docblocks, or just in tests? If just in tests, why are they different from other classes?
#34
AFAIK the previous standard was to remove it from all inherited methods, or at least that's the stick I applied with DBTNG, etc. I'm not sure where it was discussed changing that.
#35
#718596: Lacking standards for documenting classes, interfaces, methods seems to indicate the opposite, though. It requires the short-form docblocks for inherited methods.
#36
Yeah, that is what we decided on that other issue. I still don't understand why tests are an exception to the general standard?