简体   繁体   English

如何使用钩子中的方法从功能文件中使用@before=Createuser() 和@after=deleteuser()

[英]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:我看到这种方法的优点:

  • hooks themselves can be oblivious of the methods you invoke钩子本身可以忽略你调用的方法
  • someone can add new tags and new methods related to them without touching the general hooks mechanism (possibly even a separate repo?)有人可以添加新标签和与之相关的新方法,而无需触及一般的钩子机制(甚至可能是单独的回购协议?)

Disadvantages I see:我看到的缺点:

  • hooks still need a reference to instance of a class with the methods you want to run ( obj )钩子仍然需要引用 class 的实例以及您要运行的方法 ( obj )
  • you need to setup some conventions, eg.您需要设置一些约定,例如。 pattern of tags, passing args to methods标签模式,将参数传递给方法
  • you need to handle exceptions eg.你需要处理异常,例如。 what if there's no method with a given name如果没有给定名称的方法怎么办
  • general brittleness comes to my mind when thinking of this想到这个时,我想到了一般的脆弱性

( 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.

相关问题 我们可以像 before 功能一样在场景挂钩中使用 CSV 文件来获取数据吗? - Can we use CSV file in scenario hooks like in before feature to get data? 继承SqlMembershipProvider并禁用CreateUser()和DeleteUser() - Inherit SqlMembershipProvider and disable CreateUser() and DeleteUser() 如何将ValueTuple命名功能与匿名方法一起使用? - How do I use the ValueTuple naming feature with anonymous methods? 如何为钩子使用参数化方法 - SpecFlow - How to use parameterized methods for hooks - SpecFlow 如何在编辑.aspx文件时使用Visual Studio的“代码片段”功能? - How can I use Visual Studio's “Code Snippet” feature while editing a .aspx file? 如何在Excel中使用FreezePanes功能而不会使线从轴点发出? - How can I use the FreezePanes feature in Excel without getting the lines emanating out from the axis point? 我如何在 C# 中使用方法? - How can i use methods in c#? 我怎样才能在boo中使用扩展方法 - How can i use Extension Methods in boo 如何从Webreference调用CreateUser方法 - How to call the CreateUser method from a webreference 我有两个项目的解决方案,如何在Winforms项目中使用库类型项目中的方法? - I have a solution with two projects how can i use methods from the library type project in the winforms project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM