如何在不启动实际测试的情况下获取 Robot Framework 中的测试用例列表?

How to get test cases list in Robot Framework without launching the actual tests?

我有包含测试用例的文件 test.robot。

如何在不激活测试的情况下从命令行或python获取此测试用例的列表?

您可以查看testdoc tool。就像文档中解释的那样,"The created documentation is in HTML format and it includes name, documentation and other metadata of each test suite and test case".

机器人测试套件很容易用机器人解析器解析:

from robot.parsing.model import TestData
suite = TestData(parent=None, source=path_to_test_suite)
for testcase in suite.testcase_table:
    print(testcase.name)

对于 v3.2 及更高版本:

在 RobotFramework 3.2 the parsing APIs have been rewritten 中,Bryan Oakley 的回答将不再适用于这些版本。

与 pre-3.2 和 post-3.2 版本兼容的正确代码如下:

from robot.running import TestSuiteBuilder
from robot.model import SuiteVisitor


class TestCasesFinder(SuiteVisitor):
    def __init__(self):
        self.tests = []

    def visit_test(self, test):
        self.tests.append(test)


builder = TestSuiteBuilder()
testsuite = builder.build('testsuite/')
finder = TestCasesFinder()
testsuite.visit(finder)

print(*finder.tests)

进一步阅读: