机器人框架中每个测试的全局测试挂钩或全局测试拆卸

Global test hook or global test teardown for each test in robot framework

我想要某种挂钩机制运行宁before/after每次测试。 例如,如果我将 ARGUMENT=1 传递给 pybot 或为每个测试传递 python,我想使用 运行 关键字,而不是修改我项目中的全部测试。

可能吗?

类似的方式,你可以创建一个使用黄瓜框架的前或后钩子,但在这里。我还不明白。

谢谢。

我猜您正在寻找: Suite setup and Suite teardown

或者 Test setup and Test teardown

套件设置示例:

*** Settings ***
Library                         DatabaseLibrary
Force Tags                      UI  FINAL
Resource  ${CURDIR}${/}..${/}..${/}resources${/}keywords.robot
Suite setup  Run Keywords       Restore database
...                             Prepare database
Suite teardown  Run Keywords    Close All Browsers
...                             Restore database 

*** Keywords ***
Prepare database
    Connect to DB
    Execute Sql Script  ${CURDIR}${/}Setup_td.sql
    Disconnect From Database

Restore database
    Connect to DB
    Execute Sql Script  ${CURDIR}${/}Teardown_td.sql
    Disconnect From Database

我自己找到了答案。 您应该创建新的侦听器以用于测试挂钩。

关于机器人监听器: Robot Framework Listener Interface

# -*- coding: utf-8 -*-
from robot.libraries.BuiltIn import BuiltIn


class global_hooks(object):
    """
        Global scope library listener 
        as global hook mechanism.
    """

    ROBOT_LISTENER_API_VERSION = 3
    ROBOT_LIBRARY_SCOPE = "GLOBAL"

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self

    def __log_variables(self):
        """
            Example private function. 
        """
        if BuiltIn().get_variable_value(name='${SOME_VAR}', default=False):
            BuiltIn().run_keyword(name=self.log_test_variables.__name__)

    def end_test(self, data, result):
        """ The `end test` hook """

        self.__log_variables()

    def log_test_variables(self):
        """
            Keyword for showing up all variables in the test context.
        """

        BuiltIn().log_variables(level='INFO')