How can you "parameterize" the Clojure Contrib test-is?

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

How can you write data driven tests using the test-is library ? I tried to use understanding for iterating over a collection of input parameters, but since deftest is a macro waiting.

+3
source share
2 answers

From reading an article on parametrized tests in Junit, it seems that after you go past the poiler plate, the cool part of parameterization is that it allows you to enter this:

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

and it’s easy to identify four tests.

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

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

If you want to specify your inputs as a map, you can run something like:

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

although a macro arewill result in easier reading of the output.

+7
source

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

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 can rewrite deftestit run-testsyourself. It could be a dozen Clojure strings so that tests can take parameters in some other way.

+2

Source: https://habr.com/ru/post/1725063/


All Articles