简体   繁体   中英

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}))

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM