15

I'm creating a library that includes both Clojure and Java code, and would like to be able to run a single test JUnit suite across the entire code base. Seems that this should be possible since they are both running on the JVM and there is good Clojure/Java interop.

However, currently Clojure code seems to favour unit testing done through the clojure.test API using "(run-all-tests)" and friends.

Is there a good way to wrap these Clojure tests in some sort of wrapper so that they can be automatically called from JUnit?

(when I say automatically, I mean without resorting to the manual solution of writing a JUnit test case to wrap and call each Clojure test case individually :-) )

2
  • 2
    i'd probably lean toward maven on leiningen Commented Jan 31, 2011 at 1:36
  • 2
    you can easily setup maven for this.. start with this question.. Commented Jan 31, 2011 at 11:59

2 Answers 2

3

The easiest thing to do would be to call run-all-tests from a single JUnit test. You could capture the output and look at the last line that indicates the pass/fail/error count. If you see a fail or an error you can make the JUnit test fail.

However, if you want interaction with each Clojure test you'll have to implement similar functionality to what's found in clojure.test. That is, for each value in the each namespace look for a function stored in the :test meta-data value. That value is your test function.

Lines 661-686 of test.clj give a good synopsis of how tests are stored and later run in Clojure.

Sign up to request clarification or add additional context in comments.

Comments

2

Currently there exists no JUnit integration I know of. Besides calling run-all-tests from a single JUnit test as suggested by @psyllo you can build more thorough integration as follows:

  1. Build a function that generates an instance of junit.framework.Test for every test method defined in clojure. Have a look at clojure.test/test-ns on how to find all test methods.
  2. Make an instance of junit.framework.TestSuite which adds all those generated Tests for a certain ns and AOT compile this suite class.
  3. You can call the suite class from java / junit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.