API to get tests from robot file based on include/exclude tags

763 views
Skip to first unread message

Andrew Palmer

unread,
Jan 18, 2016, 4:32:50 PM1/18/16
to robotframework-users
Hi All,

In the past I've used some of the robot API to get test information from a file. I'm currently trying to grab all tests in a single or multiple robot test files based on tags. Is there any builtin API to do this? I've searched around and haven't seen anything aside from being able to grab tags from a specific test.

My current solution is to just execute a dryrun and then grab the tests out of the output.xml:

pybot --dryrun --include TAG1andTAG2orTAG3 --exclude TAG4 path/to/file.robot
grep 'test_id' output.xml      # Then parse for the test name in each line.

Thanks,
Andy

Joseph Lorenzini

unread,
Jan 18, 2016, 7:55:04 PM1/18/16
to robotframework-users
Hi Andrew,

There are a couple ways to go about doing that using the robot api. You can use the tidy api to temporarily convert the files into CSV and parse them using the python CSV module or you can use the robot framework's own module for parsing test files. Example below to give the general idea:


from __future__ import print_function
import sys
from robot.parsing.model import TestData
import os
from os import path
import re

def parse_data(robotfile):
    suite = TestData(parent=None, source=robotfile)
    testcases = suite.testcase_table.tests
    for t in testcases:
        tags = t.tags.value
        if tags:
            print('testcase:',t.name,',tags:',tags)

rootdir = sys.argv[1]
for  (dirpath, dirnames, filenames) in os.walk(rootdir):
    for f in filenames:
        if re.search('.robot$',f):
            parse_data(path.join(dirpath,f))


Pekka Klärck

unread,
Jan 19, 2016, 5:34:19 PM1/19/16
to palmer...@gmail.com, robotframework-users
Hello,

Joseph already proposed using the parsing API and that certainly
works. Using the execution model is probably easier, though:

from robot.api import TestSuiteBuilder, SuiteVisitor

class TestPrinter(SuiteVisitor):
def visit_test(self, test):
print test.name

suite = TestSuiteBuilder().build('path/to/tests.robot')
suite.filter(included_tags=['list', 'of', 'includes'],
excluded_tags=['excludes'])
suite.visit(TestPrinter())

For more information about the APIs used above see
http://robot-framework.readthedocs.org/.

Cheers,
.peke
> --
> You received this message because you are subscribed to the Google Groups
> "robotframework-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to robotframework-u...@googlegroups.com.
> To post to this group, send email to robotframe...@googlegroups.com.
> Visit this group at https://groups.google.com/group/robotframework-users.
> For more options, visit https://groups.google.com/d/optout.



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

Andrew Palmer

unread,
Jan 20, 2016, 12:43:08 AM1/20/16
to robotframework-users, palmer...@gmail.com
Peke, Joseph, appreciate the comments!

The TestSuiteBuilder seemed to do the trick, in a fraction of time and lines I was previously using.

One question, is that in our current framework we support and/or operators for tags. So for example, one could do:
pybot --dryrun --include thisORthat path/to/test.robot

It seems the example below uses just and logic, so my question is, is it possible to support both? Such as tag1ANDtag2ORtag3?

Thank you,
Andy

Andrew Palmer

unread,
Jan 20, 2016, 12:55:40 AM1/20/16
to robotframework-users, palmer...@gmail.com
Actually I just figured it out, was as simple as just using that logic in the include list:

included_tags=['thisANDthatORnothing']

Thank you again, very helpful!

Andy
Reply all
Reply to author
Forward
0 new messages