Robot Framework Integrated Development Environment (RIDE)
context.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from contextlib import contextmanager
17 
18 from robotide.lib.robot.errors import DataError
19 from robotide.lib.robot.utils import unic
20 
21 
23 
24  def __init__(self):
25  self._contexts_contexts = []
26 
27  @property
28  current = property
29 
30  def current(self):
31  return self._contexts_contexts[-1] if self._contexts_contexts else None
32 
33  @property
34  top = property
35 
36  def top(self):
37  return self._contexts_contexts[0] if self._contexts_contexts else None
38 
39  def __iter__(self):
40  return iter(self._contexts_contexts)
41 
42  @property
43  namespaces = property
44 
45  def namespaces(self):
46  return (context.namespace for context in self)
47 
48  def start_suite(self, suite, namespace, output, dry_run=False):
49  ctx = _ExecutionContext(suite, namespace, output, dry_run)
50  self._contexts_contexts.append(ctx)
51  return ctx
52 
53  def end_suite(self):
54  self._contexts_contexts.pop()
55 
56 
57 # This is ugly but currently needed e.g. by BuiltIn
58 EXECUTION_CONTEXTS = ExecutionContexts()
59 
60 
62 
65  _started_keywords_threshold = 42 # Jython on Windows don't work with higher
66 
67  def __init__(self, suite, namespace, output, dry_run=False):
68  self.suitesuite = suite
69  self.testtest = None
70  self.timeoutstimeouts = set()
71  self.namespacenamespace = namespace
72  self.outputoutput = output
73  self.dry_rundry_run = dry_run
74  self.in_suite_teardownin_suite_teardown = False
75  self.in_test_teardownin_test_teardown = False
76  self.in_keyword_teardownin_keyword_teardown = 0
77  self._started_keywords_started_keywords = 0
78  self.timeout_occurredtimeout_occurred = False
79 
80  @contextmanager
81  def suite_teardown(self):
82  self.in_suite_teardownin_suite_teardown = True
83  try:
84  yield
85  finally:
86  self.in_suite_teardownin_suite_teardown = False
87 
88  @contextmanager
89  def test_teardown(self, test):
90  self.variablesvariablesvariables.set_test('${TEST_STATUS}', test.status)
91  self.variablesvariablesvariables.set_test('${TEST_MESSAGE}', test.message)
92  self.in_test_teardownin_test_teardown = True
93  self._remove_timeout_remove_timeout(test.timeout)
94  try:
95  yield
96  finally:
97  self.in_test_teardownin_test_teardown = False
98 
99  @contextmanager
100  def keyword_teardown(self, error):
101  self.variablesvariablesvariables.set_keyword('${KEYWORD_STATUS}', 'FAIL' if error else 'PASS')
102  self.variablesvariablesvariables.set_keyword('${KEYWORD_MESSAGE}', unic(error or ''))
103  self.in_keyword_teardownin_keyword_teardown += 1
104  try:
105  yield
106  finally:
107  self.in_keyword_teardownin_keyword_teardown -= 1
108 
109  @property
110  @contextmanager
111  user_keyword = property
112 
113  def user_keyword(self):
114  self.namespacenamespace.start_user_keyword()
115  try:
116  yield
117  finally:
118  self.namespacenamespace.end_user_keyword()
119 
120  @contextmanager
121  def timeout(self, timeout):
122  self._add_timeout_add_timeout(timeout)
123  try:
124  yield
125  finally:
126  self._remove_timeout_remove_timeout(timeout)
127 
128  @property
129  in_teardown = property
130 
131  def in_teardown(self):
132  return bool(self.in_suite_teardownin_suite_teardown or
133  self.in_test_teardownin_test_teardown or
134  self.in_keyword_teardownin_keyword_teardown)
135 
136  @property
137  variables = property
138 
139  def variables(self):
140  return self.namespacenamespace.variables
141 
142  def end_suite(self, suite):
143  for name in ['${PREV_TEST_NAME}',
144  '${PREV_TEST_STATUS}',
145  '${PREV_TEST_MESSAGE}']:
146  self.variablesvariablesvariables.set_global(name, self.variablesvariablesvariables[name])
147  self.outputoutput.end_suite(suite)
148  self.namespacenamespace.end_suite(suite)
149  EXECUTION_CONTEXTS.end_suite()
150 
151  def set_suite_variables(self, suite):
152  self.variablesvariablesvariables['${SUITE_NAME}'] = suite.longname
153  self.variablesvariablesvariables['${SUITE_SOURCE}'] = suite.source or ''
154  self.variablesvariablesvariables['${SUITE_DOCUMENTATION}'] = suite.doc
155  self.variablesvariablesvariables['${SUITE_METADATA}'] = suite.metadata.copy()
156 
157  def report_suite_status(self, status, message):
158  self.variablesvariablesvariables['${SUITE_STATUS}'] = status
159  self.variablesvariablesvariables['${SUITE_MESSAGE}'] = message
160 
161  def start_test(self, test):
162  self.testtest = test
163  self._add_timeout_add_timeout(test.timeout)
164  self.namespacenamespace.start_test()
165  self.variablesvariablesvariables.set_test('${TEST_NAME}', test.name)
166  self.variablesvariablesvariables.set_test('${TEST_DOCUMENTATION}', test.doc)
167  self.variablesvariablesvariables.set_test('@{TEST_TAGS}', list(test.tags))
168 
169  def _add_timeout(self, timeout):
170  if timeout:
171  timeout.start()
172  self.timeoutstimeouts.add(timeout)
173 
174  def _remove_timeout(self, timeout):
175  if timeout in self.timeoutstimeouts:
176  self.timeoutstimeouts.remove(timeout)
177 
178  def end_test(self, test):
179  self.testtest = None
180  self._remove_timeout_remove_timeout(test.timeout)
181  self.namespacenamespace.end_test()
182  self.variablesvariablesvariables.set_suite('${PREV_TEST_NAME}', test.name)
183  self.variablesvariablesvariables.set_suite('${PREV_TEST_STATUS}', test.status)
184  self.variablesvariablesvariables.set_suite('${PREV_TEST_MESSAGE}', test.message)
185  self.timeout_occurredtimeout_occurred = False
186 
187  def start_keyword(self, keyword):
188  self._started_keywords_started_keywords += 1
189  if self._started_keywords_started_keywords > self._started_keywords_threshold_started_keywords_threshold:
190  raise DataError('Maximum limit of started keywords exceeded.')
191  self.outputoutput.start_keyword(keyword)
192 
193  def end_keyword(self, keyword):
194  self.outputoutput.end_keyword(keyword)
195  self._started_keywords_started_keywords -= 1
196 
197  def get_runner(self, name):
198  return self.namespacenamespace.get_runner(name)
199 
200  def trace(self, message):
201  self.outputoutput.trace(message)
202 
203  def debug(self, message):
204  self.outputoutput.debug(message)
205 
206  def info(self, message):
207  self.outputoutput.info(message)
208 
209  def warn(self, message):
210  self.outputoutput.warn(message)
211 
212  def fail(self, message):
213  self.outputoutput.fail(message)
Used when variable does not exist.
Definition: errors.py:67
def start_suite(self, suite, namespace, output, dry_run=False)
Definition: context.py:48
def report_suite_status(self, status, message)
Definition: context.py:157
def __init__(self, suite, namespace, output, dry_run=False)
Definition: context.py:67