如何“参数化” Clojure Contrib 的测试是什么?

发布于 2024-08-14 08:15:13 字数 557 浏览 5 评论 0原文

JunitTestNG 提供了迭代输入参数集合并针对它们运行测试的机制。在 Junit 中,这是通过 支持的参数化注解,而TestNG使用@DataProvider

如何使用 test-is 库编写数据驱动的测试?我尝试使用 for 列表理解来迭代输入参数集合,但由于 deftest 是一个宏,因此它需要 is 子句。

Both Junit and TestNG provide mechanisms for iterating over a collection of input parameters and running your tests against them. In Junit this is supported via the Parameterized annotation, while TestNG uses @DataProvider.

How can you write data-driven tests using the test-is library? I tried using for list comprehension to iterate over an input parameter collection, but because deftest is a macro it's expecting is clauses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

标点 2024-08-21 08:15:13

从阅读有关 Junit 中参数化测试的文章来看,一旦您通过了样板,参数化最酷的部分就是它可以让您输入以下内容:

      return Arrays.asList(new Object[][] {
            { 2, true },
            { 6, false },
            { 19, true },
            { 22, false }

并轻松定义四个测试。

在 test-is 中,是等效的(不需要样板代码)宏是 are

(are [n prime?] (= prime? (is-prime n))  
     3 true
     8 false)

如果您想将输入作为映射提供,那么您可以运行类似的命令:

(dorun (map #(is (= %2 (is-prime %1)) 
            { 3 true, 8 false}))

虽然 are > 宏将产生更容易阅读的输出。

From reading the article on parameterized tests in Junit it seems that once you get past the poiler plate the cool part of parameterization is that it lets you type this:

      return Arrays.asList(new Object[][] {
            { 2, true },
            { 6, false },
            { 19, true },
            { 22, false }

and easily define four tests.

in test-is the equivalent (no boiler-plate code required) macro is are

(are [n prime?] (= prime? (is-prime n))  
     3 true
     8 false)

If you want to give your inputs as a map then you could run something like:

(dorun (map #(is (= %2 (is-prime %1)) 
            { 3 true, 8 false}))

though the are macro will produce more easily read output.

遇到 2024-08-21 08:15:13

不确定我是否理解参数化测试的意义,但我会为此使用动态绑定。

user> (def *test-data* [0 1 2 3 4 5])
#'user/*test-data*

user> (deftest foo
        (doseq [x *test-data*]
           (is (< x 4))))
#'user/foo
user> (run-tests)

Testing user

FAIL in (foo) (NO_SOURCE_FILE:1)
expected: (< x 4)
  actual: (not (< 4 4))

FAIL in (foo) (NO_SOURCE_FILE:1)
expected: (< x 4)
  actual: (not (< 5 4))

Ran 1 tests containing 6 assertions.
2 failures, 0 errors.
nil

user> (defn run-tests-with-data [data]
        (binding [*test-data* data] (run-tests)))
#'user/run-tests-with-data

user> (run-tests-with-data [0 1 2 3])

Testing user

Ran 1 tests containing 4 assertions.
0 failures, 0 errors.
nil

您可以自己重写 deftestrun-tests 。让测试以其他方式接受参数可能需要十几行 Clojure。

Not sure I understand the point of parameterized tests, but I would use dynamic binding for this.

user> (def *test-data* [0 1 2 3 4 5])
#'user/*test-data*

user> (deftest foo
        (doseq [x *test-data*]
           (is (< x 4))))
#'user/foo
user> (run-tests)

Testing user

FAIL in (foo) (NO_SOURCE_FILE:1)
expected: (< x 4)
  actual: (not (< 4 4))

FAIL in (foo) (NO_SOURCE_FILE:1)
expected: (< x 4)
  actual: (not (< 5 4))

Ran 1 tests containing 6 assertions.
2 failures, 0 errors.
nil

user> (defn run-tests-with-data [data]
        (binding [*test-data* data] (run-tests)))
#'user/run-tests-with-data

user> (run-tests-with-data [0 1 2 3])

Testing user

Ran 1 tests containing 4 assertions.
0 failures, 0 errors.
nil

You could rewrite deftest and run-tests yourself. It'd be maybe a dozen lines of Clojure to let tests accept parameters in some other way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文