How to list all keywords from a .robot file which have no test cases

Viewed 670

Problem statement: I have a .robot file which contains a lot of keywords. this is a higher level robot file which does not contains any test cases. I want to list all the keywords name.

What i tried so far ?

from robot.parsing.model import TestData
suite = TestData(parent=None,source="Track2_Keywords.robot")

it gives error

raise NoTestsFound('File has no tests or tasks.') robot.parsing.populators.NoTestsFound: File has no tests or tasks.

also i tried:

from robot.parsing.model import KeywordTable
suite = KeywordTable("Track2_Keywords.robot")
for item in suite:
...     print (item.name)

but its empty.

2 Answers

First, you need to use the ResourceFile model rather than TestData or KeywordTable. Second, you must call the populate method to get the keywords to be visible. It is the populate method that actually reads the file and imports the keywords.

from robot.parsing.model import ResourceFile

rf = ResourceFile("Track2_Keywords.robot")
rf.populate()
for kw in rf.keywords:
    print(kw.name)

I don't understand what is your goal. If the file does not contain tests, then it is a Resource file. In this case you can add a dummy test case like for example:

*** Test Cases ***
Dummy Test
    No Operation

This way, your code should not complaint about missing tests.

-- If you want to find non used keywords in a test suite, you can use that existing feature in RIDE (https://github.com/robotframework/RIDE/).

Related