module_load_include fails on WebTestCase post I am writing test cases for my website using SimpleTest module. One of my test cases uses the following sequence

    $this-&gt;drupalGet( "myurl" array('query' =&gt; $params));
    $this-&gt;assertFieldByName('giftaid_granted','', "Giftaid checkbox exists");

    // now we will also attempt to submit a form and check that it goes out
    $this-&gt;drupalPost(NULL, array(), t('Submit'));


When I run this code, I get an exception in the test

Fatal error: Call to undefined function module_load_include() in ...sites/all/modules/mymodule/includes/entities.inc on line 3

1   0.0001  235336  {main}( )   ../index.php:0
2   0.0023  660936  drupal_bootstrap( ) ../index.php:20
3   0.0025  678144  _drupal_bootstrap_page_cache( ) ../bootstrap.inc:2236
4   0.0029  757040  drupal_bootstrap( ) ../bootstrap.inc:2378
5   0.0044  1174072 _drupal_bootstrap_variables( )  ../bootstrap.inc:2244
6   0.0046  1199928 variable_initialize( )  ../bootstrap.inc:2480
7   0.0102  1626520 array_map ( )   ../bootstrap.inc:935
8   0.0102  1631120 unserialize ( ) ../bootstrap.inc:935
9   0.0102  1652040 spl_autoload_call ( )   ../bootstrap.inc:0
10  0.0102  1652112 drupal_autoload_class( )    ../bootstrap.inc:0
11  0.0102  1652376 _registry_check_code( ) ../bootstrap.inc:3107
12  0.0143  2484552 require_once( '...sites/all/modules/mymodule/includes/entities.inc' )   ../bootstrap.inc:3186


The offending line is apparently the module_load_include('inc', 'mymodule', 'includes/shared'); on top of my entities.inc file.

As best I can guess from the trace, there is some sort of cached variable causing class autoloading in bootstrap, before module_load_include function is defined. But form submission on a normal page works just fine.

Am I using module_load_include incorrectly? Or is it some problem with the SimpleTest?
I figured it out for myself. This is an issue with SimpleTest. DrupalWebTestCase stores emails sent in the database, in a varialbe drupal_test_email_collector. If an email sent during a test case includes parameters, the parameters are saved. My parameters were classes. bootstrap.inc attempts to load and cache variables before modules are loaded. In restoring the emails with parameters, it was attempting to autoload the relevant class, and that was causing the crash.

This could happen, in principle, in other situations - any time a class got saved to a variable, though I have only encountered it with this test setup.