You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Clojure测试及lein test命令在首个用例失败时立即终止?

Stop Clojure Tests on First Failure & Make lein test Exit Immediately

Got it, let's tackle how to make your Clojure tests halt at the first failure and get lein test to exit right away with a failure status. Here are two reliable approaches to get this done:

1. Custom Test Runner (No External Plugins)

If you want full control without adding extra dependencies, you can build a custom test runner using Clojure's built-in clojure.test library. The idea is to override the report function to throw an exception as soon as a failure or error is encountered.

First, create a test runner namespace (e.g., test/my_runner.clj):

(ns test.my-runner
  (:require [clojure.test :as t]))

(defn stop-on-fail-report [m]
  ;; Update test counters like normal
  (t/inc-report-counter (:type m))
  (case (:type m)
    ;; On failure: print details then throw to stop execution
    :fail (do (t/print-test-fail-message m)
              (throw (ex-info "First test failure encountered" m)))
    ;; On error: same as failure
    :error (do (t/print-test-error-message m)
               (throw (ex-info "First test error encountered" m)))
    ;; For all other test events (pass, begin, end), use default printing
    (t/print-test-report m)))

(defn run-tests [& args]
  ;; Bind our custom report function and run tests
  (binding [t/report stop-on-fail-report]
    (apply t/run-tests args)))

Then configure your project.clj to use this runner:

(defproject my-project "0.1.0-SNAPSHOT"
  ;; ... your existing project config ...
  :test-runner {:main test.my-runner/run-tests})

Now when you run lein test, the test suite will stop instantly at the first failure or error, and Leiningen will exit with a non-zero status code.

2. Use the lein-fail-fast Plugin

For a quicker, no-code solution, use the lein-fail-fast plugin which handles this behavior out of the box.

First add the plugin to your project.clj:

(defproject my-project "0.1.0-SNAPSHOT"
  ;; ... your existing project config ...
  :plugins [[lein-fail-fast "0.5.0"]]
  ;; Optional: set this to true to enable fail-fast by default
  :fail-fast true)

If you don't set :fail-fast true, you can run tests with the flag explicitly:

lein test --fail-fast

Either way, the plugin will stop tests at the first failure and make lein test exit immediately with a failure status.

内容的提问来源于stack exchange,提问作者Aleksei Sotnikov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:26:20