kaocha icon indicating copy to clipboard operation
kaocha copied to clipboard

Ability to disable ::zero-assertions as fail condition

Open jaybarra opened this issue 3 years ago • 7 comments

I would like the ability to disable via configuration the zero-assertions condition as a failing condition. There are several tests where the is has been moved to a separate namespace function call which are being falsely flagged as having no conditions.

jaybarra avatar Oct 09 '20 13:10 jaybarra

Generally I would recommend writing your test in such a way that there's always an assertion. That said making this configurable doesn't hurt, PR welcome.

plexus avatar Oct 12 '20 05:10 plexus

We are running some end-to-end UI tests and prefer to allow our UI driver library to throw assertions when things aren't there. This gives us fail fast (which we want in this scenario) for free, but means that most tests do not need a clojure.test assertion.

Would a PR still be welcome?

(At the moment I'm hacking the kaocha.hierarchy, but I'm not proud of what I've done.)

ourkwest avatar Mar 25 '22 15:03 ourkwest

Yes, a PR would still be most welcome. Thanks!

plexus avatar Mar 28 '22 06:03 plexus

Having looked at the code it seems that kaocha's hook mechanism already makes this reasonably straightforward. Due to shortness of time I've used that instead. If anyone else comes here looking to disable the fail on zero assertions behaviour here's what I did:

(ns my.kaocha.hooks
  (:require
    [kaocha.hierarchy :as hierarchy]))

(hierarchy/derive! ::ignore :kaocha/known-key)

(defn defuse-zero-assertions [event]
  (if (= (:type event) :kaocha.type.var/zero-assertions)
    (assoc event :type ::ignore)
    event))

I put that code in a namespace and configured it as a hook in my tests.edn file:

#kaocha/v1
{:plugins [:kaocha.plugin/hooks]
 :kaocha.hooks/pre-report [my.kaocha.hooks/defuse-zero-assertions]}

edited: to change hook type.

ourkwest avatar Apr 06 '22 16:04 ourkwest

I'm surprised that actually works. Are you sure you used a pre-test hook and not a pre-report hook?

plexus avatar Apr 07 '22 09:04 plexus

I'm surprised that actually works. Are you sure you used a pre-test hook and not a pre-report hook?

I re-run the example above, and I found:

(1) If I run the example precisely, I will get

Execution error (ArityException) at kaocha.plugin.hooks/hooks-pre-test-hook$fn (hooks.clj:95).
Wrong number of args (2) passed to: my.kaocha.hooks/defuse-zero-assertions

(2) If I run the example with changing :kaocha.hooks/pre-test to :kaocha.hooks/pre-report, then it works.

The test below no longer throws Failure like Test ran without assertions. Did you forget an (is ...)?

(deftest example-test
  (= 2 2))

Should I add this hook using example into the doc part of kaocha project? How about put it in the section of 10_hooks.md. There is a Pre-report explanation section.

humorless avatar Mar 09 '23 07:03 humorless

I'm surprised that actually works. Are you sure you used a pre-test hook and not a pre-report hook?

You are correct. My mistake. It appears that we are using a pre-report hook.

ourkwest avatar Mar 09 '23 08:03 ourkwest