Force tags using the name of the folder of the test cases

698 views
Skip to first unread message

Laurent Bristiel

unread,
Jan 16, 2013, 4:47:28 PM1/16/13
to robotframe...@googlegroups.com
Hello all,

I organize my test cases in folder and test cases files :
- folder1
   - tc1.1
   - tc1.2
- folder2
   - tc 2.1

Folder names are the topics covered by the test cases they contain.
And I would like this information to also be stored as a tag for every test case.
So I would like to have tc1.1 and tc1.2 to have "folder1" as tag.

The best way I found so far is to have initialization file (__init__.txt) in every folder with a line :
Force Tags        folder_name

My question is : is there a way to automatically retrieve the name of the folder ?
I saw in the doc that there are automatic variables, but none is "folder_name"
(http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.5#automatic-variables)
I can not use ${SUITE_NAME} because I get the name of the tc file, not the one of the folder.

Only thing I could think of is to use  ${SUITE SOURCE} and extract the tc's parents folder name but I don't see how I could do this in the "force tags" keyword.

Anyone having any idea ?

Thanks,
Laurent

Pekka Klärck

unread,
Jan 16, 2013, 5:58:02 PM1/16/13
to lau...@bristiel.com, robotframe...@googlegroups.com
2013/1/16 Laurent Bristiel <lau...@bristiel.com>:
>
> The best way I found so far is to have initialization file (__init__.txt) in
> every folder with a line :
> Force Tags folder_name
>
> My question is : is there a way to automatically retrieve the name of the
> folder ?
> I saw in the doc that there are automatic variables, but none is
> "folder_name"
> (http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html?r=2.7.5#automatic-variables)
> I can not use ${SUITE_NAME} because I get the name of the tc file, not the
> one of the folder.
>
> Only thing I could think of is to use ${SUITE SOURCE} and extract the tc's
> parents folder name but I don't see how I could do this in the "force tags"
> keyword.

Probably the easiest solution is setting these tags using variables
and creating the variables in a variable file. If you have the
variable file in the same directory as your test suite, you shouldn't
need more than

import os.path
FOLDER_NAME = os.path.basename(os.path.dirname(__file__))

in your variable file and then you could use ${FOLDER NAME} variable
in files taking the variable file into use.

Things are a bit more difficult if you want to have only one variable
file and use that from various test suites in different directories.
In that case you could pass ${CURDIR} as an argument to the variable
file like

*** Settings ***
Variables path/to/foldername.py ${CURDIR}
Force Tags ${FOLDER NAME}

and then in your variable file have

import os.path

def get_variables(directory):
return {'FOLDER_NAME': os.path.basename(directory)}


A different solution is setting tags dynamically using `Set Tags`
keyword. If you use it in suite setup, all tests in that suite will
get those tags. A benefit of this solution is that you don't need
extra files. Having something like this ought to work:

*** Settings ****
Suite Setup Set Folder Name Tags
Library OperatingSystem

*** Keywords ***
Set Folder Name Tags
${path} ${folder name} = Split Path ${CURDIR}
Set Tags ${folder name}


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

Laurent Bristiel

unread,
Jan 17, 2013, 10:43:29 AM1/17/13
to robotframe...@googlegroups.com, lau...@bristiel.com
Thanks for your quick and detailled answer peke.
I chosed the second solution because it was quicker for me to implement.
(I have not yet any variable file).

Result : it works fine, exactly as you were expecting.
I just lost some time because I used "test setup" to launch this new keyword.
But when I had a test setup on the test itself, this last one overwrote the former.
Using "suite setup" I don't have this problem, as the different levels of "suite setup" add up.

Thanks a lot,
Laurent

Laurent Bristiel

unread,
Jul 4, 2013, 8:49:15 AM7/4/13
to robotframe...@googlegroups.com, lau...@bristiel.com
Back with this 6 months old question...
I think I have an issue with this way of setting tags.
It looks like it does not work in conjunction with --include and --exlude
Maybe Robot is
1) building its perimeter of test case to run using --include --exclude dans tags already present on test cases
2) then executing the Suite Setup that Set Tags.
3) so my tc are beeing tagged in the report but not early enough to be included/excluded

Would that be the case ?

Thanks,
Laurent
Reply all
Reply to author
Forward
0 new messages