Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a 'slow' test suite with clojure.test

I want this test to run with every 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)))

I want to make ackermann-slow-test only run when I ask for it:

(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)))

The full code is available on Github: https://github.com/bluemont/ackermann

like image 798
David J. Avatar asked Apr 11 '14 16:04

David J.


1 Answers

According to Making Leiningen work for You by Phil Hagelberg, the test-selectors feature was added to Leiningen in version 1.4.

Two easy steps. First, add this to project.clj:

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

Second, mark up your test with metadata:

(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)))

Now you have three options for running your tests:

⚡ lein test
⚡ lein test :slow
⚡ lein test :all

Also, this information is easy to find with 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])

like image 139
David J. Avatar answered Nov 08 '22 20:11

David J.