【问题标题】:Clojure Test with Multiple Assertions and Reporting具有多个断言和报告的 Clojure 测试
【发布时间】:2019-03-01 13:32:30
【问题描述】:

我在使用 clojure.test 测试框架报告失败时遇到了一些问题。

现在,我知道我可以为不同的报告覆盖一些函数,以便将其打印到控制台或我希望打印到的任何位置。我也知道我可以将此输出保存到文件中。

我的问题如下......当我像这个例子一样声明一个deftest时:

(deftest test1 (is (= 1 1) (is (= 2 1))

此测试将运行,如果我执行(run-tests)(test-var #'test1) 之类的操作,它将返回 nil,但打印失败。

我决定重写 :fail 方法进行报告,因为我想要的是这样的失败地图:{"expected" (:expected m), "actual" (:actual m)} 如果我只使用报告功能,这有点工作。

问题在于,当您通过 Clojure.test 框架运行测试时,会调用许多宏,但它的行为并不完全符合我的要求。

我的最终目标是:运行测试,如果有任何失败,而不是打印它们,将它们保存到地图并将地图返回给我。如果他们都通过了,那么我不在乎它会给我带来什么。

这甚至可能吗?如果某个测试失败,我不想停止测试,我只想将它记录在某个地方,最好是地图。


来源:

Clojure test with mutiple assertions

https://clojure.github.io/clojure/branch-1.1.x/clojure.test-api.html

https://groups.google.com/forum/#!topic/clojure/vCjso96wqps

【问题讨论】:

    标签: testing clojure frameworks clojure.test


    【解决方案1】:

    恐怕没有简单的方法可以做到这一点。 您可以提供clojure.test/report :fail defmethod 的自定义实现并将结果存储在原子中,但很难将结果传播到外层。

    如果您只使用test-var,那么它是可行的,但请注意在这种情况下不会执行测试夹具 - 请参阅test-vars source

    (:use clojure.test)
    
    (deftest failing
      (testing "fail me"
        (is (= 1 0))
        (is (= 2 1))
        (is (= 3 2))))
    
    (def test-failures (atom []))
    
    (defmethod report :fail [m]
      (swap! test-failures
             (fn [previous-failures current-failure]
               (conj previous-failures current-failure))
             {:test-var-str (testing-vars-str m)
              :expected (:expected m)
              :actual (:actual m)}))
    
    (defmethod report :end-test-var [m]
      @test-failures)
    
    (defn run-test-var [v]
      (reset! test-failures [])
      (test-var v))
    
    ;; in REPL:
    (run-test-var #'failing)
    ;; =>
    [{:test-var-str "(failing) (form-init4939336553149581727.clj:159)", :expected 1, :actual (0)}
     {:test-var-str "(failing) (form-init4939336553149581727.clj:160)", :expected 2, :actual (1)}
     {:test-var-str "(failing) (form-init4939336553149581727.clj:161)", :expected 3, :actual (2)}]
    

    还有defmethod report :end-test-ns,但这个不是很有用,因为test-ns function返回@*report-counters*

    【讨论】:

    • 感谢您的评论!这有点像我想要的。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多