-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
unittest should understand SkipTest at import time during test discovery #61139
Comments
For test discovery to work where a dependent module is optional, you end up needing to do something like what is done in http://hg.python.org/cpython/rev/15ddd683c321: -crypt = support.import_module('crypt')
+def setUpModule():
+ # this import will raise unittest.SkipTest if _crypt doesn't exist,
+ # so it has to be done in setUpModule for test discovery to work
+ global crypt
+ crypt = support.import_module('crypt') That's kind of ugly. It would be better if unittest recognized SkipTest at import time during test discovery |
Agreed and it should be easy to implement. |
FTR there is already an alternative to setupmodule: try: @unittest.skipUnless(module, 'requires module') This idiom is more lines than support.import_module, but works for non-stdlib tests too, and is useful when you don’t want the whole file to be skipped if the module is missing (like in distutils’ test_sdist where zlib can be missing). |
FWIW the difference between support.import_module and the try/except/skip is that usually the former is used when *all* the tests require the imported module, whereas the latter is used when only some of the tests requires it. |
Without the proposed enhancement, you could also combine Éric's approach with the original patch by doing something like: try: def setUpModule():
if module is None:
raise SkipTest() |
Should be: "module = support.import_module('module')" |
Still, raising SkipTest from the toplevel is useful when some toplevel setup code otherwise depends on the missing module. |
I agree that raising SkipTest (or subclasses thereof, such as ResourceDenied) at module level should be supported. That would mean no changes would be needed in most of the should-be-skipped-but-fail-instead tests listed in bpo-16748 to make test discovery play nicely, and in fact the changes to test_crypt could be mostly reverted. Personally, I don't find either of the suggestions given as alternates to what I did in test_crypt to be particularly prettier, not that what I did is pretty either. |
Here's a patch that I believe nicely handles the raising of unittest.SkipTest at module level while doing test discovery. It adds a _make_skipped_test function to unittest.loader, and an Here's some example output: ---------------------------------------------------------------------- OK (skipped=1) |
The patch looks good - can you add a test and documentation for this? |
Sure can. With a little luck, I'll have the patch ready later today; with less luck it'll be sometime later this week. |
I think this patch should cover the test and Doc changes necessary. Of course, let me know if it doesn't :) |
Thanks for the review, Ezio. I've made some changes and here's the new patch. |
LGTM. |
New changeset 61ce6deb4577 by Ezio Melotti in branch 'default': |
Applied, thanks for the patch! |
New changeset 22b6b59c70e6 by Ezio Melotti in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: