0

I have a suite of SpecFlow tests that are using the MSTest framework. I run them via Microsoft Test manager. What I want to know is if I can get it to attach a file to the run.

My code generates a HTML file that i'd like attached so that users can go into the results for test in MTM and simply open it up.

I saw a previous question had:

TestContext.AddResultFile(testPassedFile);

But when I tried to add TestContext to my "[AfterScenario]" method it doesnt have a method called AddResultFile.

Does anyone know how I might be able to achieve adding this HTML file to the results.

Festivejelly
  • 670
  • 2
  • 12
  • 30

2 Answers2

2

AFAIK there is no way of accessing the TestContext from within the StepBindings:
Access TestContext in SpecFlow Step Binding class

The only way I see is to write your own generator for the tests so that the generated test-code writes the TestContext for example to SpecFlow's ScenarioContext, so that you can access it from the step bindings.

If you want to take all that hassle, you might take a look at https://jessehouwing.net/specflow-custom-unit-test-generator/.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
paulroho
  • 1,234
  • 1
  • 11
  • 27
  • I see, thats a shame. Is there a way to attach a comment in the "comments" section of the Analysis? or even a note? I figured at least then I can link to my HTML page. – Festivejelly Jul 30 '14 at 15:16
  • What do you mean by 'the "comments" section of the Analysis'? – paulroho Jul 31 '14 at 10:54
  • Sorry, bad explaining by me.... In MTM theres a summary section which in turn has a comments section. Its basically a text box. – Festivejelly Jul 31 '14 at 12:11
  • I've no clue about MTM. But since it might get it's displayed information via the TestContext, I fear the problem will be the same. – paulroho Jul 31 '14 at 15:56
  • It wouldn't be that hard to write your own custom `UnitTestGeneratorPlugin` and `UnitTestGeneratorProvider`. There was an SO Question and Answer that had a small example that can get you started, but I closed the tab; but here's the [GitHub page for it](https://github.com/marksl/Specflow-MsTest/tree/master/MyGenerator.Generator.SpecflowPlugin). I started writing one (with the help of MSDN and CodeDom) and got quite far until I realized I didn't need it for what I was trying to do. But it's not hard. Also Bing (or google or whatever) `specflow UnitTestGenerator`. You'll find a ton of examples. – fourpastmidnight Aug 25 '14 at 03:15
  • Found that [SO Question](http://stackoverflow.com/questions/12218020/specflow-classinitialize-and-testcontext). Again the link to the GitHub page in my comment above is the full source code presented in the afore-linked SO Q&A. – fourpastmidnight Aug 25 '14 at 03:46
0

In SpecFlow 3, you can get the TestContext via content injection, and store a reference to it, which can then be accessed in the step definitions:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechTalk.SpecFlow;

[Binding]
public class Context
{
    private static TestContext test_context;

    [BeforeScenario]
    private static void SetContext(ScenarioContext context)
    {
        test_context = context.ScenarioContainer.Resolve<TestContext>();
    }

    public static void Attach(string file_name)
    {
        test_context.AddResultFile(file_name);
    }
}
Jeremy Meadows
  • 2,314
  • 1
  • 6
  • 22