Test template using :FOR loop

1,585 views
Skip to first unread message

Piotr Nestorow

unread,
Sep 1, 2011, 10:42:29 AM9/1/11
to robotframework-devel
Hi

What is the correct syntax for a test template with a :FOR loop
when using the free text notation?

What is the problem with the following example (test_examp.txt):
---------------------------------------------------------------------------------------------

*** Test Cases ***
Device Component Baseline Information [Template] Validate Device
Component Baseline Information
:FOR ${dc} IN RANGE 3
${dc}

*** Keywords ***
Validate Device Component Baseline Information [Arguments] ${which
dc}
[Documentation] Example implementation
Log ${which dc}

-----------------------------------------------------------------------------------------------


Trying the above with:
C:\IronPython\Lib\site-packages\robot\runner.py test_examp.txt
Results:
==============================================================================
Test
Examp
==============================================================================
Device Component Baseline Information
| FAIL |
FOR loop contains no keywords.
------------------------------------------------------------------------------
Test Examp
| FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================

Robert Spielmann

unread,
Sep 1, 2011, 3:31:03 PM9/1/11
to piotr.n...@gmail.com, robotframework-devel
Hi Piotr,

this is more a question for robotframework-users, nevertheless here's
an answer :) The following works:

*** Test Cases ***
Device Component Baseline Information

:FOR ${dc} IN RANGE 3

\ Validate Device Component Baseline Information ${dc}

*** Keywords ***
Validate Device Component Baseline Information
[Arguments] ${whichdc}
[Documentation] Example implementation
Log ${whichdc}

Note the \ in the line after the :FOR.

I think this special requirement for plain text syntax should be added
to the documentation, or at least an example. I'll create an issue for
that.

Regards,
Robert

2011/9/1 Piotr Nestorow <piotr.n...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "robotframework-devel" group.
> To post to this group, send email to robotframe...@googlegroups.com.
> To unsubscribe from this group, send email to robotframework-d...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/robotframework-devel?hl=en.
>
>

Piotr Nestorow

unread,
Sep 2, 2011, 4:31:50 AM9/2/11
to robotframework-devel
Tanks Robert!
Your suggestion helps.

There are still problems though and that's the reason this topic
belongs in the development group.

1. I want to use [Template] for a data driven test case.
The idea is of course that I don't want the test case to stop when a
particular iteration fails.
2. Also I need the number of iterations to be computed on the fly
before the loop.

The following example shows the idea
- Note that 'Device Component Baseline Information' works but requires
the very technical
'Run Keyword And Continue On Failure'
- 'Device Component Baseline Information Template' does not work
- As a side note: In the [Template] solution I'd like to have a test
case generated for every loop iteration.
This would improve our test case statistics :)

------------------------------------------------------------------------------
*** Test Cases ***
Device Component Baseline Information
${dcs} = Number Of Device Components
:FOR ${dc} IN RANGE ${dcs}
\ Run Keyword And Continue On Failure Validate DC ${dc}

Device Component Baseline Information Template
${dcs} = Number Of Device Components
[Template] Validate DC
:FOR ${dc} IN RANGE ${dcs}
\ ${dc}

*** Keywords ***
Number Of Device Components
[Documentation] Returns the number of DCs
[Return] 3

Validate DC [Arguments] ${which dc}
[Documentation] Validate DC baseline information
Log ${which dc}
---------------------------------------------------------------------------------------------------------
Results from execution:

==============================================================================
Test
Examp
==============================================================================
Device Component Baseline Information
| PASS |
------------------------------------------------------------------------------
Device Component Baseline Information Template
| FAIL |
Non-existing variable '${dcs}'.
------------------------------------------------------------------------------
Test Examp
| FAIL |
2 critical tests, 1 passed, 1 failed
2 tests total, 1 passed, 1 failed
==============================================================================

Regards


On 1 Sep, 21:31, Robert Spielmann <spielmann.rob...@googlemail.com>
wrote:
> Hi Piotr,
>
> this is more a question for robotframework-users, nevertheless here's
> an answer :) The following works:
>
> *** Test Cases ***
> Device Component Baseline Information
>    :FOR  ${dc}  IN RANGE  3
>    \  Validate Device Component Baseline Information  ${dc}
>
> *** Keywords ***
> Validate Device Component Baseline Information
>   [Arguments]  ${whichdc}
>   [Documentation]  Example implementation
>    Log  ${whichdc}
>
> Note the \ in the line after the :FOR.
>
> I think this special requirement for plain text syntax should be added
> to the documentation, or at least an example. I'll create an issue for
> that.
>
> Regards,
> Robert
>
> 2011/9/1 Piotr Nestorow <piotr.nesto...@gmail.com>:
>
>
>
> > Hi
>
> > What is the correct syntax for a test template with a :FOR loop
> > when using the free text notation?
>
> > What is the problem with the following example (test_examp.txt):
> > ---------------------------------------------------------------------------­------------------
>
> > *** Test Cases ***
> > Device Component Baseline Information  [Template]  Validate Device
> > Component Baseline Information
> >    :FOR  ${dc}  IN RANGE  3
> >         ${dc}
>
> > *** Keywords ***
> > Validate Device Component Baseline Information  [Arguments]  ${which
> > dc}
> >    [Documentation]  Example implementation
> >    Log   ${which dc}
>
> > ---------------------------------------------------------------------------­--------------------
>
> > Trying the above with:
> > C:\IronPython\Lib\site-packages\robot\runner.py test_examp.txt
> > Results:
> > ===========================================================================­===
> > Test
> > Examp
> > ===========================================================================­===
> > Device Component Baseline Information
> > | FAIL |
> > FOR loop contains no keywords.
> > ---------------------------------------------------------------------------­---
> > Test Examp
> > | FAIL |
> > 1 critical test, 0 passed, 1 failed
> > 1 test total, 0 passed, 1 failed
> > ===========================================================================­===
>
> > --
> > You received this message because you are subscribed to the Google Groups "robotframework-devel" group.
> > To post to this group, send email to robotframe...@googlegroups.com.
> > To unsubscribe from this group, send email to robotframework-d...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/robotframework-devel?hl=en.- Dölj citerad text -
>
> - Visa citerad text -

Pekka Klärck

unread,
Sep 4, 2011, 6:16:14 PM9/4/11
to piotr.n...@gmail.com, robotframework-devel, robotframework-users
2011/9/2 Piotr Nestorow <piotr.n...@gmail.com>:

> Tanks Robert!
> Your suggestion helps.
>
> There are still problems though and that's the reason this topic
> belongs in the development group.

This topic has actually been discussed on rf-users recently so I'll
add also that list to Cc. Possible future replies can, in my opinion,
be sent only to rf-users.

> 1. I want to use [Template] for a data driven test case.
> The idea is of course that I don't want the test case to stop when a
> particular iteration fails.

That ought to work out-of-the-box already.

> 2. Also I need the number of iterations to be computed on the fly
> before the loop.
>
> The following example shows the idea
> - Note that 'Device Component Baseline Information' works but requires
> the very technical
> 'Run Keyword And Continue On Failure'
> - 'Device Component Baseline Information Template' does not work
>

> ------------------------------------------------------------------------------
> *** Test Cases ***
> Device Component Baseline Information
>        ${dcs} =  Number Of Device Components
>        :FOR  ${dc}  IN RANGE  ${dcs}
>        \   Run Keyword And Continue On Failure  Validate DC  ${dc}
>
> Device Component Baseline Information Template
>        ${dcs} =  Number Of Device Components
>        [Template]  Validate DC
>        :FOR  ${dc}  IN RANGE  ${dcs}
>        \   ${dc}
>
> *** Keywords ***
> Number Of Device Components
>        [Documentation]  Returns the number of DCs
>        [Return]  3
>
> Validate DC  [Arguments]  ${which dc}
>        [Documentation]  Validate DC baseline information
>        Log  ${which dc}


The problem is that you try to have a workflow (getting a variable and
then using it) in a test case that has a template. That doesn't work,
but the preconditions can be done in test or suite setup. For example
this could work for you:

*** Test Cases ***
Device Component Baseline Information Template

[Setup] Set DCS Variable
[Template] Validate DC
:FOR ${dc} IN RANGE @{DCS}
\ ${dc}

***Keywords***
Set DCS Variable


${dcs} = Number Of Device Components

Set Test Variable \@{DCS} ${dcs}


> - As a side note: In the [Template] solution I'd like to have a test
> case generated for every loop iteration.
> This would improve our test case statistics :)

This would be rather hard to implement and I'm nor sure everyone would
like that (at least I wouldn't). If you want to have separate tests
for all iterations, you should not use templates but just have
separate tests instead. I know that you cannot then use for loops in
the data like this, but it would be pretty simple to generate the
whole test data based e.g. some model.

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Reply all
Reply to author
Forward
0 new messages