【发布时间】:2019-05-12 04:34:33
【问题描述】:
我需要在每个测试用例之前执行一些关键字。 假设我有一个包含 4 个测试用例的 .robot 文件,我需要在执行这 4 个测试用例之前运行一个关键字 4 次。在 TestNG 中,我们可以使用 @BeforeTest 注解。我想知道可以从 Robot Framework 中使用什么来做到这一点?
谢谢。
【问题讨论】:
标签: selenium automation robotframework
我需要在每个测试用例之前执行一些关键字。 假设我有一个包含 4 个测试用例的 .robot 文件,我需要在执行这 4 个测试用例之前运行一个关键字 4 次。在 TestNG 中,我们可以使用 @BeforeTest 注解。我想知道可以从 Robot Framework 中使用什么来做到这一点?
谢谢。
【问题讨论】:
标签: selenium automation robotframework
Test Setup、Test Teardown、Test Timeout 关键字可用于指定在每个测试用例之前需要调用的函数。
- Test Setup 将分别在 Junit/Testng 中充当 @Before/@BeforeMethod
- Test Teardown 将在 JUnit/Testng 中充当 @After/@AfterMethod
- [Setup] Keyword - 如果您只想对那个测试用例执行@BeforeTest,将使用它。
请参考以下示例-
*** Settings ***
Library OperatingSystem
Suite Setup This Is Suite Startup Keyword
Suite Teardown This Is Suite TearDown Keyword
Test Setup This Is Before Test
Test Teardown This Is After Test
*** Keywords ***
This Is Suite Startup Keyword
Log To Console This Is Suite Startup Keyword
This Is Suite TearDown Keyword
Log To Console This Is Suite TearDown Keyword
This Is Before Test
Log To Console This Is Before Test
This Is After Test
Log To Console This Is After Test
This Is Special Execution Case
Log To Console This Is Special Execution Case
*** Test Cases ***
Test Case One
[setup] This Is Special Execution Case
Log To Console This Is My Test Case 1
Test Case Two
Log To Console This Is My Test Case 2
Test Case Three
Log To Console This Is My Test Case 3
有关详细信息,请参阅Robot Framework User Guide 部分初始化文件和 2.4.5 套件设置和拆卸。
【讨论】:
Test Setup and case level [Setup] 设置扩展您的示例——它们是TestNG 的@beforeTest 注释的类比。 Suite Setup 实际上是@beforeSuite 注解。
您可以使用 Robotframework Test Setup 设置来定义将在套件中的每个案例之前运行的关键字。
如果您想在特定情况下指定设置,[Setup] 很好 - 如果设置,它将覆盖套件级别的设置:
*** Settings ***
Test Setup Log this is ran for every case
*** Test Cases ***
Case 1
Do Something
Case 2
[Setup] Log Custom case setup
Do Something Else
Case 3
Do The Third Thing
当案例 1 和案例 3 运行时,您会在它们执行之前看到消息“这对每个案例都运行”,但对于案例 2 不是 - 它有一个覆盖设置,您会看到它的消息 ( “自定义案例设置”)
【讨论】:
以下是机器人框架中的关键字作为执行钩子的替代品。
┌────────────────┬───────────────────────┐
│ Robot Keyword │ TestNG Execution Hook │
├────────────────┼───────────────────────┤
│ Test Setup │ @BeforeTest │
│ Test Teardown │ @AfterTest │
│ Suite Setup │ @BeforeSuite │
│ Suite Teardown │ @AfterSuite │
└────────────────┴───────────────────────┘
【讨论】: