~ubuntuone-control-tower/ubuntuone-dev-tools/stable-0-2

« back to all changes in this revision

Viewing changes to ubuntuone/devtools/tests/test_decorators.py

  • Committer: Tarmac
  • Author(s): Manuel de la Pena
  • Date: 2011-01-04 21:03:41 UTC
  • mfrom: (19.1.12 skip_test)
  • Revision ID: tarmac-20110104210341-8y2877edyu3cpjlu
Adds a number of decorators that allows to skip tests if certain conditions are met. Usage can be seen in the test_decorators.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Author: Manuel de la Pena <manuel@canonical.com>
 
4
#
 
5
# Copyright 2009-2010 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
"""Test the skip decorators."""
 
19
import sys
 
20
 
 
21
from twisted.trial.runner import LoggedSuite
 
22
from twisted.trial.reporter import TestResult
 
23
 
 
24
from ubuntuone.devtools import testcase
 
25
from ubuntuone.devtools.testcase import BaseTestCase
 
26
 
 
27
OTHER_PLATFORM = {
 
28
    "darwin": "win32",
 
29
    "win32": "linux2",
 
30
    "linux2": "win32"}
 
31
 
 
32
 
 
33
class TestSkipBasicDecorators(BaseTestCase):
 
34
    """Test skipping decorators."""
 
35
 
 
36
    def test_skip_decorators(self):
 
37
        """Test the decorators that skip tests."""
 
38
        operations_table = (
 
39
            (testcase.skipIf,
 
40
                (False, "False condition"),
 
41
                (True, "True condition")),
 
42
            (testcase.skipIfOS,
 
43
                (OTHER_PLATFORM[sys.platform], "skipIf other platform"),
 
44
                (sys.platform, "skipIf this platform")),
 
45
            (testcase.skipIfNotOS,
 
46
                (sys.platform, "skipIfNot this platform"),
 
47
                (OTHER_PLATFORM[sys.platform],
 
48
                    "skipIfNot other platform")))
 
49
        for deco, dont_skip, do_skip in operations_table:
 
50
 
 
51
            class Foo(BaseTestCase):
 
52
                """Dummy test case used for the decorators testing."""
 
53
                @deco(do_skip[0], do_skip[1])
 
54
                def test_skip(self):
 
55
                    """Test to skip."""
 
56
                    pass
 
57
 
 
58
                @deco(dont_skip[0], dont_skip[1])
 
59
                def test_dont_skip(self):
 
60
                    """Test not to skip."""
 
61
                    pass
 
62
 
 
63
            test_do_skip = Foo("test_skip")
 
64
            test_dont_skip = Foo("test_dont_skip")
 
65
            suite = LoggedSuite([test_do_skip, test_dont_skip])
 
66
            result = TestResult()
 
67
            suite.run(result)
 
68
            self.assertEqual(len(result.skips), 1)
 
69
            self.assertEqual(result.successes, 1)
 
70
            self.assertEqual(result.skips,
 
71
                [(test_do_skip, do_skip[1])])
 
72
 
 
73
    def test_skip_class(self):
 
74
        """Test skipping a full test class."""
 
75
        class Foo(BaseTestCase):
 
76
            """Test class to be skipped."""
 
77
 
 
78
            def test_1(self):
 
79
                """First test to skip."""
 
80
                record.append(1)
 
81
 
 
82
            def test_2(self):
 
83
                """Second test to skip."""
 
84
                record.append(1)
 
85
 
 
86
        # Decorate the class.
 
87
        # pylint: disable=C0103
 
88
        Foo = testcase.skipTest("testing")(Foo)
 
89
        # pylint: enable=C0103
 
90
        record = []
 
91
        result = TestResult()
 
92
        test = Foo("test_1")
 
93
        suite = LoggedSuite([test])
 
94
        suite.run(result)
 
95
        self.assertEqual(result.skips, [(test, "testing")])
 
96
        self.assertEqual(record, [])