我最近遇到一个要求,我必须在测试步骤之前进行一些预处理。我必须对JUnit 5(使用Jupiter引擎)和Cucumber框架执行此操作。我已经使用BeforeEachCallback
和@extendWith
注释对JUnit 5(因为它支持扩展)做了同样的事情。
但是我正在为黄瓜做同样的事情。在黄瓜中,有没有一种方法可以注册在调用每个步骤之前都会被调用的回调方法?我了解@Before
钩子,但我希望这种方法是一种通用的通用方法,将在所有功能的每一步之前被调用。
如下所示:
public void preProcesser() {
//This method should be called with method context before each step
//by method context I mean the targetMethod name (in JUnit I can easily get it from context.getTargetMethod()
}
请问有人可以给我一些指导/意见吗?
答案 0 :(得分:0)
是的,您可以在功能文件的开头添加一个Background
步骤,该步骤作为前提条件并在以下所有方案之前运行。
@Tag
Feature: Test Feature
Background: Prerequisite step
Given I perform some task
Then navigate to home page
Scenario: Test Scenario
Then I perform other tests
...
所有方案的执行流程将为-
Given I perform some task
Then navigate to home page
Then I perform other tests