【问题标题】:Show time in console output of robotframework在机器人框架的控制台输出中显示时间
【发布时间】:2021-01-05 17:21:05
【问题描述】:

是否可以在机器人框架的控制台输出中显示时间。通常输出是这样的。

==============================================================================
Example test suite
==============================================================================
First test :: Possible test documentation                             | PASS |
------------------------------------------------------------------------------
Second test                                                           | FAIL |
Error message is displayed here
==============================================================================
Example test suite                                                    | FAIL |
2 critical tests, 1 passed, 1 failed
2 tests total, 1 passed, 1 failed
==============================================================================

我想查看有关测试在控制台中运行了多长时间的信息。我尝试在机器人命令中添加选项 --console verbose,但没有发现任何变化。

您可以在输出的报告中看到此信息,但如果您没有达到该点并且只想查看哪个测试正在减慢测试套件的执行速度。

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    我认为默认记录器不可能做到这一点。但是您可以使用listener interface 并创建自己的记录器。

    例如,listener interface 3 中的 end_test 和 end_suite 方法提供了一个 result argument,它是一个具有各种属性的对象,例如 starttime、endtime 和 elapsedtime。您可能对这些感兴趣。

    一个简短的例子如何使用它:

    time_loger.py

    ROBOT_LISTENER_API_VERSION = 3
    
    def end_test(data, result):
        print('Test started: ' + result.starttime);
        print('Test ended: ' + result.endtime);
    

    我执行测试的方式是只有我的新记录器才会将消息打印到控制台:

    $ robot --listener time_logger.py --console quiet test.robot
    

    这给了我这个输出:

    Test started: 20210105 10:17:30.377
    Test ended: 20210105 10:17:32.380
    

    【讨论】:

      【解决方案2】:

      您可以创建一个小的listener,使用end_test 方法可以获取执行期间的开始时间、结束时间和经过的时间。您可以将它们从侦听器打印到控制台。

      ROBOT_LISTENER_API_VERSION = 3
      
      def end_test(data, result):
          print(f'\nStart time: {result.starttime}')
          print(f'End time: {result.endtime}')
          print(f'Elapsed time: {result.elapsedtime}')
      

      例子:

      *** Test Case ***
      Test case 1
          Sleep    1 sec
          
      Test case 2
          Sleep    2 sec
      
      Test case 3
          Sleep    3 sec
          Fail    Do a fail.
      
      Test case 4
          Sleep    4 sec
      

      像这样执行:robot --listener listener.py test.robot

      控制台输出:

      ==============================================================================
      Test
      ==============================================================================
      Test case 1
      Start time: 20210105 10:19:47.711
      End time: 20210105 10:19:48.712
      Elapsed time: 1001
      | PASS |
      ------------------------------------------------------------------------------
      Test case 2
      Start time: 20210105 10:19:48.712
      End time: 20210105 10:19:50.714
      Elapsed time: 2002
      | PASS |
      ------------------------------------------------------------------------------
      Test case 3
      Start time: 20210105 10:19:50.715
      End time: 20210105 10:19:53.716
      Elapsed time: 3001
      | FAIL |
      Do a fail.
      ------------------------------------------------------------------------------
      Test case 4
      Start time: 20210105 10:19:53.716
      End time: 20210105 10:19:57.719
      Elapsed time: 4003
      | PASS |
      ------------------------------------------------------------------------------
      Test                                                                  | FAIL |
      4 critical tests, 3 passed, 1 failed
      4 tests total, 3 passed, 1 failed
      ==============================================================================
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-30
        • 2018-04-25
        • 2020-01-12
        相关资源
        最近更新 更多