diff --git a/dmemcache.inc b/dmemcache.inc
index 5d8d541..5a1c23c 100644
--- a/dmemcache.inc
+++ b/dmemcache.inc
@@ -328,6 +328,11 @@ function dmemcache_key($key, $bin = 'cache') {
     if ($prefix = variable_get('memcache_key_prefix', '')) {
       $prefix .= '-';
     }
+    // When simpletest is running, emulate the simpletest database prefix here
+    // to avoid the child site setting memcache entries for the parent site.
+    if (isset($GLOBALS['drupal_test_info']['test_run_id'])) {
+      $prefix .= $GLOBALS['drupal_test_info']['test_run_id'];
+    }
   }
   $full_key = urlencode($prefix . $bin . '-' . $key);
 
