Skip to content

Commit

Permalink
Add profile reporter test
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahTheDuke committed Oct 14, 2024
1 parent 2b3d4f2 commit e1f86e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
6 changes: 4 additions & 2 deletions src/clojure/lazytest/reporters.clj
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,11 @@
(count slowest-vars)
(double (/ var-duration 1e9))
(double (* (/ var-duration total-duration) 100))))
(println (->> (for [suite slowest-vars]
(println (->> (for [suite slowest-vars
:let [ns (some-> suite :source :var symbol namespace)
id (str ns (when ns "/") (s/identifier suite))]]
(format " %s %.5f seconds"
(s/identifier suite)
id
(double (/ (:lazytest.runner/duration suite) 1e9))))
(str/join \newline)))
(flush)))
Expand Down
8 changes: 4 additions & 4 deletions src/clojure/lazytest/runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
(let [id (:doc suite)
start (System/nanoTime)
config (-> config
(update :lazytest.runner/depth #(if id (inc %) %))
(update :lazytest.runner/suite-history conj suite))
(update ::depth #(if id (inc %) %))
(update ::suite-history conj suite))
f (if-let [around-fn (combine-arounds suite)]
#(let [ret (volatile! nil)
tests (propagate-eachs suite %)]
Expand All @@ -34,8 +34,8 @@
results (vec (keep f (:children suite)))
duration (double (- (System/nanoTime) start))]
(-> (suite-result suite results)
(assoc :lazytest.runner/source-type source-type)
(assoc :lazytest.runner/duration duration))))
(assoc ::source-type source-type)
(assoc ::duration duration))))

(defmethod run-tree :lazytest/run
run-test--lazytest-run
Expand Down
58 changes: 34 additions & 24 deletions test/clojure/lazytest/reporters_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
[lazytest.core :refer [->ex-failed defdescribe describe expect expect-it it]]
[lazytest.reporters :as sut]
[lazytest.runner :as runner]
[lazytest.suite :as s]
[lazytest.suite :as s :refer [suite]]
[lazytest.test-case :as tc]
[lazytest.test-utils :refer [with-out-str-no-color]]))
[lazytest.test-utils :refer [with-out-str-no-color]]
[lazytest.extensions.matcher-combinators :refer [match?]])
(:import
[java.util.regex Pattern]))

(set! *warn-on-reflection* true)

Expand All @@ -31,37 +34,29 @@
:children results})

(defn ->passing [& {:as extra}]
(-> (tc/test-case-result
:pass
(runner/prep-test-case
(tc/test-case {:doc "example test-case"
:body (fn [])})))
(-> (tc/test-case {:doc "example test-case"
:body (fn [] true)})
(runner/run-tree (->config {:reporter [(constantly nil)]}))
(merge extra)))

(defn ->failing [& {:as extra}]
(let [data (merge {:file "example.clj"
:line 1
:message "failing"
:evaluated (list = 1 2)
:actual false}
extra)
thrown (->ex-failed '() (= 1 2) data)]
(tc/test-case-result
:fail (runner/prep-test-case
(tc/test-case {:doc "example test-case"
:body (fn [])}))
thrown)))
(-> (tc/test-case {:doc "example test-case"
:body (fn [] (expect (= 1 2) (if (contains? extra :message)
(:message extra)
"failing")))})
(runner/run-tree (->config {:reporter [(constantly nil)]}))
(assoc :file "example.clj")
(assoc :line 1)
(merge (dissoc extra :message))))

(defn ->erroring [& _]
(let [thrown (ex-info "deliberate error"
{:ex :info
:file "example.clj"
:line 1})]
(tc/test-case-result
:fail (runner/prep-test-case
(tc/test-case {:doc "example test-case"
:body (fn [])}))
thrown)))
(-> (tc/test-case {:doc "example test-case"
:body (fn [] (throw thrown))})
(runner/run-tree (->config {:reporter [(constantly nil)]})))))

(defn stub-stack-trace
[_ex]
Expand Down Expand Up @@ -345,6 +340,21 @@
(expect
(= (* 2 depth) (- (count out) (count (str/triml out)))))))))

(defdescribe profile-test
(it "prints the right times"
(let [test-suite (-> (describe "suite"
(it "it" (expect (= 2 1))))
(assoc :type :lazytest/var :var #'profile-test))
results (runner/run-test-suite
(suite {:type :lazytest/ns
:nses [*ns*]
:children [test-suite]})
(->config {:reporter (constantly nil)}))
suite-results (assoc test-suite :type :end-test-run :results results)]
(expect (match? (Pattern/compile "Top 1 slowest test namespaces.*Top 1 slowest test vars" Pattern/DOTALL)
(-> (sut/profile nil suite-results)
(with-out-str-no-color)))))))

(defdescribe defdescribe-no-doc nil (it "works" nil))
(defdescribe defdescribe-with-doc "cool docs" (it "works" nil))

Expand Down

0 comments on commit e1f86e9

Please sign in to comment.