MSTest

Reqnroll supports MsTest V2 or later (NuGet Version 2.2.8 or higher).

Note

MsTest V4 is supported from Reqnroll v3.2 onwards.

Documentation for MSTest can be found here.

Needed NuGet Packages

For Reqnroll: Reqnroll.MSTest

For MSTest: MSTest.TestFramework

For Test Discovery & Execution:

Accessing TestContext

You can access the MsTest TestContext instance in your step definition or hook classes by constructor injection:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[Binding]
public class MyStepDefs
{
    private readonly TestContext _testContext;
    public MyStepDefs(TestContext testContext) // use it as ctor parameter
    { 
        _testContext = testContext;
    }

    [Given("a step")]
    public void GivenAStep()
    {
        //you can access the TestContext injected in the ctor
        _testContext.WriteLine(_testContext.TestRunDirectory);
    }


    [BeforeScenario()]
    public void BeforeScenario()
    {
        //you can access the TestContext injected in the ctor
        _testContext.WriteLine(_testContext.TestRunDirectory);
    } 
}

In the static BeforeTestRun/AfterTestRun hooks you can use parameter injection:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[Binding]
public class Hooks
{
    [BeforeTestRun]
    public static void BeforeTestRun(TestContext testContext)
    {
        //you can access the TestContext injected as parameter
        testContext.WriteLine(testContext.TestRunDirectory);
    }

    [AfterTestRun]
    public static void AfterTestRun(TestContext testContext)
    {
        //you can access the TestContext injected as parameter
        testContext.WriteLine(testContext.DeploymentDirectory);
    }
}

Tags on Examples - Workaround

The MsTest Generator MsTest does not support applying tags (categories) to specific entries of parameterized tests, see issues 4089 and issues 1043

In short, tags on Examples are not send to the test execution. So @Test and @Acceptance are not available for test filtering/reporting/etc.

Scenario: Sample Scenario  
   Given sample step

@Test
Examples:  
| User   |  
| Tester |

@Acceptance
Examples:  
| User   |  
| Acc    |

The workaround for now is to disable the row tests. Note that this does impact how tests names are displayed:

{
  "$schema": "https://schemas.reqnroll.net/reqnroll-config-latest.json",
  // add the line below
  "generator": {"allowRowTests" :  false},

  "bindingAssemblies": [
  ]
}

Tags for TestClass Attributes

The MsTest Generator can generate test class attributes from tags specified on a feature.

Owner

Tag:

@Owner:John

Output:

[Microsoft.VisualStudio.TestTools.UnitTesting.OwnerAttribute("John")]

Priority

Tag:

@Priority:1

Output:

[Microsoft.VisualStudio.TestTools.UnitTesting.PriorityAttribute(1)]

Remarks:

The attribute is generated only when the value is a valid integer (valid means supported by int.TryParse)

WorkItem

Tag:

@WorkItem:123

Output:

[Microsoft.VisualStudio.TestTools.UnitTesting.WorkItemAttribute(123)]

DeploymentItem

Example 1 : Copy a file to the same directory as the deployed test assemblies

Tag:

@MsTest:DeploymentItem:test.txt

Output:

[Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute("test.txt")]

Example 2 : Copy a file to a sub-directory relative to the deployment directory

Tag:

@MsTest:DeploymentItem:Resources\DeploymentItemTestFile.txt:Data

Output:

[Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute("Resources\\DeploymentItemTestFile.txt", "Data")]