unit testing - combining test.check and clojure test

Keywords:unit  testing 


I am having trouble to use test.check together with normal tests. I tried the whole day to figure out what is going on, but I am still not sure. This is what I have now:

(deftest user-can-only-be-inserted-once
  (;normal test))

(defspec insert-one-user-should-let-me-retrieve-that-one-user
         (prop/for-all ; test.check test))

Now, as long as I execute "lein test" from the console after each change in the test file it works as expected. However, running tests from the cursive repl or with the quickie plugin via "lein quickie" the repl will reload the namespaces not correct.

The effect is that, whenever a test fails and I fix that test it will still fail in the repl, so it looks like something is not working correctly there.

If anyone has an idea how to fix this, I'd be happy to hear it. I would also like to hear if someone has a different setup which combines normal tests and test.check or if this is not intended to work at all.

Thanks, Sven

Update Actually I found out that the problem is not the combination of normal and test.check but the fact that I am trying to check an assertion is throw on a specific case. In normal test you would do this with (is (thrown? Assertion (function... But when I try this in test.check it just fails.

1 Answer: 

If I understand your update correctly, your test expects that the exception is thrown. If the exception does not occur, then the test should fail.

If so, then you will need to write the property in defspec to catch the exception, return it as a value, and verify that the exception occurs.

You could do something like this:

(defn catcher [f]
  (try (f)
   (catch Throwable t t)))

(defspec some-specification-name
  (prop/for-all [...generator-bindings...]
    (not-nil? (catcher (my-function generated-values)))))