~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to mago/core.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
import unittest
 
17
import sys
 
18
import testtools
 
19
 
 
20
try:
 
21
    import nose
 
22
except ImportError:
 
23
    print """nose is required. Install the package python-nose.
 
24
Exiting..."""
 
25
    sys.exit(1)
 
26
 
 
27
from mago.application import TestApplication
 
28
from mago.config import get_config
 
29
 
 
30
# Available Nose plugins
 
31
from mago.noseplugins.maybe import Maybe # Just a test plugin
 
32
from mago.noseplugins.magoxml import MagoXML
 
33
PLUGINS = [ Maybe(), MagoXML() ]
 
34
 
 
35
# Global configuration
 
36
# defaults to ~/.magorc (defined in config.py)
 
37
CONFIGFILE = None
 
38
 
 
39
# Global Configuration
 
40
magoConfig = get_config()
 
41
 
 
42
from inspect import getfile, getmodule
 
43
 
 
44
class TestCase(testtools.TestCase):
 
45
    """Base class for desktop testing"""
 
46
    setupOnce = False
 
47
 
 
48
    # Set it static because LDTP can run only one app at once.
 
49
    application = None
 
50
 
 
51
    # Class and Test Configuration
 
52
    # This configuration is specific to the run and different from the
 
53
    # global configuration.
 
54
    # By default the name $(basename MODULEFILEPATH).ini
 
55
    testConfig = None
 
56
 
 
57
    def __init__(self, methodName = 'testMethod'):
 
58
        """Construct a TestCase instance
 
59
 
 
60
        Loads the configuration for the application and the test
 
61
        """
 
62
        super(TestCase, self).__init__(methodName)
 
63
        #self.magoConfig = get_config()
 
64
 
 
65
        moduleFile = getfile(getmodule(self))
 
66
        classConfigFile = '.'.join(moduleFile.split('.')[:-1]) + ".ini"
 
67
        self.testConfig = get_config(classConfigFile)
 
68
 
 
69
 
 
70
    @classmethod
 
71
    def setUpClass(self):
 
72
        """setUpClass
 
73
 
 
74
        Called once for all the tests in a TestCase.
 
75
        set setupOnce to True to activate it.
 
76
        This is used if you want to launch the application to test only once
 
77
        at the beginning of the test session or want to do some extensive setup
 
78
        """
 
79
        super(TestCase, self).setUpClass()
 
80
 
 
81
    def setUp(self):
 
82
        """setUp
 
83
 
 
84
        Launch the application when setupOnce is set to False
 
85
        """
 
86
        super(TestCase, self).setUp()
 
87
 
 
88
        if not hasattr(TestCase, 'application'):
 
89
            TestCase.application = None
 
90
 
 
91
        application = TestCase.application
 
92
        # Check mandatory propertys, launcher, launcher_args and window_name
 
93
 
 
94
        if not application:
 
95
            TestCase.application =\
 
96
                    TestApplication(self.launcher, window_name = self.window_name)
 
97
            #TestCase.application.launch()
 
98
        else:
 
99
            if not application.isRunning:
 
100
                TestCase.application.launch()
 
101
 
 
102
    @classmethod
 
103
    def tearDownClass(self):
 
104
        """tearDownClass
 
105
 
 
106
        Called once at the end of the class to close the application and
 
107
        cleanup the environment and destroy the application.
 
108
        Only called if setupOnce is set to True.
 
109
        """
 
110
        super(TestCase, self).tearDownClass()
 
111
        if self.setupOnce and TestCase.application:
 
112
            del TestCase.application
 
113
 
 
114
    def tearDown(self):
 
115
        """tearDown
 
116
 
 
117
        Called once at the end of the test to close the application and
 
118
        cleanup the environment and destroy the application.
 
119
        Only called if setupOnce is set to False.
 
120
        """
 
121
        super(TestCase, self).tearDown()
 
122
        if not self.setupOnce and TestCase.application:
 
123
            del TestCase.application
 
124
 
 
125
    def getapplication(self):
 
126
        return self.application
 
127
 
 
128
 
 
129
class TestResult(testtools.TestResult):
 
130
    showAll = True
 
131
    def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1):
 
132
        super(TestResult, self).__init__()
 
133
        print "TestResult::__init__"
 
134
        self.stream = stream
 
135
        self.descriptions = descriptions
 
136
 
 
137
    def getDescription(self, test):
 
138
        doc_first_line = test.shortDescription()
 
139
        if self.descriptions and doc_first_line:
 
140
            return '\n'.join((str(test), doc_first_line))
 
141
        else:
 
142
            return str(test)
 
143
 
 
144
    def startTest(self, test):
 
145
        super(TestResult, self).startTest(test)
 
146
        if self.showAll:
 
147
            self.stream.write(self.getDescription(test))
 
148
            self.stream.write(" ... ")
 
149
            self.stream.flush()
 
150
 
 
151
    def addSuccess(self, test):
 
152
        super(TestResult, self).addSuccess(test)
 
153
        if self.showAll:
 
154
            self.stream.write(self.getDescription(test))
 
155
            self.stream.write(" Success ")
 
156
            self.stream.flush()
 
157
 
 
158
 
 
159
class TextTestRunner(unittest.TextTestRunner):
 
160
    resultclass = TestResult
 
161
 
 
162
    #def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, resultclass=None):
 
163
    #    super(TextTestRunner, self).__init__(stream, descriptions, verbosity, resultclass)
 
164
    #    print "TestRunner::__init__"
 
165
 
 
166
#main = nose.main
 
167
 
 
168
def main():
 
169
    global PLUGINS
 
170
 
 
171
    nose.main(addplugins = PLUGINS)
 
172
 
 
173
 
 
174
if __name__ == '__main__':
 
175
    main()