diff --git a/meta/lib/patchtest/selftest/selftest b/meta/lib/patchtest/selftest/selftest
index 26d1b61227..d922e12206 100755
--- a/meta/lib/patchtest/selftest/selftest
+++ b/meta/lib/patchtest/selftest/selftest
@@ -56,6 +56,8 @@ def analyze_result(results, patch, counts, return_code):
     expected_result = str(patch["expected"])
     for resultline in results.splitlines():
         if testid in resultline:
+            if ':' not in resultline:
+                continue
             result, _ = resultline.split(':', 1)
 
             if expected_result.upper() == "FAIL" and result.upper() == "FAIL" and return_code != 0:
@@ -80,7 +82,10 @@ def analyze_result(results, patch, counts, return_code):
                      counts["error"] = counts["error"] + 1
             break
     else:
-        print ("No test for=%s" % patch["patch"])
+        if return_code != 0:
+            print("patchtest failed before producing results:\n%s" % results.strip())
+            sys.exit(return_code)
+        print("No test for=%s" % patch["patch"])
 
     return counts
 
@@ -131,12 +136,10 @@ def git_detach_head():
 
 # Once the tests are in oe-core, we can remove the testdir param and use os.path.dirname to get relative paths
 def test(root, patch):
-    res = True
     patchpath = os.path.abspath(os.path.join(root, patch))
-    
-    cmd     = 'patchtest --base-commit HEAD --repodir %s --testdir %s/tests --patch %s' % (repodir, topdir, patchpath)
-    results = subprocess.run(cmd, capture_output=True, universal_newlines=True, shell=True)
-
+    cmd = 'patchtest --base-commit HEAD --repodir %s --testdir %s/tests --patch %s' % (repodir, topdir, patchpath)
+    results = subprocess.run(cmd, universal_newlines=True, shell=True,
+                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
     return results.returncode, results.stdout
 
 def test_head_attached(patches, counts, branch):
@@ -171,10 +174,12 @@ def run_tests(patches, counts):
     temp_branch = "test_patchtest_head_attached"
     git_state = get_git_state()
     assert git_state['branch'] != temp_branch, f"Cannot run patchtest selftest while on branch '{temp_branch}'"
-    counts = test_head_attached(patches, counts, temp_branch)
-    counts = test_head_detached(patches, counts)
-    restore_git_state(git_state)
-    run_sh(f"git branch -D {temp_branch}")
+    try:
+        counts = test_head_attached(patches, counts, temp_branch)
+        counts = test_head_detached(patches, counts)
+    finally:
+        restore_git_state(git_state)
+        run_sh(f"git branch -D {temp_branch}")
 
     return counts
 
