2

I am trying to run a SpecFlow scenario from code instead of through Test Explorer or the command line. 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.

EDIT on why I want to do this:
We want to run specific scenarios from TFS. It is very cumbersome to connect TestMethods to WorkItems in TFS, because:

  • 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 Scenario Outlines get practically unusable, while they are a very powerful feature.

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.

AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • I think it is a bit tricky to get a scenario running from code since you have to setup a testrunner environment which - I think - could be a bit complex. Can you briefly explain what you want to achieve by doing this? Maybe there is a more simple way to solve your problem instead of running a Scenario by code. – realtime Mar 28 '14 at 11:34
  • @realtime, thank you for asking, I added an explanation. – AutomatedChaos Mar 28 '14 at 12:14
  • Thanks for your explanation! Sorry, this currently runs a bit off-topic. But I do not get the sense behind connecting WorkItems to TestMethods. If I have a WorkItem, it addresses a certain bug, maybe a feature i want to have. Then I want to have an isolated test case which addresses exclusively this issue. This alone helps in traceability a lot! An single test case is (in my world) equal to a feature file. So I connect features (or files) to WorkItems. Changes of Scenario Names do not care anymore and you have a lot less trace-connections to maintain. – realtime Apr 29 '14 at 21:09

1 Answers1

1

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)
  2. Find their MethodInfos with a TestAttribute and DescriptionAttribute (aka Scenarios)
  3. Store them to a Dictionary
  4. Call them by "Title of the Feature/Title of the Scenario" with Activator.CreateInstance and Invoke

You have to set the (private) field "testRunner" according to your needs of course.

Andulanus
  • 21
  • 3