简体   繁体   中英

Lein test with Selectors — how to specify a test for multiple conditions?

I have some tests which need to run using a connection to either the local MySQL database or the remote MySQL database.

I can choose which to run using something like this; the (sql/with-conn...) stuff sets up a dynamic binding to make the proper connection, and all subsequent calls just use the binding.

(sql/with-conn (sql/conn :remote)
    (test-one)  ;; will use remote db
    (test-two)) ;; will use remote db

Similarly,

(sql/with-conn (sql/conn :local)
    (test-one)  ;; will use local db
    (test-two)) ;; will use local db

I'd like to choose the binding from the command line by using selectors, as that appears to be the proper approach. I tried adding metadata, so I could say lein test :local (after adding :selectors {:local :local} to my project.clj :

(deftest ^:local test-one [] ...)
(deftest ^:local test-two [] ...)

This works, but now I can't do lein test :remote .

I haven't been able to figure out how lein uses the value of the selector map, such that I can select a test with, say, :local or :remote , but not when the argument is :cache , which is intended to run another set of tests.

I tried adding tags to two different high level tests:

(deftest ^:local test-local []
    (sql/with-conn (sql/conn :local)
        (test-one)
        (test-two)))

(deftest ^:remote test-remote []
    (sql/with-conn (sql/conn :remote)
        (test-one)
        (test-two)))

But lein test :local and lein test :remote only calls the high level test and ignores test-one and test-two, I believe because the lower level tests in this case aren't tagged.

I tried using :only to call a high level test without metadata, and not using custom selectors, but again the lower level tests are not called.

What is odd as that when run from the Emacs Cider REPL, the high level tests do in fact run the low level tests.

So how do I run these tests from Lein, with essentially different arguments?

Your last example is on the right track:

(deftest ^:local test-local []
    (sql/with-conn (sql/conn :local)
        (test-one)
        (test-two)))

(deftest ^:remote test-remote []
    (sql/with-conn (sql/conn :remote)
        (test-one)
        (test-two)))

The missing piece is that test-one and test-two need to be regular functions created with defn , not tests created with deftest .


Update

Test selectors add Clojure metadata to the test function. It is intended primarily to categorize functions as :fast vs :slow , for example. Test selectors are not intended to parameterize the test functions, however, which is what you want to do.

Your solution with profiles is more aligned with the intended usage of lein . Note that profiles can be stored in more than one location (see the lein profile docs for full information).

Profiles that are intended to stay with the project in SCM are added to project.clj under the :profiles key. Profile info that should not be committed into SCM (such as cloud or db usernames & passwords) can be kept in profiles.clj that lives alongside project.clj but is user-specific and is NOT checked into SCM.

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