diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index e4182843de9a474fbf7a3814d350451665a12ef7..284df1d360af399da5d909af103d0e358c3b29c8 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -1132,15 +1132,28 @@ protected function curlExec($curl_options, $redirect = FALSE) {
     if (!empty($this->curlCookies)) {
       $cookies = $this->curlCookies;
     }
-    // In order to debug webtests you need to either set a cookie or have the
-    // xdebug session in the URL. If the developer listens to connection on the
-    // parent site, by default the cookie is not forwarded to the client side,
-    // so you can't debug actual running code. In order to make debuggers work
+    // In order to debug web tests you need to either set a cookie, have the
+    // Xdebug session in the URL or set an environment variable in case of CLI
+    // requests. If the developer listens to connection on the parent site, by
+    // default the cookie is not forwarded to the client side, so you cannot
+    // debug the code running on the child site. In order to make debuggers work
     // this bit of information is forwarded. Make sure that the debugger listens
     // to at least three external connections.
     if (isset($_COOKIE['XDEBUG_SESSION'])) {
       $cookies[] = 'XDEBUG_SESSION=' . $_COOKIE['XDEBUG_SESSION'];
     }
+    // For CLI requests, the information is stored in $_SERVER.
+    if (isset($_SERVER['XDEBUG_CONFIG'])) {
+      // $_SERVER['XDEBUG_CONFIG'] has the form "key1=value1 key2=value2 ...".
+      $pairs = explode(' ', $_SERVER['XDEBUG_CONFIG']);
+      foreach ($pairs as $pair) {
+        list($key, $value) = explode('=', $pair);
+        // Account for key-value pairs being separated by multiple spaces.
+        if (trim($key, ' ') == 'idekey') {
+          $cookies[] = 'XDEBUG_SESSION=' . trim($value, ' ');
+        }
+      }
+    }
 
     // Merge additional cookies in.
     if (!empty($cookies)) {