簡體   English   中英

一種鏈接測試的方法,一個在clojure中一個接一個地運行?

[英]A way to chain the test, one running after the other in clojure?

Lein test以隨機順序運行我的功能。

我有兩個函數可以修改相同的數據。 我需要先運行第一個,然后再運行第二個。 我的測試順序

例:

;;===============my file=============
;;this fails if x and y are not found.
(defn create-data [x y]
  (go add x y))

;;if the update function doesn't find x and y it adds them so create-data fails when it runs after update-data
(defn update-data [x y]
  (go update x y))

;;======my file test=======
(deftest create-test
  (testing "this should run first"
   (is (= 20 create-data)))

(deftest create-test
  (testing "this should run second"
   (is (= 20 update-data)))

因此,我認為為這兩個功能創建一個測試將使其正常工作,但沒有成功。

(deftest test-create-update.
  (testing "this should run second"
    (is (= 20 create-data))
    (is (= 20 update-data)))

我想要可以同時運行這兩個功能但首先要運行創建數據的東西,無論結果(通過還是失敗)都將運行更新數據。 我的考試都需要。 他們各自工作。 但是我需要自動測試。

您可以使用測試夾具來創建和拆除測試環境。 可以針對所有測試或每個單獨的測試執行此操作。

參見使用夾具

; Here we register my-test-fixture to be called once, wrapping ALL tests 
; in the namespace
(use-fixtures :once my-test-fixture)

如果要對多個名稱空間強制執行順序,則可以將它們包裝在my-test-fixture

為這兩個功能創建一個測試的直覺是一個很好的方法。 您遇到的問題很可能與測試無關。

您發布的代碼(go add xy)表明您正在使用core.async。 有一些問題:

  1. go塊返回一個通道,並且其中的代碼“稍后”執行,因此,除非您對結果進行阻止,否則無法保證會發生任何事情。
  2. (go add xy)不執行任何功能,它僅返回y。 您可能想要(!< (go (add xy)))
  3. 除非add通過原子或ref或類似方法修改某些狀態,否則什么都不會改變。

我相信這里的真正問題是代碼,而不是測試。 或者,如果代碼“有效”,那是因為您的測試沒有阻塞。 能否請你提供什么更詳細的goadd在你的背景?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM