Merge lp:~statik/testtools/skip-decorators into lp:~testtools-committers/testtools/trunk

Proposed by Elliot Murphy
Status: Merged
Approved by: Jonathan Lange
Approved revision: 16
Merged at revision: not available
Proposed branch: lp:~statik/testtools/skip-decorators
Merge into: lp:~testtools-committers/testtools/trunk
Diff against target: None lines
To merge this branch: bzr merge lp:~statik/testtools/skip-decorators
Reviewer Review Type Date Requested Status
Jonathan Lange Approve
Review via email: mp+7524@code.launchpad.net

This proposal supersedes a proposal from 2009-04-09.

Commit message

This adds some decorators:
 skip, skipIf, and skipUnless

They are intended to enable people to wire in test skipping using the
same decorators that are in the python2.7 standard library, so that
migration to python2.7 doesn't need to change any test cases, just
import the decorator from the standard library instead of testools.

To post a comment you must log in.
Revision history for this message
Elliot Murphy (statik) wrote : Posted in a previous version of this proposal

Add skip, skipIf, and skipUnless decorators to make it easy to write unit tests which are forward-compatible with the skipping support in python2.7 stdlib.

Revision history for this message
Elliot Murphy (statik) wrote : Posted in a previous version of this proposal

Merged with trunk to pick up the latest stuff from Robert. Rev16 of this branch is what is going through REVU now, to hopefully go into Karmic when the archives open.

Revision history for this message
Robert Collins (lifeless) wrote : Posted in a previous version of this proposal

I did it by mail, it failed. so here is:
 status needsfixing

        @This has a test for the skip decorator, but could probably use some
        cleverness with clone_test_with_new_id in order to generate some
        tests for all three decorators.

        see testscenarios for glue to do this with much ease.

        I hope to get a patch into 2.7 to make skips take closures not evaluated
        conditions. Or something. Bah!

        Needs info in the manual about this.

        -Rob

review: Needs Fixing
Revision history for this message
Jonathan Lange (jml) wrote : Posted in a previous version of this proposal

Sorry about the delay on this patch -- what's the story, is it still something we want to land?

Revision history for this message
Elliot Murphy (statik) wrote : Posted in a previous version of this proposal

> Sorry about the delay on this patch -- what's the story, is it still something
> we want to land?

Yes please, I'd like to see it land. I can work on adding some other tests tonight, but I'm hesitant to add a dependency on testscenarios.

Revision history for this message
Elliot Murphy (statik) wrote : Posted in a previous version of this proposal

> > Sorry about the delay on this patch -- what's the story, is it still
> something
> > we want to land?
>
> Yes please, I'd like to see it land. I can work on adding some other tests
> tonight, but I'm hesitant to add a dependency on testscenarios.

Oops, I actually already added the missing tests a while ago. I'm going to resubmit this merge proposal so the diff will be regenerated.

Revision history for this message
Jonathan Lange (jml) wrote :

Hey Elliot,

Thanks for doing this patch and thanks again for your patience in waiting for me to review it!

It all looks fine to me. Please change the imports in testtools/tests/test_testtools.py so that there's one per line, e.g.

  from foo.bar import (
    baz,
    qux,
    )

Thanks,
jml

review: Approve
lp:~statik/testtools/skip-decorators updated
17. By Elliot Murphy

Reformatted imports per code review comments from jml.

Revision history for this message
Elliot Murphy (statik) wrote :

Thanks for the review! I've reformatted the imports as requested and pushed rev 17.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'testtools/__init__.py'
2--- testtools/__init__.py 2009-03-27 08:20:20 +0000
3+++ testtools/__init__.py 2009-04-16 18:21:49 +0000
4@@ -9,10 +9,19 @@
5 'MultiTestResult',
6 'TestCase',
7 'TestResult',
8+ 'skip',
9+ 'skipIf',
10+ 'skipUnless',
11 'ThreadsafeForwardingResult',
12 ]
13
14-from testtools.testcase import TestCase, clone_test_with_new_id
15+from testtools.testcase import (
16+ TestCase,
17+ clone_test_with_new_id,
18+ skip,
19+ skipIf,
20+ skipUnless,
21+ )
22 from testtools.testresult import (
23 MultiTestResult,
24 TestResult,
25
26=== modified file 'testtools/testcase.py'
27--- testtools/testcase.py 2009-03-05 04:38:11 +0000
28+++ testtools/testcase.py 2009-04-09 17:24:49 +0000
29@@ -6,9 +6,13 @@
30 __all__ = [
31 'change_test_id',
32 'TestCase',
33+ 'skip',
34+ 'skipIf',
35+ 'skipUnless',
36 ]
37
38 from copy import deepcopy
39+import functools
40 import unittest
41
42
43@@ -199,3 +203,35 @@
44 newTest.id = lambda: new_id
45 return newTest
46
47+def skip(reason):
48+ """A decorator to skip unit tests.
49+
50+ This is just syntactic sugar so users don't have to change any of their
51+ unit tests in order to migrate to python 2.7, which provides the
52+ @unittest.skip decorator.
53+ """
54+ def decorator(test_item):
55+ @functools.wraps(test_item)
56+ def skip_wrapper(*args, **kwargs):
57+ raise TestCase.skipException(reason)
58+ return skip_wrapper
59+ return decorator
60+
61+
62+def skipIf(condition, reason):
63+ """Skip a test if the condition is true."""
64+ if condition:
65+ return skip(reason)
66+ def _id(obj):
67+ return obj
68+ return _id
69+
70+
71+def skipUnless(condition, reason):
72+ """Skip a test unless the condition is true."""
73+ if not condition:
74+ return skip(reason)
75+ def _id(obj):
76+ return obj
77+ return _id
78+
79
80=== modified file 'testtools/tests/test_testtools.py'
81--- testtools/tests/test_testtools.py 2009-02-28 04:22:53 +0000
82+++ testtools/tests/test_testtools.py 2009-04-09 17:34:30 +0000
83@@ -3,7 +3,8 @@
84 """Tests for extensions to the base test library."""
85
86 import unittest
87-from testtools import TestCase, clone_test_with_new_id
88+from testtools import (TestCase, clone_test_with_new_id,
89+ skip, skipIf, skipUnless)
90 from testtools.tests.helpers import LoggingResult
91
92
93@@ -428,6 +429,38 @@
94 test.run(result)
95 self.assertEqual(1, len(result.errors))
96
97+ def test_skip_decorator(self):
98+ class SkippingTest(TestCase):
99+ @skip("skipping this test")
100+ def test_that_is_decorated_with_skip(self):
101+ self.fail()
102+ result = unittest.TestResult()
103+ test = SkippingTest("test_that_is_decorated_with_skip")
104+ test.run(result)
105+ self.assertEqual(1, len(result.errors))
106+
107+
108+ def test_skipIf_decorator(self):
109+ class SkippingTest(TestCase):
110+ @skipIf(True, "skipping this test")
111+ def test_that_is_decorated_with_skipIf(self):
112+ self.fail()
113+ result = unittest.TestResult()
114+ test = SkippingTest("test_that_is_decorated_with_skipIf")
115+ test.run(result)
116+ self.assertEqual(1, len(result.errors))
117+
118+
119+ def test_skipUnless_decorator(self):
120+ class SkippingTest(TestCase):
121+ @skipUnless(False, "skipping this test")
122+ def test_that_is_decorated_with_skipUnless(self):
123+ self.fail()
124+ result = unittest.TestResult()
125+ test = SkippingTest("test_that_is_decorated_with_skipUnless")
126+ test.run(result)
127+ self.assertEqual(1, len(result.errors))
128+
129
130 def test_suite():
131 from unittest import TestLoader

Subscribers

People subscribed via source and target branches