testing - 如何使用 clojure.test 将值从 fixture 传递到测试?

标签 testing clojure fixtures

使用 clojure.test's use-fixture 时, 有没有办法将值从 fixture 函数传递到测试函数?

最佳答案

两个不错的选择是动态绑定(bind)和with-redefs。您可以从 fixture 中的测试命名空间绑定(bind)一个 var,然后在测试定义中使用它:

核心.clj:

(ns hello.core
  (:gen-class))

 (defn foo [x]
  (inc x))

测试/你好/core.clj:

(ns hello.core-test
  (:require [clojure.test :refer :all]
            [hello.core :refer :all]))

(def ^:dynamic *a* 4)

(defn setup [f]
  (binding [*a* 42]
    (with-redefs [hello.core/foo (constantly 42)]
      (f))))

(use-fixtures :once setup)

(deftest a-test
  (testing "testing the number 42"
    (is (= *a* (foo 75)))))

您可以通过比较直接调用测试(不使用固定装置)和通过 run-tests 调用它来判断它是否有效:

hello.core-test> (a-test)

FAIL in (a-test) (core_test.clj:17)
testing the number 42
expected: (= *a* (foo 75))
  actual: (not (= 4 76))
nil
hello.core-test> (run-tests)

Testing hello.core-test

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
{:test 1, :pass 1, :fail 0, :error 0, :type :summary}

这种方法之所以有效,是因为 fixtures 关闭了它们运行的​​测试,尽管它们实际上并没有直接(通常)直接调用测试函数,所以使用闭包来传递是有意义的测试代码的信息。

关于testing - 如何使用 clojure.test 将值从 fixture 传递到测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31735423/

相关文章:

clojure - cljc单文件宏定义可以与clojurescript一起使用吗?

ruby-on-rails - Ruby on Rails 使用外键删除固定装置

php - 如何以干净的方式传递测试更新方法的所有必填字段?

javascript - 使用 mocha,我如何运行名​​称中*没有* (slow) 的所有测试?

python - Python 成语 "if __name__ == ' __main_ _'"的 clojure 等价物是什么?

c# - 创建固定周数的灯具列表

python - 何时使用 TestClass 实例变量与 Pytest Fixtures

python - 使用用户对象在 Django 中测试表单

ruby-on-rails - 带有@javascript标签功能的 cucumber 文件下载

amazon-web-services - 如何从 Clojure 项目或 jar 创建 AWS Lambda 函数?