Skip to content

Commit 60c75af

Browse files
committed
Test and doc that type conversion works with embedded args.
See robotframework#2890 and robotframework#2947.
1 parent 3707672 commit 60c75af

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*** Settings ***
2+
Suite Setup Run Tests ${EMPTY} keywords/type_conversion/embedded_arguments.robot
3+
Resource atest_resource.robot
4+
5+
*** Test Cases ***
6+
Types via annotations
7+
[Tags] require-py3
8+
Check Test Case ${TESTNAME}
9+
10+
Types via @keyword
11+
Check Test Case ${TESTNAME}
12+
13+
Types via defaults
14+
Check Test Case ${TESTNAME}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from robot.api.deco import keyword
2+
3+
4+
try:
5+
exec('''
6+
@keyword(name=r'${num1:\d+} + ${num2:\d+} = ${exp:\d+}')
7+
def add(num1: int, num2: int, expected: int):
8+
result = num1 + num2
9+
assert result == expected, (result, expected)
10+
''')
11+
except SyntaxError:
12+
pass
13+
14+
15+
@keyword(name=r'${num1:\d+} - ${num2:\d+} = ${exp:\d+}', types=(int, int, int))
16+
def sub(num1, num2, expected):
17+
result = num1 - num2
18+
assert result == expected, (result, expected)
19+
20+
21+
@keyword(name=r'${num1:\d+} * ${num2:\d+} = ${exp:\d+}')
22+
def mul(num1=0, num2=0, expected=0):
23+
result = num1 * num2
24+
assert result == expected, (result, expected)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
*** Settings ***
2+
Library EmbeddedArguments.py
3+
4+
*** Test Cases ***
5+
Types via annotations
6+
[Tags] require-py3
7+
1 + 2 = 3
8+
2 + 2 = 4
9+
10+
Types via @keyword
11+
2 - 1 = 1
12+
2 - 2 = 0
13+
14+
Types via defaults
15+
1 * 2 = 2
16+
2 * 2 = 4

doc/userguide/src/ExtendingRobotFramework/CreatingTestLibraries.rst

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,15 +1320,33 @@ __ `Using a custom keyword name`_
13201320

13211321
from robot.api.deco import keyword
13221322

1323-
@keyword('Add ${quantity:\d+} Copies Of ${item} To Cart')
1323+
@keyword('Add ${quantity:\d+} copies of ${item} to cart')
13241324
def add_copies_to_cart(quantity, item):
13251325
# ...
13261326

13271327
.. sourcecode:: robotframework
13281328

13291329
*** Test Cases ***
13301330
My Test
1331-
Add 7 Copies Of Coffee To Cart
1331+
Add 7 copies of coffee to cart
1332+
1333+
By default arguments are passed to implementing keywords as strings, but
1334+
automatic `argument type conversion`__ works if type information is specified
1335+
somehow. With Python 3 it is convenient to use `function annotations`__,
1336+
and alternatively it is possible to pass types to the `@keyword decorator`__:
1337+
1338+
.. sourcecode:: python
1339+
1340+
@keyword('Add ${quantity:\d+} copies of ${item} to cart',
1341+
types={'quantity': int})
1342+
def add_copies_to_cart(quantity: int, item):
1343+
# ...
1344+
1345+
__ `Argument types`_
1346+
__ `Specifying argument types using function annotations`_
1347+
__ `Specifying argument types using @keyword decorator`_
1348+
1349+
.. note:: Automatic type conversion is new in Robot Framework 3.1.
13321350

13331351
Communicating with Robot Framework
13341352
----------------------------------

0 commit comments

Comments
 (0)