Skip to main content
All CollectionsBehave Pro ClassicBuild Tools
SpecFlow for Visual Studio and .NET
SpecFlow for Visual Studio and .NET

Export features and scenarios for test automation for Visual Studio and .NET using MSBuild

Alan Parkinson avatar
Written by Alan Parkinson
Updated over 2 years ago

We are currently aware that SpecFlow Runner will be retired soon with the release of .NET 6, as a result we are currently working on alternative support for customers, we will update any concerned customers we know of and update the the documentation when we have a resoloution in place. Read more, here.

Behave Pro can export the created Feature and Scenarios to any gherkin compatible test automation tool. For Visual Studio/.NET based projects we have a plugin for MSBuild that integrates into the build process. We host our plugin on the NuGet gallery, and it is available through the NuGet Packet Manager Shell:

PM> Install-Package Hindsight.Behave

Alternatively, you can use the built in NuGet Gallery interface from inside Visual Studio

Using within a script

You can use the Jira Connector within your own C# code to download features from Behave:

using Hindsight.Behave;

public class Demo
{
    public void Download()
    {
        JiraConnector jc = new JiraConnector("https://behave.pro");
        jc.Fetch(
            // The project id of the target project
            project: "10100",
            // The API Key username
            username: "Cloud userId OR Jira Server Username",
            // The API Key value
            password: "Cloud API key OR Jira Server Password",
            // Directory to extract features to (relative to script)
            directory: "features",
            // Boolean whether to include manual tests
            manual: true,
            // Boolean whether to verify the SSH certificate
            verify: true,
            // Boolean whether to return feature files that
            // are compatible with NUnit.
            isNUnit: true
        );
    }
}

Note: For Behave Pro Cloud the above URL should be used, but for Jira Server you need to use your full Jira Server URL.

This will download all features from project ID 10100 into the features director, including manual tagged scenarios.

Using with MSBuild

You can use the Behave assembly as an MSBuild task as part of your build process. You can modify the build process for a project by “unloading” it in Visual Studio (right click the project in the solution explorer and click “Unload Project”) and then right clicking and clicking “Edit {PROJECT}.csproj”, or editing the .csproj file directly in the directory for the project. Create a new reference to the Behave.dll assembly, which will be in your project after using NuGet to install it:

<UsingTask
    TaskName="Behave"
    AssemblyFile="$(NuGetPackageRoot)\Hindsight.Behave.0.2.0\lib\net45\Behave.dll" />

The Behave.dll is available under the following framework targets: net20, net35, net45, and net46.
If $(NuGetPackageRoot) is not working for you, you can use the following:

<UsingTask
    TaskName="Behave"
    AssemblyFile="..\packages\Hindsight.Behave.0.2.0\lib\net45\Behave.dll" />

To use with NET Core 2.0 (netstandard2.0), the UsingTask parameter is slightly different:

<UsingTask
    TaskName="Behave"
    AssemblyFile="$(NuGetPackageRoot)\hindsight.behave\0.2.0\lib\netstandard2.0\Behave.dll" />

You can then use the Behave task to fetch features, either in one of the existing BeforeBuild or AfterBuild target blocks, or by defining your own as we have below:

For NET Framework:

<Target Name="BeforeBuild">
    <Behave
        host="https://behave.pro"
        project="10900"
        username="Cloud userId OR Jira Server Username"
        password="Cloud API key OR Jira Server Password"
        directory="Features\"
        manual="true" />
</Target>

For NET Core:

<Target Name="BeforeBuilds" AfterTargets="Build">
    <Behave
        host="https://behave.pro"
        project="10900"
        username="Cloud userId OR Jira Server Username"
        password="Cloud API key OR Jira Server Password"
        directory="Features\"
        manual="true" />
</Target>

The task has four required parameters, host , project , username  and password . These 4 parameters are different depending on if you are using Jira Cloud or Server. You can retrieve the correct details the Jira project's admin area; 'Project Settings' > 'Behave Pro' > 'Generate config'. This article has more information on finding these configuration items

Add nunit=“true” to the task options if you need to use the NUnit runner. This strips characters from tags (traits) that NUnit doesn’t work with.

Step by Step with Visual Studio 2017

This guide will take you from a new solution through to integrating Behave Pro with SpecFlow, and running the feature files.

Visual Studio setup

Install SpecFlow for Visual Studio 2017 and NUnit 3 Test Adapter from the Extensions and Updates dialog in the Tools menu, and restart Visual Studio when prompted.

Project setup

Create a new project as NET Framework (or NET Core 2.0). 

Install the Hindsight.Behave, SpecFlow, SpecFlow.NUnit, NUnit, NUnit.Runners and NUnitTestAdapter into your solution, using the NuGet Package Manager from the Tools menu.

Configuring MSBuild task

Unload the project, in order to edit the .csproj file (which is the MSBuild configuration used to build that particular project).

In the .csproj file, uncomment the last section of the root Project element which defines Targets, and add the below code:

...
<UsingTask TaskName="Behave"
             AssemblyFile="..\packages\Hindsight.Behave.0.2.0\lib\net45\Behave.dll" />
  <Target Name="BeforeBuild">
    <Behave host="https://behave.pro"
            project="11500"
            username="..."
            password="..."
            directory="Features\"
            manual="true"
            verify="true"
            nunit="true" />
  </Target>

Note: For Behave Pro Cloud the above URL should be used, but for Jira Server you need to use your full Jira Server URL.

Then, reload the project from the Project Explorer.

Fetching and including Feature files from Behave Pro

By building the Specs project, the Feature files will be fetched from Behave Pro. They will not initially be visible in the project explorer, so if you click “Show All Files” you can include the Features directory into the project.

Generating step definitions

Follow the SpecFlow Getting Started guide to generate Step definitions for all the .feature files that were fetched. If you are using the latest SpecFlow, the definitions will be generated automatically:

Filtering which scenarios to run

The build tool will download all the features for the project to your local machine. In order to only run certain tests (e.g. by tag, by assignee, by issue), switch the Test Explorer view to Traits.

Did this answer your question?