cognitect-labs / test-runner

A test runner for clojure.test
Eclipse Public License 2.0
287 stars 31 forks source link

Tests are run in `user` namespace #38

Closed raystubbs closed 3 years ago

raystubbs commented 3 years ago

Using clj -X:test seems to run the tests in the user namespace, which breaks tests that rely on *ns*. Here's a repro.

(ns example.core-test
  (:require
    [clojure.test :refer [deftest is]]))

(deftest example-test
  (is (= (str *ns*) "example.core-test")))

Result:

FAIL in (example-test) (core_test.cljc:24)
expected: (= (str *ns*) "example.core-test")
  actual: (not (= "user" "example.core-test"))
puredanger commented 3 years ago

Do you have an example closer to what actually fails due to this?

raystubbs commented 3 years ago

Yeah, this test fails since the defclass macro bases class names on the current ns.

puredanger commented 3 years ago

I don't think there is any good reason to have this expectation. clojure.test tests can be run from any namespace and often are run from namespaces other than the place where they are defined.

If you want to enforce this constraint, you can do so via a :once fixture:

(defn ns-fixture
  [f]
  (in-ns 'example.core-test)
  (f))

(use-fixtures :once ns-fixture)
raystubbs commented 3 years ago

Hmm, okay. Seems odd that you can't depend on the namespace; but makes some sense if that's the convention.