let failfirst t =
  let was_successful = OUnitResultSummary.was_successful in
  let rec find_failing =
    function
      | path :: tl ->
          begin
            match OUnitCache.get_result path t.cache with
              | Some result ->
                  (* Find the first formerly failing test. *)
                  if was_successful [path, result, Nonethen
                    find_failing tl
                  else
                    Choose path
              | None ->
                  Choose path
          end
      | [] ->
          begin
            let wait_results_running =
              List.fold_left
                (fun wait path ->
                   match OUnitCache.get_result path t.cache with
                     | Some result ->
                         (not (was_successful [path, result, None])) || wait
                     | None ->
                         (* No former result, we need the result of
                          * this test.
                          *)

                         true)
                false t.tests_running
            in
              if wait_results_running then
                (* We need more data about currently running tests. *)
                ChooseToPostpone
              else if was_successful t.tests_passed then
                (* All tests that were red has become green, continue. *)
                simple t
              else
                (* Some tests still fail, skip the rest. *)
                allskip t
          end
  in
    find_failing t.tests_planned