Wanted to post this as a reminder to myself.

Simpletest currently only considers classes in .test files as possible test classes.

It should be possible to backport the 8.x-style PSR-0 test class lookup (everything in Drupal\module\Tests) and add them in hook_simpletest_alter().

Will try to implement it myself, I'm also happy to answer questions (I wrote the 8.x code, with input from others)

Comments

donquixote’s picture

So this should be part of X Autoload? Cool! I'm curious for your patch.
(xautoload has its own tests in .test, as far as i remember. but this is going to stay this way, because tests should still work if xautoload does not)

berdir’s picture

StatusFileSize
new2.18 KB

Well, I tried ;)

The attached patch does expose test classes to Simpletest, you can select and start them through the UI.

However, running them doesn't work, not exactly sure why. There are also a bunch of other problems, notably that we'd have to register the autoloader for disabled modules (you can run tests of disabled modules, important for testbot). And speaking of testbot, that one would of course have to have xautoload enabled on the test environment.

So, this is probably not going to fly any time soon, but here's the patch in case someone wants to play with it.

donquixote’s picture

However, running them doesn't work, not exactly sure why.

Do the classes fail to load?

There are also a bunch of other problems, notably that we'd have to register the autoloader for disabled modules (you can run tests of disabled modules, important for testbot).

Technically this should be possible, e.g. with a custom plugin/handler. Of course this is more work.

berdir’s picture

I just had a messed up installation (pro tip: simpletest and redis cache don't like each other), I guess it should actually work.

Will do another try later...

donquixote’s picture

Title: Expose test classes to Simpletest using hook_simplenews_alter() » Expose test classes to Simpletest using hook_simpletest_alter()

It is hook_simpletest_alter(), not simplenews

donquixote’s picture

Status: Active » Needs review
StatusFileSize
new3.01 KB

#2 works for me.

New patch with an example unit test case.

donquixote’s picture

Status: Needs review » Fixed

This is good enough to commit to the -dev version.
(esp, seems to be regression-free)
http://drupalcode.org/project/xautoload.git/commitdiff/ca4b5fb309faccbed...
http://drupalcode.org/project/xautoload.git/commitdiff/a4840161a1d800ece...

Further improvements (code style, documentation, bugfixes) can be done as follow-up.
(by reopening this issue)

Thanks Berdir for proposing this!

donquixote’s picture

Status: Fixed » Needs work

we'd have to register the autoloader for disabled modules (you can run tests of disabled modules, important for testbot).

That's the next thing we need to do.
I am confident we can fix this!

donquixote’s picture

Fixed!

Step 1:
Let simpletest register a handler to load test classes of disabled modules.
http://drupalcode.org/project/xautoload.git/commitdiff/56c39148bfe7653e7...

Step 2:
Add an example module to demonstrate the loading of test classes in disabled modules.
http://drupalcode.org/project/xautoload.git/commit/ed83d29667e9058c915b4...

donquixote’s picture

Status: Needs work » Fixed

@Berdir,
any other problems?

There are also a bunch of other problems, notably that we'd have to register the autoloader for disabled modules (you can run tests of disabled modules, important for testbot). And speaking of testbot, that one would of course have to have xautoload enabled on the test environment.

donquixote’s picture

Status: Fixed » Needs work

Next step:
From #1693398-14: Allow PSR-0 test classes to be used in D7 (followups)

We register the full namespace \Drupal\(module), instead of just \Drupal\(module)\Tests. Is this intended?
(xautoload currently only works for \Drupal\(module)\Tests on disabled modules, so I probably need to change that.)

I guess, the preferred logic would be
- register \Drupal\(module) for enabled modules.
- register \Drupal\(module) for disabled modules that have a lib/Drupal/(module)/Tests folder.
- register nothing for other disabled modules.

berdir’s picture

Yay for pushing this forward, would be nice if we could support PSR-0 tests on the testbot.

It's still being discussed in core what exactly should be registered, currently all but that is causing problems with plugins curently. But we should probably follow what core does.

Once this is working fine, we can talk with rfay/jthorson if they're interested to deploy this module on the testbot. I think they have the possibility to do something like this.

donquixote’s picture

Just found in D8 there is
core/modules/system/lib/Drupal/system/Tests/Menu/TrailTest.php

This means that yes, we should also scan subfolders of ../Tests/ !
So far I thought that's not supposed to happen.
Easy change, I guess.

xano’s picture

Polite bump. How close is the 7.x-2.x to full support?

donquixote’s picture

xautoload simpletest support is working ok for local test runs.
Known issues:

  • It does not find tests in subfolders. (see #13) Patches welcome.
  • Drupal's commandline scripts/run-tests.sh does not support backslashes in the name.
    You could patch D7 core to fix this: #1693398-20: Allow PSR-0 test classes to be used in D7 (followups)
    But if you are sane, you already use drush, and don't need those shell scripts.

The problem: Testbot does not enable xautoload for finding those tests, and thus all the hook_simpletest_alter() does not help for automated module testing on drupal.org.
See #1693398: Allow PSR-0 test classes to be used in D7 (followups). I tried to bring xautoload into the discussion, but people were not convinced.

I personally think we should patch the testbot rather than Drupal core. So the testbot should install/enable xautoload (or any contrib module) to find the PSR-0 tests.
Unfortunately, I have no idea how the testbot works, and have no time to study it.
http://drupalcode.org/project/project_issue_file_review.git/tree/refs/he...
This thing looks big. I would not know where to start.

donquixote’s picture

Btw, to circumvent all of this testbot crazyness, we could do the following:

A drush command to create a file modulename.generated.test
This file would contain stuff like

class Drupal_modulename_Tests_MyTest extends Drupal\modulename\Tests\MyTest {}
class Drupal_modulename_Tests_AnotherTest extends Drupal\modulename\Tests\AnotherTest {}
...

So you could work on the tests in PSR-0, and Drupal testbot would still detect them in the traditional way.

xano’s picture

The problem: Testbot does not enable xautoload for finding those tests, and thus all the hook_simpletest_alter() does not help for automated module testing on drupal.org.

If a module Foo specifies X Autoload as one of its dependencies, then installing Foo during a test should also automatically install X Autoload. This doesn't work this way for disabled modules.

Also, pifr_simpletest.client.inc contains pifr_client_review_pifr_simpletest::findTestFiles(), which can scan for D7 and D8 style test classes. I haven't looked at it into great detail, but it looks like this works regardless of the core version a module is compatible with.

donquixote’s picture

#17
Interesting, thanks for exploring!
Can you play with this and confirm that it gets as far as to find those tests?

Btw, I am totally confused why all pifr branches are labeled 6.x-*, while the instructions tell me to install it on a D7 site..

berdir’s picture

#17: No, that does not help.

We need this module to be enabled in the parent site, so that it is triggered and the files are added to the registry and available for testing.

The dependency should allow it to be relatively simple. PIFR just needs to check if the module exists and if yes, enable it.

donquixote’s picture

the files are added to the registry

You mean the D7 core autoload registry?
If the test classes are already in the registry, we don't need any xautoload or classloader anymore.
(xautoload or classloader may still be enabled at a later stage during the test, but that's a different story)

Would it be an option to let PIFR alter the D7 core autoload registry?
(and I still don't understand if PIFR is to run on a D6 or a D7 site..)

berdir’s picture

Right now, PIFR still runs on 6.x which means this is not possible anyway right now.

The port to 7.x is ongoing

xano’s picture

#17: No, that does not help.

You mean the findTestFiles() method? If so, why? I read the other issue, but I may be missing something.

donquixote’s picture

I would assume that findTestFiles() already does what we want, that is, finding those tests. Independent of the Drupal version.
It would be great if someone can confirm that :)

But then it fails at making those test classes autoload, because at the time this would need to happen, xautoload is not enabled.

donquixote’s picture

Berdir,
#1693398-29: Allow PSR-0 test classes to be used in D7 (followups)

I am now in favor of doing this all in core, with a custom PSR-0 loader only for test classes.
I'd like to get some feedback over there :)

It is generally difficult for a module to have tests about itself, if the testing module depends on it. So this is good news for xautoload.

donquixote’s picture

The core issue now has a patch which is green and awesome and wants to be reviewed.
#1693398-91: Allow PSR-0 test classes to be used in D7 (followups)

donquixote’s picture

Issue summary: View changes

It is simpletest, not simplenews :)

donquixote’s picture

This is now implemented in D7 core, so the simpletest integration can be removed in the upcoming xautoload-7.x-4.x

donquixote’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)

This qualifies as wontfix since #1693398: Allow PSR-0 test classes to be used in D7 (followups).

hook_simpletest_alter() is still in xautoload 7.x-3.x, but has been removed in 7.x-4.x.