Skip to content

Should catch unexpected exceptions and fail the test suite #59

@metametadata

Description

@metametadata

Example:

(ns unit.a.b
  (:require 
    [clojure.test.check.generators :as gen]
    [com.gfredericks.test.chuck.clojure-test :as chuck]
    [clojure.test :as ct :refer :all]))

(deftest foo
  (chuck/checking
    "bar"
    10
    [x gen/int]
    (throw (ex-info "oops" {}))))

Expected:

<somehow report the exception with stacktrace>
...
0 failures, 1 errors.
Tests failed.

Actual:

Tests failed, smallest case: [{x 0}] 
Seed 1534810136116

Ran 1 tests containing 0 assertions.
0 failures, 0 errors.

My current workaround is to redefine the reporting method so that it always "fails" the test via (is false):

(defmethod ct/report ::chuck/shrunk
  [m]
  (is false "Property test failed, see logs for more info")
  (println
    ;"\nCase:\n" (with-out-str (pp/pprint (:fail m)))
    "\nShrunk:\n" (with-out-str (pp/pprint (-> m :shrunk :smallest)))
    (str "\n(num-tests: " (:num-tests m)
         ", total-nodes-visited: " (-> m :shrunk :total-nodes-visited)
         ", depth: " (-> m :shrunk :depth)
         ", failing-size: " (:failing-size m)
         ", seed: " (:seed m) ")"))
  (println "Error:" (pr-str (-> m :result-data :clojure.test.check.properties/error))))

Output:

FAIL in (foo) (b.clj:21)
bar
Property test failed, see logs for more info
expected: false
  actual: false

Shrunk:
 [{x 0}]
 
(num-tests: 1, total-nodes-visited: 0, depth: 0, failing-size: 0, seed: 1534810550729)
Error: #error {
 :cause "oops"
 :data {}
 :via
 [{:type clojure.lang.ExceptionInfo
   :message "oops"
   :data {}
   :at [clojure.core$ex_info invokeStatic "core.clj" 4739]}]
 :trace
 [[clojure.core$ex_info invokeStatic "core.clj" 4739]
  [clojure.core$ex_info invoke "core.clj" 4739]
  ...]}

Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Tests failed.

Workaround output for the expected failure:

(deftest foo
  (chuck/checking
    "bar"
    1000
    [x gen/int]
    (is (not= 5 x))))
lein test :only unit.a.b/foo

FAIL in (foo) (b.clj:21)
bar
Property test failed, see logs for more info
expected: false
  actual: false

Shrunk:
 [{x 5}]
 
(num-tests: 114, total-nodes-visited: 3, depth: 0, failing-size: 113, seed: 1534810835278)
Error: nil

lein test :only unit.a.b/foo

FAIL in (foo) (b.clj:179)
bar
expected: (not= 5 x)
  actual: (not (not= 5 5))

Ran 1 tests containing 2 assertions.
2 failures, 0 errors.
Tests failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions