diff --git a/Library/Homebrew/dev-cmd/test.rb b/Library/Homebrew/dev-cmd/test.rb
index b05dbbd2faa47cb5a766990fe230c64ef8dbf2ca..0a2e5b452c87188608e4158607da2fe5642dae63 100644
--- a/Library/Homebrew/dev-cmd/test.rb
+++ b/Library/Homebrew/dev-cmd/test.rb
@@ -25,6 +25,8 @@ module Homebrew
              description: "Test the head version of a formula."
       switch "--keep-tmp",
              description: "Retain the temporary files created for the test."
+      switch "--retry",
+             description: "Retry if a testing fails."
       switch :verbose
       switch :debug
       conflicts "--devel", "--HEAD"
@@ -70,7 +72,7 @@ module Homebrew
         next
       end
 
-      puts "Testing #{f.full_name}"
+      oh1 "Testing #{f.full_name}"
 
       env = ENV.to_hash
 
@@ -108,6 +110,7 @@ module Homebrew
           end
         end
       rescue Exception => e # rubocop:disable Lint/RescueException
+        retry if retry_test?(f)
         ofail "#{f.full_name}: failed"
         puts e, e.backtrace
       ensure
@@ -115,4 +118,16 @@ module Homebrew
       end
     end
   end
+
+  def retry_test?(f)
+    @test_failed ||= Set.new
+    if args.retry? && @test_failed.add?(f)
+      oh1 "Testing #{f.full_name} (again)"
+      f.clear_cache
+      true
+    else
+      Homebrew.failed = true
+      false
+    end
+  end
 end
diff --git a/docs/Manpage.md b/docs/Manpage.md
index 965cce33039dbe4743f42ed5856d0063a81a043c..22cca4677c197aeb674169ad81c88e9d265e71d8 100644
--- a/docs/Manpage.md
+++ b/docs/Manpage.md
@@ -1036,6 +1036,8 @@ wrong with the installed formula.
   Test the head version of a formula.
 * `--keep-tmp`:
   Retain the temporary files created for the test.
+* `--retry`:
+  Retry if a testing fails.
 
 ### `tests` [*`options`*]
 
diff --git a/manpages/brew.1 b/manpages/brew.1
index ab13db97554bfb75de9c1bdb2f68f89157a4345a..a9aad74da500d22c5a2bb51cde5a03427485712b 100644
--- a/manpages/brew.1
+++ b/manpages/brew.1
@@ -1351,6 +1351,10 @@ Test the head version of a formula\.
 \fB\-\-keep\-tmp\fR
 Retain the temporary files created for the test\.
 .
+.TP
+\fB\-\-retry\fR
+Retry if a testing fails\.
+.
 .SS "\fBtests\fR [\fIoptions\fR]"
 Run Homebrew\'s unit and integration tests\.
 .