testing

Testing tools, testing philosophy & methodology...
hoynk 2020-11-20T17:54:03.012900Z

Is it possible attach a fixture only to a few tests within a namespace?

seancorfield 2020-11-20T17:58:36.014Z

@boccato Not directly, but you could add metadata on those tests and check for the metadata in the fixture. I think. Not sure if the fixture is passed the Var or just the function value.

seancorfield 2020-11-20T18:02:05.014500Z

Hmm, nope. The fixture is passed the test function, not the Var. That's a shame.

seancorfield 2020-11-20T18:03:53.015200Z

Not sure what to suggest. I guess I'd probably break those tests out into a separate namespace and add the special fixture to that new namespace...

Jivago Alves 2020-11-20T18:13:52.018Z

@boccato I’m not sure if it helps but in our projects we defined a macro to clean up the test’s fixture from our DB. Maybe you could use the macro when you need some additional stuff done for the test.

(defonce ^:dynamic *db*
  (-> (db-config-for :test)
      new-db
      component/start
      (assoc :enable? false)))

(defn clean
  [f]
  (jdbc/with-db-transaction [db *db*]
    (jdbc/db-set-rollback-only! db)
    (binding  [*db* db]
      (try
        (f)
        (catch SQLException e
          (prn (.getNextException e))
          (throw e))))))

(defmacro def-integration-test
  [test-name & body]
  `(deftest ~(vary-meta test-name assoc :integration true)
     (clean (fn [] ~@body))))
Hope it’s helpful.

hoynk 2020-11-20T18:15:08.018100Z

I m leaning towards separating them in namespaces. Thanks for the reply.

Jivago Alves 2020-11-20T18:17:55.018300Z

This is just an example. We use the dynamic *db* in our tests to make it easier to CRUD the db without worrying about cleaning up the DB in the context of the test. The :integration keyword is useful to filter the tests and run them in parallel in our pipeline.

hoynk 2020-11-20T18:18:40.018500Z

Definetly helps to see how ppl tackle similar problems... liked the idea of creating a diferent kind of deftest but in my case might be overkill.

Jivago Alves 2020-11-20T18:19:17.018700Z

So in the same namespace, you might find deftest for pure fns and def-integration-test for fns with side effects. For us, it’s very easy to spot the difference.

hoynk 2020-11-20T18:19:40.018900Z

Yeah... I liked the approach.

hoynk 2020-11-20T18:19:49.019100Z

Will keep your code, it might be handy in the future.

Jivago Alves 2020-11-20T18:20:18.019300Z

Of course, we reached that code after trying simpler options. It’s only useful when you have the need.