pitalig
pitalig

Reputation: 177

How to run tests before start ring server

I'm developing an clojure API and I want to everytime I start my server, run the tests before it.

I've found the function (run-tests) and (run-all-tests) but clojure says that this function does not exist at that namespace. If I put (:use clojure.test) at my ns, the function works, but it runs tests from libs that I'm using and from clojure core.

Here is my handler, where I placed the main method, then I want that "lein run" runs my tests then start the server.

(ns clojure-api.handler
  (:use compojure.core)
  (:require [compojure.route :as route]
            [org.httpkit.server :refer [run-server]]))

(defroutes app-routes 
    ;routes...
)

(def app
  (-> app-routes
      ;Some middleware))

(defn -main  
  [& args]
  (println "Running bank tests")
  (run-all-tests)
  (println "Starting server")
  (run-server app {:port 3000}))

Upvotes: 3

Views: 211

Answers (2)

Minh Tuan Nguyen
Minh Tuan Nguyen

Reputation: 1054

if your project is a leiningen project, you can define an alias in the project.clj that combine the test und run tasks like this:

:aliases {"test-and-run" ["do" "test" "run"]}

;; .lein.sh test-and-run

Upvotes: 1

user2609980
user2609980

Reputation: 10484

You can specify for what namespaces to run tests via (run-tests 'ns-one-test 'ns-two-test).

(defn -main  
  [& args]
  (println "Running bank tests")
  (run-tests 'ns-one-test 'ns-two-test)
  (println "Starting server")
  (run-server app {:port 3000}))

This would work and only run the tests in the specified namespaces.

As the comments state, tests are usually run in a setup before a deploy (by using for example Jenkins, GitLab or CircleCI). When a test fails the next step is not executed, a report is shown somewhere, and the code is not deployed.

Upvotes: 1

Related Questions