繁体   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