简体   繁体   English

从代码运行 SpecFlow 场景

[英]Run SpecFlow scenario from code

I am trying to run a SpecFlow scenario from code instead of through Test Explorer or the command line.我正在尝试从代码而不是通过测试资源管理器或命令行运行 SpecFlow 场景。 Has someone managed to do this?有没有人设法做到这一点?

From a scenario, I can extract the method name and test method with recursion, but I cannot run this scenario method.从场景中,我可以通过递归提取方法名称和测试方法,但我无法运行此场景方法。 It seems to need a proper initialize and teardown, but I could't manage to do this.它似乎需要适当的初始化和拆卸,但我无法做到这一点。

My first thought was to use the TechTalk.SpecFlow.TestRunner class, but it doesn't seem to have a scenario selection method.我的第一个想法是使用TechTalk.SpecFlow.TestRunner类,但它似乎没有场景选择方法。

EDIT on why I want to do this:编辑我为什么要这样做:
We want to run specific scenarios from TFS.我们想从 TFS 运行特定的场景。 It is very cumbersome to connect TestMethods to WorkItems in TFS, because:在 TFS 中将 TestMethods 连接到 WorkItems 非常麻烦,因为:

  • You can only assign one testmethod to one workitem您只能将一种测试方法分配给一个工作项
  • For each workitem you have to search the method name, with in itself is a hassle, because the list is very long with lots of specflow scenarios.对于每个工作项,您必须搜索方法名称,这本身就很麻烦,因为列表很长,有很多规范流场景。
  • When your specflow scenario gets a different name (which happens a lot), TFS cannot find the correct method anymore当您的 specflow 场景获得不同的名称时(这种情况经常发生),TFS 无法再找到正确的方法
  • Specflow Scenario Outlines get practically unusable, while they are a very powerful feature. Specflow Scenario Outlines 几乎无法使用,但它们是一个非常强大的功能。

I want to create a mechanism where each automated workitem gets the same method assigned.我想创建一种机制,让每个自动化工作项都分配相同的方法。 This method extracts the workitem id and search and executes the scenario(s) with this workitem tagged.此方法提取工作项 id 并搜索并执行带有此工作项标记的场景。

I had a similar problem since my tests have some dependencies between Scenarios (shame on me, but it saves tons of copy-paste lines per Feature file).我有一个类似的问题,因为我的测试在场景之间有一些依赖关系(我很惭愧,但它为每个功能文件节省了大量的复制粘贴行)。 In most cases I would stick to isolated Scenarios of course.在大多数情况下,我当然会坚持孤立的场景。

I used reflection我使用了反射

  1. Find all Types with a DescriptionAttribute (aka Features) 查找具有 DescriptionAttribute (又名特征)的所有类型
  2. Find their MethodInfos with a TestAttribute and DescriptionAttribute (aka Scenarios)使用 TestAttribute 和 DescriptionAttribute(又名场景)查找它们的 MethodInfos
  3. Store them to a Dictionary将它们存储到字典中
  4. Call them by "Title of the Feature/Title of the Scenario" with Activator.CreateInstance and Invoke使用 Activator.CreateInstance 和 Invoke 通过“功能标题/场景标题”调用它们

You have to set the (private) field "testRunner" according to your needs of course.当然,您必须根据您的需要设置(私有)字段“testRunner”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM