Skip to content

Commit 54baf35

Browse files
author
Matej Cotman
authored
Merge pull request #26 from eficode/feat/multiple-params
Add support for passing multiple args from handler's keyword method
2 parents 4cbc4e9 + eb6f950 commit 54baf35

File tree

11 files changed

+311
-4
lines changed

11 files changed

+311
-4
lines changed

src/oxygen/base_handler.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import re
22

3+
from inspect import signature, Parameter
4+
5+
from oxygen.errors import MismatchArgumentException
36
from .robot_interface import (RobotInterface, get_keywords_from,
47
set_special_keyword)
58

@@ -100,14 +103,42 @@ def _build_results(self, keyword, setup_keyword, teardown_keyword):
100103
setup_keyword: The special oxygen setup wrapper
101104
teardown_keyword: The special oxygen teardown wrapper
102105
'''
103-
test = keyword.parent
104-
test_results = self.parse_results(self.run_time_data)
105-
end_time, result_suite = self._interface.result.build_suite(100000,
106-
test_results)
106+
accepted_params = signature(self.parse_results).parameters
107+
accepted_params_max = len(accepted_params)
108+
accepted_params_min = len([
109+
n for n, v in accepted_params.items()
110+
if v.default == Parameter.empty])
111+
is_multiple_inputs = isinstance(self.run_time_data, tuple)
112+
113+
# there are multiple inputs and in the range of accepted min and max
114+
if is_multiple_inputs and (accepted_params_min <= len(
115+
self.run_time_data) <= accepted_params_max):
116+
test_results = self.parse_results(*self.run_time_data)
117+
118+
# there is single input and one required, also can be more non-required
119+
elif not is_multiple_inputs and accepted_params_min == 1:
120+
test_results = self.parse_results(self.run_time_data)
121+
122+
# else if there are multiple inputs and not in the range of accepted
123+
elif is_multiple_inputs:
124+
raise MismatchArgumentException(
125+
f'parse_results expects at least {accepted_params_min} and'
126+
f' at most {accepted_params_max} arguments '
127+
f'but got {len(self.run_time_data)}')
128+
129+
# at this point there could be only multiple required and single input
130+
else:
131+
raise MismatchArgumentException(
132+
f'parse_results expects at least {accepted_params_min} '
133+
'arguments but got 1')
134+
135+
_, result_suite = self._interface.result.build_suite(
136+
100000, test_results)
107137

108138
if not result_suite:
109139
return
110140

141+
test = keyword.parent
111142
self._set_suite_tags(result_suite, *(self._tags + list(test.tags)))
112143

113144
if setup_keyword:

src/oxygen/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ class ResultFileNotFoundException(Exception):
2424

2525
class ResultFileIsNotAFileException(Exception):
2626
pass
27+
28+
29+
class MismatchArgumentException(Exception):
30+
pass

tests/resources/example_robot_output.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,56 @@
258258
</tags>
259259
<status status="PASS" starttime="20200506 17:39:30.140" endtime="20200506 17:39:30.142" critical="yes"></status>
260260
</test>
261+
<test id="s1-s1-t7" name="My Fifth Test">
262+
<kw name="Log" library="BuiltIn">
263+
<doc>Logs the given message with the given level.</doc>
264+
<arguments>
265+
<arg>My Dummy Handler Setup Here 1</arg>
266+
</arguments>
267+
<msg timestamp="20200506 17:39:28.028" level="INFO">My Dummy Handler Setup Here 1</msg>
268+
<status status="PASS" starttime="20200506 17:39:28.028" endtime="20200506 17:39:28.029"></status>
269+
</kw>
270+
<kw name="Log" library="BuiltIn">
271+
<doc>Logs the given message with the given level.</doc>
272+
<arguments>
273+
<arg>My Dummy Handler Setup Here 2</arg>
274+
</arguments>
275+
<msg timestamp="20200506 17:39:28.029" level="INFO">My Dummy Handler Setup Here 2</msg>
276+
<status status="PASS" starttime="20200506 17:39:28.029" endtime="20200506 17:39:28.029"></status>
277+
</kw>
278+
<kw name="Run My Dummy Handler" library="oxygen.OxygenLibrary">
279+
<doc>Run My Dummy Handler tool specified with ``command``.</doc>
280+
<arguments>
281+
<arg>${RESOURCES}/my_dummy_handler.xml</arg>
282+
<arg>echo</arg>
283+
<arg>MY_DUMMY_HANDLER_TEST_STRING</arg>
284+
</arguments>
285+
<msg timestamp="20200506 17:39:28.041" level="INFO">MY_DUMMY_HANDLER_TEST_STRING
286+
</msg>
287+
<msg timestamp="20200506 17:39:28.041" level="INFO">Result file: /Users/tkairi/Coding/oxygen/tests/atest/../resources/my_dummy_handler.xml</msg>
288+
<status status="PASS" starttime="20200506 17:39:28.029" endtime="20200506 17:39:28.041"></status>
289+
</kw>
290+
<kw name="Log" library="BuiltIn">
291+
<doc>Logs the given message with the given level.</doc>
292+
<arguments>
293+
<arg>My Dummy Handler Teardown Here 1</arg>
294+
</arguments>
295+
<msg timestamp="20200506 17:39:28.042" level="INFO">My Dummy Handler Teardown Here 1</msg>
296+
<status status="PASS" starttime="20200506 17:39:28.042" endtime="20200506 17:39:28.042"></status>
297+
</kw>
298+
<kw name="Log" library="BuiltIn">
299+
<doc>Logs the given message with the given level.</doc>
300+
<arguments>
301+
<arg>My Dummy Handler Teardown Here 2</arg>
302+
</arguments>
303+
<msg timestamp="20200506 17:39:28.043" level="INFO">My Dummy Handler Teardown Here 2</msg>
304+
<status status="PASS" starttime="20200506 17:39:28.043" endtime="20200506 17:39:28.043"></status>
305+
</kw>
306+
<tags>
307+
<tag>MY_DUMMY_HANDLER_ROBOT_TAG</tag>
308+
</tags>
309+
<status status="PASS" starttime="20200506 17:39:28.028" endtime="20200506 17:39:28.043" critical="yes"></status>
310+
</test>
261311
<status status="PASS" starttime="20200506 17:39:27.974" endtime="20200506 17:39:30.143"></status>
262312
</suite>
263313
<status status="PASS" starttime="20200506 17:39:27.937" endtime="20200506 17:39:30.146"></status>
@@ -272,6 +322,7 @@
272322
<stat pass="2" fail="0">JUNIT_ROBOT_TAG</stat>
273323
<stat pass="1" fail="0">NO_OXYGEN_HERE</stat>
274324
<stat pass="2" fail="0">ZAP_ROBOT_TAG</stat>
325+
<stat pass="2" fail="0">MY_DUMMY_HANDLER_ROBOT_TAG</stat>
275326
</tag>
276327
<suite>
277328
<stat pass="6" fail="0" id="s1" name="Atest">Atest</stat>

tests/resources/my_dummy_handlers/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from oxygen import BaseHandler
2+
3+
4+
class MyDummyHandler(BaseHandler):
5+
'''
6+
A test handler that throws mismatch argument exception if single argument
7+
is passed with multiple accepted
8+
'''
9+
10+
def run_my_dummy_handler(self, result_file):
11+
return result_file
12+
13+
def parse_results(self, result_file, foo='foo'):
14+
return {
15+
'name': result_file,
16+
'foo': foo
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from oxygen import BaseHandler
2+
3+
4+
class MyDummyHandler(BaseHandler):
5+
'''
6+
A test handler for unfolding parse_results arguments
7+
if it has multiple parameters
8+
'''
9+
10+
def run_my_dummy_handler(self, result_file):
11+
return result_file, 'foo'
12+
13+
def parse_results(self, result_file, foo):
14+
return {
15+
'name': result_file,
16+
'foo': foo
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from oxygen import BaseHandler
2+
3+
4+
class MyDummyHandler(BaseHandler):
5+
'''
6+
A test handler that throws mismatch argument exception because
7+
parse_results expects too many arguments
8+
'''
9+
10+
def run_my_dummy_handler(self, result_file):
11+
return result_file, 'foo'
12+
13+
def parse_results(self, result_file, foo, bar):
14+
return {
15+
'name': result_file,
16+
'foo': foo,
17+
'bar': bar
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from oxygen import BaseHandler
2+
3+
4+
class MyDummyHandler(BaseHandler):
5+
'''
6+
A test handler for passing tuple if parse_results accepts one parameter
7+
'''
8+
9+
def run_my_dummy_handler(self, result_file):
10+
return result_file, 'foo'
11+
12+
def parse_results(self, result_file):
13+
return {
14+
'name': result_file
15+
}

tests/utest/helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
tags: ZAP
2626
accepted_risk_level: 2
2727
required_confidence_level: 1
28+
oxygen.my_dummy_handler:
29+
handler: MyDummyHandler
30+
keyword: run_my_dummy_handler
31+
tags: MY_DUMMY_HANDLER
2832
'''
2933

3034
RESOURCES_PATH = Path.cwd() / 'tests' / 'resources'

tests/utest/my_dummy_handler/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)