[英]How can I use @before=Createuser() and @after=deleteuser() from feature file with methods in hooks
Trying to add custom code behind hook which will be executed on order which we pass as @before or @after尝试在 hook 后面添加自定义代码,这些代码将按照我们作为 @before 或 @after 传递的顺序执行
@tag @before=CreateUser() @after=DeleteUser()
Scenario: [scenario name]
Given [context]
When [action]
Then [outcome]
CreateUser() and DeleteUser() will be Hooks.cs CreateUser() 和 DeleteUser() 将是 Hooks.cs
How can I achieve this?我怎样才能做到这一点?
[Binding]
public class MyHooks
{
[BeforeScenario]
public void SetupTestUsers(ScenarioContext scenarioContext)
{
Console.WriteLine("Hook");
}
public void CreateUser(ScenarioContext scenarioContext)
{
Console.WriteLine("User Created");
}
public void DeleteUser(ScenarioContext scenarioContext)
{
Console.WriteLine("User Delete");
}
}
As Greg Burghardt suggested, scoped bindings with hooks seem like the best idea.正如 Greg Burghardt 所建议的那样,带钩子的作用域绑定似乎是最好的主意。 But you need your hooks to be aware of these methods that you want to invoke using tags.但是你需要你的钩子知道你想使用标签调用的这些方法。
If you insist that you want to have a more dynamic (so to say) system where you actually put method names in tags, you can have a generalized hook method that goes through tags, available as an array of strings in _scenarioContext.ScenarioInfo.Tags
and finds the ones that stick to some predefined pattern of your choosing, eg.如果您坚持希望拥有一个更动态(可以这么说)的系统,您实际上将方法名称放在标签中,您可以拥有一个通用的钩子方法,该方法通过标签,在_scenarioContext.ScenarioInfo.Tags
中作为字符串数组提供并找到坚持您选择的某些预定义模式的那些,例如。 @before=(.*)
. @before=(.*)
。 Then you pass them to然后你将它们传递给
obj.GetType().GetMethod(methodNameFromTag).Invoke(obj, null);
where obj
is an instance of a class that contains these methods ( CreateUser()
, DeleteUser()
etc.).其中obj
是 class 的实例,其中包含这些方法( CreateUser()
、 DeleteUser()
等)。
Advantages I see of this approach:我看到这种方法的优点:
Disadvantages I see:我看到的缺点:
obj
)钩子仍然需要引用 class 的实例以及您要运行的方法 ( obj
)( Note: in SpecFlow, to access all tags of a scenario including ones inherited from feature file, you must use _scenarioContext.ScenarioInfo.ScenarioAndFeatureTags
). (注意:在 SpecFlow 中,要访问场景的所有标签,包括从特征文件继承的标签,您必须使用_scenarioContext.ScenarioInfo.ScenarioAndFeatureTags
)。
You should be able to combine hooks with scoped bindings to achieve what you want.您应该能够将钩子与范围绑定结合起来以实现您想要的。
[Binding]
public class MyHooks
{
[BeforeScenario]
public void SetupTestUsers(ScenarioContext scenarioContext)
{
Console.WriteLine("Hook");
}
[BeforeScenario]
[Scope(Tag = "tag")]
public void CreateUser(ScenarioContext scenarioContext)
{
Console.WriteLine("User Created");
}
[AfterScenario]
[Scope(Tag = "tag")]
public void DeleteUser(ScenarioContext scenarioContext)
{
Console.WriteLine("User Delete");
}
}
The resulting Gherkin:由此产生的小黄瓜:
@tag
Scenario: [scenario name]
Given [context]
When [action]
Then [outcome]
If tags are not sufficient, you can also use feature titles and scenario titles in the [Scope]
attribute.如果标签不够,还可以在[Scope]
属性中使用功能标题和场景标题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.