Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you "parameterize" Clojure Contrib's test-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.

like image 444
Robert Campbell Avatar asked Dec 07 '25 10:12

Robert Campbell


1 Answers

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.

like image 57
Arthur Ulfeldt Avatar answered Dec 10 '25 16:12

Arthur Ulfeldt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!