Bundle not loaded in doc test

I am writing a custom add-on that registers a resource registry bundle with a css and a js file. Everything works fine in the browser, but the trouble starts when I try to execute some doc tests. I want to test that the response contains my <link ...rel="stylesheet" ... data-bundle="mybundle"... and my <script ...data-bundle="mybundle" tags, and it does not. (When I load the page manually in the browser, those tags exist.)
In case you are wondering, I am doing this because later I want to put an expression on my registered bundle so that these resources only get included on certain pages, and I want to be able to test this.

The standard test_setup.py tests run fine, and I know my add-on is installed in the test layer.

Below is the doc test. I explicitly go to prefs_install_products_form to check that the add-on is installed, and to @@resourceregistry-controlpanel to check that the bundle exists, just to be sure.

Bundle Style and Script Tags Present                                                                                                   
------------------------------------                                                                                                   
                                                                                                                                       
  >>> from plone.testing.zope import Browser                                                                                           
  >>> from plone.app.testing import SITE_OWNER_NAME                                                                                    
  >>> from plone.app.testing import SITE_OWNER_PASSWORD                                                                                
  >>> app = layer['app']                                                                                                               
  >>> browser = Browser(app)                                                                                                           
                                                                                                                                       
  >>> browser.open('http://nohost/plone/login_form')                                                                                   
  >>> browser.getControl('Login Name').value = SITE_OWNER_NAME                                                                         
  >>> browser.getControl('Password').value = SITE_OWNER_PASSWORD                                                                       
  >>> browser.getControl('Log in').click()                                                                                             
                                                                                                                                       
Create object                                                                                                                          
                                                                                                                                       
  >>> browser.getLink('Page').click()                                                                                                  
  >>> browser.getControl('Title').value = 'Front Page'                                                                                 
  >>> browser.getControl('Save').click()                                                                                               
                                                                                                                                       
Go to Add-ons control panel                                                                                                            
                                                                                                                                       
  >>> browser.open('http://nohost/plone/prefs_install_products_form')                                                                  
  >>> browser.contents                                                                                                                 
  '...<input...name="uninstall_product" value="myaddon...'                                                                 

Go to Resource Registry control panel
  >>> browser.open('http://nohost/plone/@@resourceregistry-controlpanel')
  >>> browser.contents
  '...mybundle...'                                                                                                                                           

Check object view                                                                                                                      
                                                                                                                                       
  >>> browser.open('http://nohost/plone/front-page')                                                                                   
  >>> browser.contents                                                                                                                 
  '...<script ... data-bundle="mybundle...'        <---- this fails                                                                                    
  1. Why does this fail in doctests?
  2. Is there a better way?
  3. How can I monitor/inspect the stdout/stderr stream from the instance that is driven by the tests, the way it would normally get written to a log file or to the console in 'fg' mode?

Thanks!

I figured it out. There were two problems:

  1. debug vs production: I was testing manually with the site runnning in fg mode, while bin/test runs in "production" mode. I realized this by putting a breakpoint in Products.CMFPlone.resources.browser.resources.ordered_bundles_result().
  2. Merge with: The GenericSetup registry record for a bundle has a <value key="merge_with">, which I had copied and pasted from somewhere else with the value default. As a result, my resources were in fact being loaded even under test, but merged with the default.js bundle, making the test fail. In fg mode the merging does not happen, so the resources were loaded in their own bundle.

The solution in this case is to leave the merge_with record value empty. This is just a solution in the sense that it makes my tests behave the way I expect, although it's not necessarily the best way to handle resources.