Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:test-selectors doesn't suppress clojure.test fixtures #1269

Closed
gfredericks opened this issue Jul 31, 2013 · 14 comments · Fixed by #1271
Closed

:test-selectors doesn't suppress clojure.test fixtures #1269

gfredericks opened this issue Jul 31, 2013 · 14 comments · Fixed by #1271

Comments

@gfredericks
Copy link
Collaborator

The implementation of :test-selectors hooks clojure.test/test-var, which is responsible for running just the tests, not the fixtures.

This means that, for example, if I have an :each fixture around a set of five tests that are all excluded by my :test-selectors, the fixture will still run five times.

Given that one use case for :test-selectors is to exclude tests that require some unavailable external resource, and a use case for fixtures is for managing external resources, this is rather inconvenient.

One fix for just the :each fixtures is to stop hooking test-var and instead hook test-all-vars and for each var in the namespace remove the :test metadata when the :test-selector requires it.

This doesn't address :once fixtures though, which seems much trickier.

@gfredericks
Copy link
Collaborator Author

Seems to be related to http://dev.clojure.org/jira/browse/CLJ-866 which would provide a nicer hook.

@gfredericks
Copy link
Collaborator Author

Given CLJ-866 will likely not be available quickly, is the removal of :test metadata an acceptable option? I'd be happy to code it up if so.

@technomancy
Copy link
Owner

Yeah, removing :test metadata instead of using hooke would be great. Even better if you can save the metadata and add it back in with a try/finally so it'll still work with mechanisms that re-use a JVM. Thanks!

@technomancy
Copy link
Owner

You shouldn't need to hook test-all-vars though; just removing and re-adding the metadata in Leiningen's own code surrounding the call to clojure.test/run-tests would do the trick.

gfredericks added a commit to gfredericks/leiningen that referenced this issue Aug 1, 2013
The old method skipped tests by adding a hook to
clojure.test/test-var, the problem being that :each fixtures
associated with the test have already run at that point, which is
unideal.

This change skips test by removing their :test metadata before running
the tests at all, which causes clojure.test to not see it as a test.

We use ns-interns to enumerate the vars, the same way clojure.test
does.
@gfredericks gfredericks mentioned this issue Aug 1, 2013
ajoberstar added a commit to clojurephant/jovial that referenced this issue Jun 12, 2016
As noted in a leiningen issue[1], overriding test-var isn't an
appropriate filtering approach, as it doesn't stop fixtures from
running for the filtered-out tests. They pointed to a Clojure
issue[2] that added the test-vars fn, which runs the fixtures
only for the vars you pass in. However, they indicated later in
that thread that while they waited for that issue to be resolved
(which it is now), they would just remove the :test metadata from
the vars. I haven't looked to confirm which approach they took,
so not sure if I'm being consistent. The only drawback seems to
be the lack of support fo the :test-ns-hook feature that allows
you to control order. This doesn't seem like that great of a
feature anyway, so I'm not too bothered.

[1] technomancy/leiningen#1269
[2] http://dev.clojure.org/jira/browse/CLJ-866
@ehashman
Copy link
Contributor

ehashman commented Jul 5, 2017

Drive-by comment: I just ran into the :once fixture issue on Leiningen 2.7.1. It looks like CLJ-866 has been merged since this was closed. Is this something we're interested in revisiting?

@truemped
Copy link

Another drive-by comment. Also ran into exactly this today. Any possibilities or ideas to deal with this situation? I also have one clojure test file that only contains integration tests. Would be nice to not have it spin up dependencies...

@codeasone
Copy link

+1

I'm on 2.7.1 and have failing unit tests due to :once fixtures for ^:slow tests being invoked on lein test as well as lein test :slow.

@technomancy
Copy link
Owner

This implementation is a mess; due to the fact that clojure.test doesn't accept patches we have to do it all with monkeypatches, and the result is fairly problematic and unmaintainable.

I would recommend moving to circleci.test going forward if you can instead of relying on a combination of the unmaintained clojure.test library and the pile of hacks that lives in Leiningen. It should be easy to switch over to because it allows you to continue using your existing clojure.test/deftest calls but just provides a more flexible mechanism for running them.

@codeasone
Copy link

Thanks for the advice @technomancy. I'll start using circleci.test.

@codeasone
Copy link

codeasone commented Aug 10, 2017

So, I've used circleci.test for a bit. I like the isolatation/enforce fixtures.

However it doesn't appear to limit :once fixture invocation based on test-selectors. In my case I have a db_test.clj file with some deftest ^:integration expressions, and a (use-fixtures :once with-data).

The fixtures are setup regardless of whether I lein test :integration or lein test :unit. Am I missing something, or is this simply not supported functionality and I should look at coding it up?

@technomancy
Copy link
Owner

technomancy commented Aug 10, 2017 via email

@logosity
Copy link

@technomancy Just stumbled onto this thread for more-or-less the same reason as the others - can you point to more about (or elaborate on) clojure.test being unmaintained? Thanks!

@technomancy
Copy link
Owner

technomancy commented Aug 15, 2017 via email

@jac1013
Copy link

jac1013 commented Feb 9, 2020

I know this is an old issue, this might help people that stumble into this these days, circleci.test doesn't solve the issue that's mentioned here #1269 (comment) (the main issue this ticket was created for). I'm a beginner in Clojure and I couldn't sort it out (maybe there is a way which I cannot understand).

Ended up doing my setup and teardown in a bash script 🤷‍♂️, at least this let me run lein test without setting up my test database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants