msg179681 - (view) |
Author: Brett Cannon (brett.cannon) *  |
Date: 2013-01-11 14:43 |
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 |
|
|
msg179682 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2013-01-11 14:44 |
Agreed and it should be easy to implement. |
|
|
msg179700 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2013-01-11 16:29 |
FTR there is already an alternative to setupmodule: try: import module except ImportError: module = None @unittest.skipUnless(module, 'requires module') class ModuleTests(unittest.TestCase): pass 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). |
|
|
msg179770 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2013-01-12 05:34 |
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. |
|
|
msg179903 - (view) |
Author: Chris Jerdonek (chris.jerdonek) *  |
Date: 2013-01-14 00:06 |
Without the proposed enhancement, you could also combine Éric's approach with the original patch by doing something like: try: support.import_module(module) except SkipTest: module = None def setUpModule(): if module is None: raise SkipTest() |
|
|
msg179904 - (view) |
Author: Chris Jerdonek (chris.jerdonek) *  |
Date: 2013-01-14 00:08 |
Should be: "module = support.import_module('module')" |
|
|
msg179946 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2013-01-14 16:01 |
Still, raising SkipTest from the toplevel is useful when some toplevel setup code otherwise depends on the missing module. |
|
|
msg179952 - (view) |
Author: Zachary Ware (zach.ware) *  |
Date: 2013-01-14 16:34 |
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 issue 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. |
|
|
msg180877 - (view) |
Author: Zachary Ware (zach.ware) *  |
Date: 2013-01-28 19:48 |
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 ``except case.SkipTest`` clause to TestLoader._find_tests. For our own test package, this covers non-enabled resources as well. Here's some example output: """ P:\cpython>python -m unittest discover -v Lib/test/ "test_curs*" test_curses (unittest.loader.ModuleSkipped) ... skipped "Use of the 'curses' res ource not enabled" ---------------------------------------------------------------------- Ran 1 test in 0.001s OK (skipped=1) [102289 refs, 38970 blocks] """ |
|
|
msg181864 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2013-02-11 00:32 |
The patch looks good - can you add a test and documentation for this? |
|
|
msg181902 - (view) |
Author: Zachary Ware (zach.ware) *  |
Date: 2013-02-11 15:53 |
Sure can. With a little luck, I'll have the patch ready later today; with less luck it'll be sometime later this week. |
|
|
msg181909 - (view) |
Author: Zachary Ware (zach.ware) *  |
Date: 2013-02-11 16:54 |
I think this patch should cover the test and Doc changes necessary. Of course, let me know if it doesn't :) |
|
|
msg182969 - (view) |
Author: Zachary Ware (zach.ware) *  |
Date: 2013-02-25 18:29 |
Thanks for the review, Ezio. I've made some changes and here's the new patch. |
|
|
msg183122 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2013-02-27 08:16 |
LGTM. |
|
|
msg183256 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2013-03-01 12:48 |
New changeset 61ce6deb4577 by Ezio Melotti in branch 'default': #16935: unittest now counts the module as skipped if it raises SkipTest, instead of counting it as an error. Patch by Zachary Ware. http://hg.python.org/cpython/rev/61ce6deb4577 |
|
|
msg183257 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2013-03-01 12:49 |
Applied, thanks for the patch! SkipTest should probably be documented, but that's a separate issue :) |
|
|
msg183258 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2013-03-01 12:54 |
New changeset 22b6b59c70e6 by Ezio Melotti in branch 'default': #16935: update test_crypt now that unittest discover understands SkipTest. http://hg.python.org/cpython/rev/22b6b59c70e6 |
|
|
msg297375 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2017-06-30 10:52 |
New changeset e4f9a2d2be42d5a2cdd624f8ed7cdf5028c5fbc3 by Victor Stinner in branch 'master': bpo-30813: Fix unittest when hunting refleaks (#2502) https://github.com/python/cpython/commit/e4f9a2d2be42d5a2cdd624f8ed7cdf5028c5fbc3 |
|
|
msg297385 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2017-06-30 11:12 |
New changeset 714afccf6e7644d21ce1a39e90bf83cb0c9a74f1 by Victor Stinner in branch '3.5': bpo-30813: Fix unittest when hunting refleaks (#2502) (#2506) https://github.com/python/cpython/commit/714afccf6e7644d21ce1a39e90bf83cb0c9a74f1 |
|
|
msg297389 - (view) |
Author: STINNER Victor (vstinner) *  |
Date: 2017-06-30 11:12 |
New changeset 22d4e8fb99b16657eabfe7f9fee2d40a5ef882f6 by Victor Stinner in branch '3.6': bpo-30813: Fix unittest when hunting refleaks (#2502) (#2505) https://github.com/python/cpython/commit/22d4e8fb99b16657eabfe7f9fee2d40a5ef882f6 |
|
|