使用clojure.test创建一个“慢速”测试套件

15

我希望每次运行lein test都能运行这个测试:

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

(deftest ackermann-test
  (testing "ack-1, ack-2, ack-3"
    (are [m n e]
         (= (ack-1 m n) (ack-2 m n) (ack-3 m n) e)
         0 0  1
         0 1  2
         0 2  3
         1 0  2
         1 1  3
         1 2  4
         2 0  3
         2 1  5
         2 2  7
         3 0  5
         3 1 13
         3 2 29)))
我希望只有在我要求时才运行ackermann-slow-test
(deftest ackermann-slow-test
  (testing "ackermann (slow)"
    (are [m n e] (= (ack-3 m n) e)
         3 3     61
         3 4    125
         4 0     13
         4 1  65533)))

完整代码可在Github上获取:https://github.com/bluemont/ackermann

1个回答

31
根据 Phil Hagelberg 的 Making Leiningen work for You 所述,test-selectors 功能是在版本 1.4 中添加到 Leiningen 的。
两个简单步骤。首先,在 project.clj 中添加以下内容:
:test-selectors {:default (complement :slow)
                 :slow :slow
                 :all (constantly true)}

第二步,使用元数据标记您的测试:
(deftest ^:slow ackermann-slow-test
  (testing "ackermann (slow)"
    (are [m n e] (= (ack-3 m n) e)
         3 3     61
         3 4    125
         4 0     13
         4 1  65533)))

现在您有三种运行测试的选项:
⚡ lein test
⚡ lein test :slow
⚡ lein test :all

此外,你可以通过 lein test -h 命令轻松找到这些信息:

Run the project's tests.

Marking deftest or ns forms with metadata allows you to pick selectors to specify a subset of your test suite to run:

(deftest ^:integration network-heavy-test
  (is (= [1 2 3] (:numbers (network-operation)))))

Write the selectors in project.clj:

:test-selectors {:default (complement :integration)
                 :integration :integration
                 :all (constantly true)}

Arguments to this task will be considered test selectors if they are keywords, otherwise arguments must be test namespaces or files to run. With no arguments the :default test selector is used if present, otherwise all tests are run. Test selector arguments must come after the list of namespaces.

A default :only test-selector is available to run select tests. For example, lein test :only leiningen.test.test/test-default-selector only runs the specified test. A default :all test-selector is available to run all tests.

Arguments: ([& tests])


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接