简体   繁体   English

一种链接测试的方法,一个在clojure中一个接一个地运行?

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

Lein test runs my functions in random order. Lein test以随机顺序运行我的功能。

I have two functions that modify the same data. 我有两个函数可以修改相同的数据。 I need the first one running first and the second one after that. 我需要先运行第一个,然后再运行第二个。 The order in my test firles 我的测试顺序

Example: 例:

;;===============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)))

so I thought creating one test for both functions will make it work but it doesn't. 因此,我认为为这两个功能创建一个测试将使其正常工作,但没有成功。

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

I want something that will run both functions but will run create-data first for sure and regardless of the result (whether passed or failed) will run the update-data. 我想要可以同时运行这两个功能但首先要运行创建数据的东西,无论结果(通过还是失败)都将运行更新数据。 I need both in my test. 我的考试都需要。 Individually they work. 他们各自工作。 but i need automated testing. 但是我需要自动测试。

You could use test fixtures to create and tear down test environments. 您可以使用测试夹具来创建和拆除测试环境。 This can be done for all tests, or on every individual tests. 可以针对所有测试或每个单独的测试执行此操作。

See use-fixtures : 参见使用夹具

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

If you want to enforce order over several namespaces, you could wrap them in my-test-fixture . 如果要对多个名称空间强制执行顺序,则可以将它们包装在my-test-fixture

Your intuition for creating one test for both functions is a fine approach. 为这两个功能创建一个测试的直觉是一个很好的方法。 The issue you are having is most likely not related to testing. 您遇到的问题很可能与测试无关。

The code you posted (go add xy) suggest you are using core.async. 您发布的代码(go add xy)表明您正在使用core.async。 There are some problems: 有一些问题:

  1. go blocks return a channel and the code in them is executed "sometime later", so unless you block on the result, you are not guaranteed that anything has happened. go块返回一个通道,并且其中的代码“稍后”执行,因此,除非您对结果进行阻止,否则无法保证会发生任何事情。
  2. (go add xy) does not perform any functions, it just returns y. (go add xy)不执行任何功能,它仅返回y。 You probably want (!< (go (add xy))) 您可能想要(!< (go (add xy)))
  3. Unless add modifies some state via an atom or ref or similar, nothing will have changed. 除非add通过原子或ref或类似方法修改某些状态,否则什么都不会改变。

I believe the real problem here is with the code, not the tests. 我相信这里的真正问题是代码,而不是测试。 Or if the code "works" then it is because you have no blocking in your test. 或者,如果代码“有效”,那是因为您的测试没有阻塞。 Can you please provide more detail on what go and add are in your context? 能否请你提供什么更详细的goadd在你的背景?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM