Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The 'right' way to run unit tests in Clojure

Currently, I define the following function in the REPL at the start of a coding session:

(defn rt []
  (let [tns 'my.namespace-test]
    (use tns :reload-all)
    (cojure.test/test-ns tns)))

And everytime I make a change I rerun the tests:

user=>(rt)

That been working moderately well for me. When I remove a test, I have to restart the REPL and redefine the method which is a little annoying. Also I've heard bad rumblings about using the use function like this. So my questions are:

  • Is using use this way going to cause me a problem down the line?
  • Is there a more idiomatic workflow than what I'm currently doing?
like image 614
Garrett Rowe Avatar asked Jul 26 '13 22:07

Garrett Rowe


People also ask

How do you run a Clojure test?

Running Tests You can run tests by using the cljs. test/run-tests macro. This may be done in your REPL or at the end of your file. If you have many test namespaces it's idiomatic to create a test runner namespace which imports all of your test namespaces and then invokes run-tests .

How do you run unit testing?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).

Should unit tests be run in parallel?

Running unit tests in parallel can significantly improve the speed at which they run. However, you have to make sure that one test does not affect another in any way. Else your tests are green most of the time, but sometimes one or more tests will fail.

Which one is used to run the unit tests?

Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases.


1 Answers

most people run

lein test

form a different terminal. Which guarantees that what is in the files is what is tested not what is in your memory. Using reload-all can lead to false passes if you have changed a function name and are still calling the old name somewhere.

  • calling use like that is not a problem in it's self, it just constrains you to not have any name conflicts if you use more namespaces in your tests. So long as you have one, it's ok.
  • using lein lets you specify unit and integration tests and easily run them in groups using the test-selectors feature.
like image 167
Arthur Ulfeldt Avatar answered Sep 25 '22 08:09

Arthur Ulfeldt