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

Proposed by Elliot Murphy
Status: Superseded
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
Robert Collins Needs Fixing
Jonathan Lange Pending
Review via email: mp+5405@code.launchpad.net

This proposal has been superseded by a proposal from 2009-06-16.

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

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.

lp:~statik/testtools/skip-decorators updated
16. By Elliot Murphy

Merged trunk.

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

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 :

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 :

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 :

> 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 :

> > 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.

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

Reformatted imports per code review comments from jml.

Unmerged revisions

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

Subscribers

People subscribed via source and target branches