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

subtests error in the subTest decorator. #235

Open
come-maiz opened this issue Jun 16, 2016 · 12 comments
Open

subtests error in the subTest decorator. #235

come-maiz opened this issue Jun 16, 2016 · 12 comments

Comments

@come-maiz
Copy link
Contributor

come-maiz commented Jun 16, 2016

python's unittest now has a cool feature for test scenarios called subTest: https://docs.python.org/3/library/unittest.html#unittest.TestCase.subTest

I'm trying to update some tests from unittest.TestCase to testtools.TestCase, but there are some that use subTest which blocks me from using testtools.

This is the trace from one of the failing tests:

ERROR: test_yaml_valid_epochs (snapcraft.tests.test_yaml.TestYaml)
snapcraft.tests.test_yaml.TestYaml.test_yaml_valid_epochs
----------------------------------------------------------------------
testtools.testresult.real._StringException: Empty attachments:
  pythonlogging:''
  pythonlogging:''-1

Traceback (most recent call last):
  File "/usr/lib/python3.5/unittest/mock.py", line 1157, in patched
    return func(*args, **keywargs)
  File "/home/elopio/workspace/canonical/snapcraft/snapcraft/tests/test_yaml.py", line 669, in test_yaml_valid_epochs
    with self.subTest(key=epoch):
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/lib/python3/dist-packages/unittest2/case.py", line 535, in subTest
    if not self._outcome.result_supports_subtests:
AttributeError: 'NoneType' object has no attribute 'result_supports_subtests'

I think it would be great if testtools supports subTests.

@rbtcollins
Copy link
Member

Indeed, it would be nice, Would love it if you wanted to put a patch forward.

@rbtcollins rbtcollins changed the title Doesn't support subTests subtests error in the subTest decorator. Jun 17, 2016
@rbtcollins
Copy link
Member

Arguably the bug is in subTest itself - it presumes _outcome exists, but _outcome is a unittest internal attribute that isn't part of the API contract, so we should fix this in CPython's stdlib.

@come-maiz
Copy link
Contributor Author

I agree that unittest should have a fall back, and that the error sucks because of what they are doing. I'll rename this bug.

@come-maiz
Copy link
Contributor Author

Oh, noes, it was you who renamed it. I think my name was better 😄

@nicoddemus
Copy link

Filed an upstream issue: https://bugs.python.org/issue34900

@nicoddemus
Copy link

python/cpython#9707 👍

@nicoddemus
Copy link

That PR has been merged, so I guess this can now be closed. 👍

@marph91
Copy link

marph91 commented Nov 9, 2019

I'm running in the same error, using Python 3.6.9. The change should be already included in 3.6.8.

Simple reproducer:

testsuite.py

import testtools


class TC(testtools.TestCase):
    def test_subtest(self):
        with self.subTest():
            pass

Output:

> python3 -m unittest testsuite.py 
E
======================================================================
ERROR: test_subtest (testsuite.TC)
testsuite.TC.test_subtest
----------------------------------------------------------------------
testtools.testresult.real._StringException: Traceback (most recent call last):
  File "/home/workspace/python/test_unittest/testsuite.py", line 32, in test_subtest
    with self.subTest():
  File "/usr/lib64/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "/usr/lib/python3.6/site-packages/unittest2/case.py", line 535, in subTest
    if not self._outcome.result_supports_subtests:
AttributeError: 'NoneType' object has no attribute 'result_supports_subtests'


----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

@ecbaldwin
Copy link

I'm running into the same thing in 3.8.1.

  File "/code/cascade_auth/tests/unit/db/alembic/test_migrations.py", line 116, in test_each_migration
    with self.subTest("Post upgrade: %s" % test_class.__name__):
  File "/usr/local/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.8/site-packages/unittest2/case.py", line 535, in subTest
    if not self._outcome.result_supports_subtests:
AttributeError: 'NoneType' object has no attribute 'result_supports_subtests'

@FedericoRessi
Copy link
Contributor

The problem here is testools is using unittest2 [1] library that hasn't desired fix implemented.

File "/usr/local/lib/python3.8/site-packages/unittest2/case.py", line 535, in subTest

[1] https://github.com/testing-cabal/testtools/blob/master/requirements.txt

Anyway the above patch is just skipping implementing the feature if self._outcome is None. Therefore an actual fix should make test cases providing some instance for self._outcome attribute to have subtests properly working.

@nicoddemus
Copy link

I'm sure a similar patch to python/cpython#9707 can be applied to unittest2 if someone is inclined to open a PR.

@FedericoRessi
Copy link
Contributor

I am proposing to drop using very old unittest2 library for newer python versions. Such library hasn't changed since 2005 :-(

FedericoRessi pushed a commit to FedericoRessi/testtools that referenced this issue Jul 8, 2020
unittest2 library isn't been updated since some years.
Newer python frameworks are expected to has far more fixes
than that unittest2.

Actually unittest2 states it is only tested up to python 3.4

This therefore limit the use of unittest2 library up to
the last python 3.4 release.

It is expected to fix Issue testing-cabal#235
openstack-mirroring pushed a commit to openstack/ironic that referenced this issue Oct 14, 2022
Any version of testtools lower than 2.5.0 fails if the
attribute 'result_supports_subtests' is present on any
test object because of old unittest support.

See also testing-cabal/testtools#235
and testing-cabal/testtools@38fc9a9
for more info.

Change-Id: I23a1ca789f4ea4ffd981e1071f3c2c6c333ac330
openstack-mirroring pushed a commit to openstack/openstack that referenced this issue Oct 14, 2022
* Update ironic from branch 'master'
  to 4339f6f65dbecaf7d38bdb985465fb7c1124b63c
  - Merge "Bump min version of testtools"
  - Bump min version of testtools
    
    Any version of testtools lower than 2.5.0 fails if the
    attribute 'result_supports_subtests' is present on any
    test object because of old unittest support.
    
    See also testing-cabal/testtools#235
    and testing-cabal/testtools@38fc9a9
    for more info.
    
    Change-Id: I23a1ca789f4ea4ffd981e1071f3c2c6c333ac330
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

No branches or pull requests

6 participants