How do you execute a test script in Robot Framework?

There are several ways to execute test scripts in Robot Framework. Here's a breakdown of the most common methods:

1. From the command line:

This is the most common way to execute Robot Framework tests. You use the robot command followed by the path to your test suite or test case file.

* Running a single test case file :

robot my_test_case.robot

* Running all test cases in a directory (test suite) :

robot my_test_suite_directory/

* Running multiple test case files or directories :

robot test_case_1.robot test_suite_directory/ test_case_2.robot

* Running tests based on tags :

robot -i tag1 -i tag2 my_test_suite_directory/  # Include tests with tag1 AND tag2
robot -e tag3 my_test_suite_directory/  # Exclude tests with tag3

* Setting variables from the command line :

robot -v my_variable:my_value my_test_case.robot

* Specifying the output directory :

robot -d results my_test_suite_directory/

* Generating logs and reports: Robot Framework automatically generates HTML reports and logs. You can customize their location and names.

2. Using the ride command (if using RIDE):

If you're using the RIDE IDE, you can execute tests directly from the RIDE interface. RIDE provides a graphical interface for running tests, viewing results, and debugging. You typically right-click on a test suite or test case and select "Run."


3. Programmatically (using the Robot Framework API):

You can embed Robot Framework execution within your Python code. This is useful for integrating Robot Framework with other tools or for more complex execution scenarios.

from robot.api import run_from_cli

if __name__ == '__main__':
    run_from_cli(['my_test_case.robot'])  # Pass arguments as a list
    # Or run_from_cli(['-d', 'results', 'my_test_suite_directory/'])


4. Using a build tool (e.g., Jenkins, Maven, Gradle):

For continuous integration and continuous delivery (CI/CD), you'll often integrate Robot Framework execution into your build process using tools like Jenkins, Maven, or Gradle. These tools allow you to automate test execution as part of your build pipeline.

Key options and considerations:

  • -d (outputdir): Specifies the directory where Robot Framework should write its output files (logs, reports, etc.).
  • -i (include): Runs tests whose tags match the given pattern.
  • -e (exclude): Excludes tests whose tags match the given pattern.
  • -v (variable): Sets a variable that can be used in your tests.
  • -l (log level): Sets the log level (e.g., DEBUG, INFO, WARN, ERROR).
  • -r (report): Sets the name of the generated report file.
  • -g (log): Sets the name of the generated log file.
  • Parallel execution: Robot Framework supports parallel test execution to speed up test runs, especially for large test suites. You can use the --processes or --threads options.