diff --git a/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php b/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php
index 02ea524b09..423d144c4f 100644
--- a/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php
+++ b/core/modules/system/tests/modules/test_page_test/src/Controller/TestPageTestController.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\test_page_test\Controller;
 
+use Drupal\user\Entity\Role;
+
 /**
  * Controller routines for test_page_test routes.
  */
@@ -23,4 +25,16 @@ public function testPage() {
     ];
   }
 
+  /**
+   * Returns a test page and with the call to the dump() method.
+   */
+  public function testPageVarDump() {
+    $role = Role::create(['id' => 'test_role']);
+    dump($role);
+    return [
+      '#title' => t('Test page with var dump'),
+      '#markup' => t('Test page text.'),
+    ];
+  }
+
 }
diff --git a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml
index 43b6cf54c1..f3744df5ee 100644
--- a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml
+++ b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml
@@ -144,3 +144,11 @@ test_page_test.deprecations:
     _controller: '\Drupal\test_page_test\Controller\Test::deprecations'
   requirements:
     _access: 'TRUE'
+
+test_page_test.test_page_var_dump:
+  path: '/test-page-var-dump'
+  defaults:
+    _title: 'Test front page with var dump'
+    _controller: '\Drupal\test_page_test\Controller\TestPageTestController::testPageVarDump'
+  requirements:
+    _access: 'TRUE'
diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
index 4be2efc648..befbf33024 100644
--- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
+++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
@@ -917,4 +917,23 @@ public function testDeprecationHeaders() {
     $this->assertCount(1, $test_deprecation_messages);
   }
 
+  /**
+   * Tests the dump() method provided by the Symfony component var-dump.
+   */
+  public function testVarDump() {
+    // Visit a Drupal page with call to the dump() method.
+    $body = $this->drupalGet('test-page-var-dump');
+    $this->assertSession()->statusCodeEquals(200);
+
+    // It is too strict to assert all properties of the Role and it is easy to
+    // break if one of these properties gets removed or gets a new default
+    // value. It should be sufficient to test just a couple of properties.
+    $this->assertStringContainsString('<span class=sf-dump-note>', $body);
+    $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">id</span>: "<span class=sf-dump-str title="9 characters">test_role</span>"', $body);
+    $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">label</span>: <span class=sf-dump-const>null</span>', $body);
+    $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">permissions</span>: []', $body);
+    $this->assertStringContainsString('  #<span class=sf-dump-protected title="Protected property">uuid</span>: "', $body);
+    $this->assertStringContainsString('</samp>}', $body);
+  }
+
 }
diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php
index afe40b747a..e820555aa8 100644
--- a/core/tests/Drupal/KernelTests/KernelTestBase.php
+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -22,6 +22,7 @@
 use Drupal\Tests\TestRequirementsTrait;
 use Drupal\Tests\Traits\PHPUnit8Warnings;
 use Drupal\TestTools\Comparator\MarkupInterfaceComparator;
+use Drupal\TestTools\TestVarDumper;
 use PHPUnit\Framework\Exception;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\DependencyInjection\Reference;
@@ -30,6 +31,7 @@
 use org\bovigo\vfs\visitor\vfsStreamPrintVisitor;
 use Drupal\Core\Routing\RouteObjectInterface;
 use Symfony\Component\Routing\Route;
+use Symfony\Component\VarDumper\VarDumper;
 
 /**
  * Base class for functional integration tests.
@@ -221,6 +223,7 @@ abstract class KernelTestBase extends TestCase implements ServiceProviderInterfa
    */
   public static function setUpBeforeClass() {
     parent::setUpBeforeClass();
+    VarDumper::setHandler(TestVarDumper::class . '::cliHandler');
 
     // Change the current dir to DRUPAL_ROOT.
     chdir(static::getDrupalRoot());
diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php
index 8a00387e70..4c21c82adc 100644
--- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php
+++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php
@@ -4,6 +4,7 @@
 
 use Drupal\Component\FileCache\FileCacheFactory;
 use Drupal\Core\Database\Database;
+use Drupal\user\Entity\Role;
 use org\bovigo\vfs\vfsStream;
 use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
 use PHPUnit\Framework\SkippedTestError;
@@ -340,4 +341,24 @@ public function testKernelTestBaseInstallSchema() {
     $this->assertFalse(Database::getConnection()->schema()->tableExists('key_value'));
   }
 
+  /**
+   * Tests the dump() method provided by the Symfony component var-dump.
+   */
+  public function testVarDump() {
+    $this->enableModules(['system', 'user']);
+    $role = Role::create(['id' => 'test_role']);
+    $uuid = $role->uuid();
+    $this->expectOutputRegex('|Drupal\\\\user\\\\Entity\\\\Role|');
+    // Assert that `#foo: bar\n` like pattern presents.
+    $this->expectOutputRegex('|#.+:.+\\\n|');
+    // It is too strict to assert all properties of the Role and it is easy to
+    // break if one of these properties gets removed or gets a new default
+    // value. It should be sufficient to test just a couple of properties.
+    $this->expectOutputRegex('|id.+test_role|');
+    $this->expectOutputRegex('|label.+null|');
+    $this->expectOutputRegex('|permissions.+\[\]|');
+    $this->expectOutputRegex("|uuid.+$uuid|");
+    dump($role);
+  }
+
 }
diff --git a/core/tests/Drupal/TestTools/TestVarDumper.php b/core/tests/Drupal/TestTools/TestVarDumper.php
new file mode 100644
index 0000000000..e18338d212
--- /dev/null
+++ b/core/tests/Drupal/TestTools/TestVarDumper.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Drupal\TestTools;
+
+use Symfony\Component\VarDumper\Cloner\VarCloner;
+use Symfony\Component\VarDumper\Dumper\CliDumper;
+use Symfony\Component\VarDumper\Dumper\HtmlDumper;
+
+/**
+ * Provides handlers for the Symfony VarDumper to work within tests.
+ *
+ * This allows the dump() function to produce output on the terminal without
+ * causing PHPUnit to complain.
+ */
+class TestVarDumper {
+
+  /**
+   * A CLI handler for \Symfony\Component\VarDumper\VarDumper.
+   */
+  public static function cliHandler($var) {
+    $cloner = new VarCloner();
+    $dumper = new CliDumper();
+    fwrite(STDOUT, "\n");
+    $dumper->setColors(TRUE);
+    $dumper->dump(
+      $cloner->cloneVar($var),
+      function ($line, $depth, $indent_pad) {
+        // A negative depth means "end of dump".
+        if ($depth >= 0) {
+          // Adds a two spaces indentation to the line.
+          print str_repeat($indent_pad, $depth) . $line . "\n";
+        }
+      }
+    );
+  }
+
+  /**
+   * A HTML handler for \Symfony\Component\VarDumper\VarDumper.
+   */
+  public static function htmlHandler($var) {
+    $cloner = new VarCloner();
+    $dumper = new HtmlDumper();
+    $dumper->dump($cloner->cloneVar($var));
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php
index 0aac9ce747..fa27b4aa13 100644
--- a/core/tests/Drupal/Tests/BrowserTestBase.php
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -19,11 +19,13 @@
 use Drupal\Tests\Traits\PHPUnit8Warnings;
 use Drupal\Tests\user\Traits\UserCreationTrait;
 use Drupal\TestTools\Comparator\MarkupInterfaceComparator;
+use Drupal\TestTools\TestVarDumper;
 use GuzzleHttp\Cookie\CookieJar;
 use PHPUnit\Framework\TestCase;
 use Psr\Http\Message\RequestInterface;
 use Psr\Http\Message\ResponseInterface;
 use Symfony\Component\CssSelector\CssSelectorConverter;
+use Symfony\Component\VarDumper\VarDumper;
 
 /**
  * Provides a test case for functional Drupal tests.
@@ -212,6 +214,14 @@ abstract class BrowserTestBase extends TestCase {
    */
   protected $originalContainer;
 
+  /**
+   * {@inheritdoc}
+   */
+  public static function setUpBeforeClass() {
+    parent::setUpBeforeClass();
+    VarDumper::setHandler(TestVarDumper::class . '::htmlHandler');
+  }
+
   /**
    * Initializes Mink sessions.
    */
