【问题标题】:Clojure - tests with components strategyClojure - 使用组件策略进行测试
【发布时间】:2016-02-01 04:30:09
【问题描述】:

我正在使用 Stuart Sierra 组件实现一个应用程序。正如他在自述文件中所说:

有一种连贯的方式来设置和拆除所有相关的状态 与应用程序可以实现快速的开发周期,而无需 重新启动JVM。它还可以使单元测试更快更多 独立,因为创建和启动系统的成本很低 足以让每个测试都可以创建一个新的系统实例。

这里的首选策略是什么?类似于 JUnit oneTimeSetUp / oneTimeTearDown 的东西,或者真的在每个测试之间(类似于 setUp / tearDown)?

如果在每次测试之间,是否有一种简单的方法可以为所有测试(之前和之后)启动/停止系统,而无需每次都重复代码?

编辑:示例代码说明我的意思

(defn test-component-lifecycle [f]
  (println "Setting up test-system")
  (let [s (system/new-test-system)]
    (f s) ;; I cannot pass an argument here ( https://github.com/clojure/clojure/blob/master/src/clj/clojure/test.clj#L718 ), so how can I pass a system in parameters of a test ?
    (println "Stopping test-system")
    (component/stop s)))

(use-fixtures :once test-component-lifecycle)

注意:我在这里说的是单元测试。

【问题讨论】:

    标签: unit-testing testing clojure clojure-testing


    【解决方案1】:

    我会编写一个宏,它采用系统映射并在运行测试之前启动所有组件并在测试后停止所有组件。

    例如:

    (ns de.hh.new-test
     (:require [clojure.test :refer :all]
               [com.stuartsierra.component :as component]))
    
    
    ;;; Macro to start and stop component
    (defmacro with-started-components [bindings & body]
        `(let [~(bindings 0) (component/start ~(bindings 1))]
           (try
              (let* ~(destructure (vec (drop 2 bindings)))
                ~@body)
           (catch Exception e1#)
           (finally
             (component/stop ~(bindings 0))))))
    
    ;; Test Component
    (defprotocol Action
      (do-it [self]))
    
    (defrecord TestComponent [state]
       component/Lifecycle
       (start [self]
          (println "====> start")
          (assoc self :state (atom state)))
       (stop [self]
          (println "====> stop"))
    
       Action
        (do-it [self]
           (println "====> do action")
           @(:state self)))
    
    ;: TEST
    (deftest ^:focused component-test
       (with-started-components
          [system (component/system-map :test-component (->TestComponent"startup-state"))
           test-component (:test-component system)]
    
        (is (= "startup-state" (do-it test-component)))))
    

    运行测试你应该看到这样的输出

    ====> start
    ====> do action
    ====> stop
    
    Ran 1 tests containing 1 assertions.
    0 failures, 0 errors.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多