![](/img/trans.png)
[英]Can we use CSV file in scenario hooks like in before feature to get data?
[英]How can I use @before=Createuser() and @after=deleteuser() from feature file with methods in hooks
嘗試在 hook 后面添加自定義代碼,這些代碼將按照我們作為 @before 或 @after 傳遞的順序執行
@tag @before=CreateUser() @after=DeleteUser()
Scenario: [scenario name]
Given [context]
When [action]
Then [outcome]
CreateUser() 和 DeleteUser() 將是 Hooks.cs
我怎樣才能做到這一點?
[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");
}
}
正如 Greg Burghardt 所建議的那樣,帶鈎子的作用域綁定似乎是最好的主意。 但是你需要你的鈎子知道你想使用標簽調用的這些方法。
如果您堅持希望擁有一個更動態(可以這么說)的系統,您實際上將方法名稱放在標簽中,您可以擁有一個通用的鈎子方法,該方法通過標簽,在_scenarioContext.ScenarioInfo.Tags
中作為字符串數組提供並找到堅持您選擇的某些預定義模式的那些,例如。 @before=(.*)
。 然后你將它們傳遞給
obj.GetType().GetMethod(methodNameFromTag).Invoke(obj, null);
其中obj
是 class 的實例,其中包含這些方法( CreateUser()
、 DeleteUser()
等)。
我看到這種方法的優點:
我看到的缺點:
obj
) (注意:在 SpecFlow 中,要訪問場景的所有標簽,包括從特征文件繼承的標簽,您必須使用_scenarioContext.ScenarioInfo.ScenarioAndFeatureTags
)。
[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");
}
}
由此產生的小黃瓜:
@tag
Scenario: [scenario name]
Given [context]
When [action]
Then [outcome]
如果標簽不夠,還可以在[Scope]
屬性中使用功能標題和場景標題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.