From 316a49d2ec3367d77e06f64fa39a0fe4133c14c9 Mon Sep 17 00:00:00 2001 From: Noah Bogart Date: Fri, 22 Mar 2024 11:08:12 -0400 Subject: [PATCH 1/3] Demonstrate a once fixture that calls f twice --- .../d-tests/ddd/double_once_fixture_test.clj | 17 +++++++ test/unit/kaocha/fixtures_test.clj | 45 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 fixtures/d-tests/ddd/double_once_fixture_test.clj create mode 100644 test/unit/kaocha/fixtures_test.clj diff --git a/fixtures/d-tests/ddd/double_once_fixture_test.clj b/fixtures/d-tests/ddd/double_once_fixture_test.clj new file mode 100644 index 00000000..ba0795be --- /dev/null +++ b/fixtures/d-tests/ddd/double_once_fixture_test.clj @@ -0,0 +1,17 @@ +(ns ddd.double-once-fixture-test + (:require [clojure.test :refer [deftest is use-fixtures]])) + +(def ^:dynamic *val* nil) + +(defn doubled-fixture [f] + (binding [*val* :one] + (f)) + (binding [*val* :two] + (f))) + +(use-fixtures :once doubled-fixture) + +(deftest example-fail-test + (if (= :one *val*) + (is (= 1 2)) + (is (= 2 2)))) diff --git a/test/unit/kaocha/fixtures_test.clj b/test/unit/kaocha/fixtures_test.clj new file mode 100644 index 00000000..e486e5b7 --- /dev/null +++ b/test/unit/kaocha/fixtures_test.clj @@ -0,0 +1,45 @@ +(ns kaocha.fixtures-test + (:refer-clojure :exclude [symbol]) + (:require [clojure.test :as t :refer [testing is deftest]] + [kaocha.test-factories :as f] + [kaocha.testable :as testable] + [kaocha.classpath :as classpath] + [kaocha.test-helper] + [kaocha.core-ext :refer :all] + [kaocha.test-util :refer [with-test-ctx]] + [kaocha.type.var] + [matcher-combinators.test :refer [match?]])) + +(deftest once-fixtures-test + (classpath/add-classpath "fixtures/d-tests") + (testing "once fixture calling f twice" + (require 'ddd.double-once-fixture-test) + (let [{:keys [result report]} + (with-test-ctx {:fail-fast? false} + (testable/run {:kaocha.testable/type :kaocha.type/var + :kaocha.testable/id :ddd.double-once-fixture-test/example-fail-test + :kaocha.testable/desc "example-fail-test" + :kaocha.var/name 'ddd.double-once-fixture-test/example-fail-test + :kaocha.var/var (resolve 'ddd.double-once-fixture-test/example-fail-test) + :kaocha.var/test (-> (resolve 'ddd.double-once-fixture-test/example-fail-test) meta :test)} + (f/test-plan {})))] + + (is (match? {:kaocha.testable/type :kaocha.type/var + :kaocha.testable/id :ddd.double-once-fixture-test/example-fail-test + :kaocha.testable/desc "example-fail-test" + :kaocha.var/name 'ddd.double-once-fixture-test/example-fail-test + :kaocha.var/var (resolve 'ddd.double-once-fixture-test/example-fail-test) + :kaocha.var/test fn? + :kaocha.result/count 1 + :kaocha.result/pass 0 + :kaocha.result/error 0 + :kaocha.result/fail 1} + result)) + + (is (match? [{:type :begin-test-var} + {:type :pass + :expected '(= 1 2) + :actual '(not (= 1 2)) + :message nil} + {:type :end-test-var}] + report))))) From 6773b7ca0c3f5dbb8cd2f856dc7b7ae2f0d31418 Mon Sep 17 00:00:00 2001 From: Noah Bogart Date: Fri, 22 Mar 2024 12:36:09 -0400 Subject: [PATCH 2/3] Implement fix --- src/kaocha/type/ns.clj | 6 ++- test/unit/kaocha/fixtures_test.clj | 60 ++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/kaocha/type/ns.clj b/src/kaocha/type/ns.clj index 470489c9..ee2578e3 100644 --- a/src/kaocha/type/ns.clj +++ b/src/kaocha/type/ns.clj @@ -16,8 +16,10 @@ (defn run-tests [testable test-plan fixture-fn] ;; It's not guaranteed the the fixture-fn returns the result of calling the ;; tests function, so we need to put it in a box for reference. - (let [result (atom (:kaocha.test-plan/tests testable))] - (fixture-fn #(swap! result testable/run-testables test-plan)) + (let [testables (:kaocha.test-plan/tests testable) + result (atom [])] + (fixture-fn #(let [test-result (testable/run-testables testables test-plan)] + (swap! result into test-result))) @result)) (defmethod testable/-load :kaocha.type/ns [testable] diff --git a/test/unit/kaocha/fixtures_test.clj b/test/unit/kaocha/fixtures_test.clj index e486e5b7..9e8b89ef 100644 --- a/test/unit/kaocha/fixtures_test.clj +++ b/test/unit/kaocha/fixtures_test.clj @@ -16,30 +16,50 @@ (require 'ddd.double-once-fixture-test) (let [{:keys [result report]} (with-test-ctx {:fail-fast? false} - (testable/run {:kaocha.testable/type :kaocha.type/var - :kaocha.testable/id :ddd.double-once-fixture-test/example-fail-test - :kaocha.testable/desc "example-fail-test" - :kaocha.var/name 'ddd.double-once-fixture-test/example-fail-test - :kaocha.var/var (resolve 'ddd.double-once-fixture-test/example-fail-test) - :kaocha.var/test (-> (resolve 'ddd.double-once-fixture-test/example-fail-test) meta :test)} + (testable/run (testable/load {:kaocha.testable/type :kaocha.type/ns + :kaocha.testable/id :ddd.double-once-fixture-test + :kaocha.testable/desc "ddd.double-once-fixture-test" + :kaocha.ns/name 'ddd.double-once-fixture-test}) (f/test-plan {})))] - (is (match? {:kaocha.testable/type :kaocha.type/var - :kaocha.testable/id :ddd.double-once-fixture-test/example-fail-test - :kaocha.testable/desc "example-fail-test" - :kaocha.var/name 'ddd.double-once-fixture-test/example-fail-test - :kaocha.var/var (resolve 'ddd.double-once-fixture-test/example-fail-test) - :kaocha.var/test fn? - :kaocha.result/count 1 - :kaocha.result/pass 0 - :kaocha.result/error 0 - :kaocha.result/fail 1} + (is (match? {:kaocha.testable/type :kaocha.type/ns + :kaocha.testable/id :ddd.double-once-fixture-test + :kaocha.testable/desc "ddd.double-once-fixture-test" + :kaocha.result/tests + [{:kaocha.testable/type :kaocha.type/var + :kaocha.testable/id :ddd.double-once-fixture-test/example-fail-test + :kaocha.testable/desc "example-fail-test" + :kaocha.var/name 'ddd.double-once-fixture-test/example-fail-test + :kaocha.var/var (resolve 'ddd.double-once-fixture-test/example-fail-test) + :kaocha.var/test fn? + :kaocha.result/count 1 + :kaocha.result/pass 0 + :kaocha.result/error 0 + :kaocha.result/fail 1} + {:kaocha.testable/type :kaocha.type/var + :kaocha.testable/id :ddd.double-once-fixture-test/example-fail-test + :kaocha.testable/desc "example-fail-test" + :kaocha.var/name 'ddd.double-once-fixture-test/example-fail-test + :kaocha.var/var (resolve 'ddd.double-once-fixture-test/example-fail-test) + :kaocha.var/test fn? + :kaocha.result/count 1 + :kaocha.result/pass 1 + :kaocha.result/error 0 + :kaocha.result/fail 0}]} result)) - (is (match? [{:type :begin-test-var} - {:type :pass + (is (match? [{:type :begin-test-ns} + {:type :begin-test-var} + {:type :fail :expected '(= 1 2) :actual '(not (= 1 2)) :message nil} - {:type :end-test-var}] - report))))) + {:type :end-test-var} + {:type :begin-test-var} + {:type :pass + :expected '(= 2 2) + :actual (list = 2 2) + :message nil} + {:type :end-test-var} + {:type :end-test-ns}] + (mapv #(select-keys % [:type :expected :actual :message]) report)))))) From a124899c15f77cef529c3803111d083d668f7846 Mon Sep 17 00:00:00 2001 From: Noah Bogart Date: Sat, 23 Mar 2024 08:01:53 -0400 Subject: [PATCH 3/3] Add changelog entry --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a304794..7b38ad7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ ## Fixed +- Track all results when `:once` fixtures call `f` multiple times. + (thanks [@NoahTheDuke](https://github.com/NoahTheDuke)) + ## Changed # 1.87.1366 (2023-09-27 / f514905) @@ -1075,4 +1078,4 @@ namespace. - The configuration format has changed, you should now start with the `#kaocha {}` tagged reader literal in `tests.edn` to provide defaults. If you want more control then overwrite `tests.edn` with the output of `--print-config` and - tweak. \ No newline at end of file + tweak.