Install Drupal with the Minimal install profile, then enable the Testing module.
Go to the Testing page, and run the "Search Excerpt Extraction" test (in the Search group).
You will immediately get a failure error like "Fatal error: Call to undefined function search_excerpt() " (this is a function in search.module).
Now if you enable the Search module and then run the test, you are fine.
So it seems as though simpletest is assuming it doesn't need to enable the search module, although that test says it needs to be enabled in its setup function?
function setUp() {
parent::setUp('search');
}
Comments
Comment #2
mfbOnly the WebTests can install modules, nothing happens if you try to enable a module in a UnitTest setUp function. See http://api.scratch.drupal.org/api/drupal/modules--simpletest--drupal_web...
A work-around would be to include the module file manually.
Comment #3
klausiComment #4
klausiNot sure what the best solution is here. Simpletest just lists all test cases on the testing page, which is a problem for unit tests that cannot specify module dependencies. We could do this hack with the manual include, but then this statement should stay in the setUp() method I think.
Comment #5
damien tournoud commented#2 is the correct solution.
Comment #6
webchickThis is an API change and a WTF for test writers. Don't like this approach.
We should fix this by changing DrupalUnitTestCase::setUp() or whatever so that it accepts the setUp('module') syntax in tests and does the proper require_once statement behind the scenes.
Comment #7
damien tournoud commentedNope.
DrupalUnitTestCase cannot install module. That's by design. There is no API change at all here.
Comment #8
klausiI disagree. Setup tasks should stay in setUp(). Sorry for the whitespace fixes, Eclipse does that automatically.
Comment #9
jhodgdonDamien: why do you think DrupalUnitTestCase should not allow the modules the use case is testing to be installed? I guess means that for any module that is not part of the Standard install profile (which I think is what is loaded during automatic tests), we cannot define a Unit Test that will run successfully by automated testing, without explicitly loading the module inside the test?
That just seems silly. Can you explain why that is the design?
Comment #10
mfbThe UnitTests don't access the database. How would you install a module without accessing the database?
See http://api.scratch.drupal.org/api/drupal/modules--simpletest--drupal_web... "These tests can not access the database nor files. Calling any Drupal function that needs the database will throw exceptions. These include watchdog(), module_implements(), module_invoke_all() etc."
All you can do is include a module.
Comment #11
webchickRight. So we document that setUp('module') in DrupalUnitTestCase behaves differently (require_once $foo.module) because there isn't a database available. But test authors don't have to learn a whole new test syntax.
Comment #12
jhodgdon+1 to webchick's plan - to make it look like setup works the same in DrupalUnitTestCase as for DrupalWebTestCase.
In which case, the patches above don't do that. They are rather putting band-aids on the problem by fixing this one test case that I happened to notice doesn't work, and not addressing any other unit tests in core that might also have the same problem.
Comment #13
mfbHaving it work the same could also cause some confusion.. Developers might think their module is actually being installed when it isn't?
I grepped the core test files. batch.test and graph.test have their require_once statements at the beginning of their test methods. The other unit tests are for required modules: filter.test simpletest.test system.test or include files: bootstrap.test common.test menu.test so I believe search.test is the only case of this problem in core.
By the way, it might be prettier to do drupal_load('module', 'search') than require_once? This function can work with or without the database so seems to work here.
Comment #14
jhodgdonOK. Well let's get a clean patch that just does the module load for search.module then (skip the other changes), and leave it at that.
Comment #15
jhodgdonOh, and maybe we should also establish the pattern that all these includes should be in the setup functions? That seems like a reasonable idea from the patch in #8, so it sounds like the graph and batch tests should do that?
Comment #16
mfbhere's a patch as per #15
Comment #17
jhodgdonThis docblock needs to start with a one-line description:
I like the description here, so let's keep that, but all docblocks must start with a one-line description of what the function does.
Other than that, I am OK with the patch in #15.
Comment #18
mfbadded one-liner
Comment #19
jhodgdonLooks fine. I think this is a reasonable approach, and at least it's now documented.
Comment #20
webchickThat works for me. What say you, Damien?
Comment #21
damien tournoud commentedWorks for me too.
Comment #22
dries commentedGreat. We all like it then. Committed to CVS HEAD.