Skip to content

Commit

Permalink
Fix setUpClass upcalls on Python 2.6.
Browse files Browse the repository at this point in the history
``testtools.TestCase`` now inherits from unittest2.TestCase, which
provides a ``setUpClass`` for upcalls on Python 2.6.
(Robert Collins, #1393283)

Change-Id: Id56212e3d7d519c7b73d2e19d3e34013fac34544
  • Loading branch information
rbtcollins committed Nov 17, 2014
1 parent f6034dd commit d30ee53
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Changes and improvements to testtools_, grouped by release.
NEXT
~~~~

Changes
-------

* ``testtools.TestCase`` now inherits from unittest2.TestCase, which
provides a ``setUpClass`` for upcalls on Python 2.6.
(Robert Collins, #1393283)

1.3.0
~~~~~

Expand Down
2 changes: 1 addition & 1 deletion testtools/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import itertools
import sys
import types
import unittest

from extras import (
safe_hasattr,
try_import,
)
import unittest2 as unittest

from testtools import (
content,
Expand Down
47 changes: 25 additions & 22 deletions testtools/tests/test_testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,40 +1602,43 @@ def test_raises(self):
self.assertRaises(ZeroDivisionError, wrapped)


class Attributes(WithAttributes, TestCase):
@attr('foo')
def simple(self):
pass

# Not sorted here, forward or backwards.
@attr('foo', 'quux', 'bar')
def many(self):
pass

# Not sorted here, forward or backwards.
@attr('bar')
@attr('quux')
@attr('foo')
def decorated(self):
pass


class TestAttributes(TestCase):

def test_simple_attr(self):
# Adding an attr to a test changes its id().
class MyTest(WithAttributes, TestCase):
@attr('foo')
def test_bar(self):
pass
case = MyTest('test_bar')
self.assertEqual('testtools.tests.test_testcase.MyTest.test_bar[foo]',
case = Attributes('simple')
self.assertEqual(
'testtools.tests.test_testcase.Attributes.simple[foo]',
case.id())

def test_multiple_attributes(self):
class MyTest(WithAttributes, TestCase):
# Not sorted here, forward or backwards.
@attr('foo', 'quux', 'bar')
def test_bar(self):
pass
case = MyTest('test_bar')
case = Attributes('many')
self.assertEqual(
'testtools.tests.test_testcase.MyTest.test_bar[bar,foo,quux]',
'testtools.tests.test_testcase.Attributes.many[bar,foo,quux]',
case.id())

def test_multiple_attr_decorators(self):
class MyTest(WithAttributes, TestCase):
# Not sorted here, forward or backwards.
@attr('bar')
@attr('quux')
@attr('foo')
def test_bar(self):
pass
case = MyTest('test_bar')
case = Attributes('decorated')
self.assertEqual(
'testtools.tests.test_testcase.MyTest.test_bar[bar,foo,quux]',
'testtools.tests.test_testcase.Attributes.decorated[bar,foo,quux]',
case.id())


Expand Down
19 changes: 19 additions & 0 deletions testtools/tests/test_testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,25 @@ def test_notrun(self):
suite.run(result)
self.assertEqual(['addSkip'], [item[0] for item in log])

def test_setupclass_upcall(self):
# Note that this is kindof-a-case-test, kindof-suite, because
# setUpClass is linked between them.
class Simples(TestCase):
@classmethod
def setUpClass(cls):
super(Simples, cls).setUpClass()
def test_simple(self):
pass
# Test discovery uses the default suite from unittest2 (unless users
# deliberately change things, in which case they keep both pieces).
suite = unittest2.TestSuite([Simples("test_simple")])
log = []
result = LoggingResult(log)
suite.run(result)
self.assertEqual(
['startTest', 'addSuccess', 'stopTest'],
[item[0] for item in log])


class TestFixtureSuite(TestCase):

Expand Down

0 comments on commit d30ee53

Please sign in to comment.