Coder Social home page Coder Social logo

totaltest / specflow.contrib.variants Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 5.0 511 KB

SpecFlow plugin to allow variants of a test to be run using tags e.g. against different browsers

License: MIT License

C# 93.88% Smalltalk 2.19% Gherkin 3.93%
specflow test-automation nunit mstest xunit selenium webdriver bdd specflow-plugin

specflow.contrib.variants's Introduction

Build Status Azure DevOps tests Nuget (with prereleases)

SpecFlow.Contrib.Variants

SpecFlow plugin to allow variants of a test to be run using tags. For example (but not limited to) running scenarios or features against different browsers if performing UI tests. Supports MsTest, NUnit and xUnit

1. SpecFlow v3+ notes

In line with SpecFlow's docs, it is required that one of the following unit test providers package is installed (apart from SpecRun which is not supported by this plugin):

  • SpecFlow.xUnit
  • SpecFlow.MsTest
  • SpecFlow.NUnit

It is also recommended that specflow.json is used over app.config. When using this plugin however, app.config is also supported for .net framework projects. Details about specific configuration is explained further below.
Note that only specflow.json is supported in .net core projects so app.config can't be used for those. Original docs can be found here: SpecFlow Configuration

2. SpecFlow v2.4 notes

As this version of SpecFlow only works with app.config, the details for configuration if using this version can be found below.

3. Usage

3.1 Installation

Install plugin using Nuget Package Manager

PM> Install-Package SpecFlow.Contrib.Variants

3.2 Overview

Feature variant tags mean each scenario within that feature is run for each variant.
i.e 4 test cases for the below two scenarios:

@Browser:Chrome
@Browser:Firefox
Feature: AnExampleFeature

Scenario: Simple scenario
	Given something has happened
	When I do something
	Then the result should be something else

Scenario: Simple scenario two
	Given something has happened
	When I do something
	Then the result should be something else


Scenario variant tags mean the scenario is run for each of its variants.
i.e 3 test cases for the below scenario:

Feature: AnExampleFeature

@Browser:Chrome
@Browser:Firefox
@Browser:Edge
Scenario: Simple scenario
	Given something has happened
	When I do something
	Then the result should be something

3.3 Access the variant

The variant key/value can then be accessed via the ScenarioContext static or injected class. This decision was made to cater for all supported test frameworks (NUnit, MsTest and xUnit).

[Binding]
public sealed class Hooks
{
    private readonly ScenarioContext _scenarioContext;
    private IWebDriver _driver;

    public Hooks(ScenarioContext scenarioContext)
    {
        _scenarioContext = scenarioContext;
    }

    [BeforeScenario]
    public void BeforeScenario()
    {
        _scenarioContext.TryGetValue("Browser", out var browser);

        switch (browser)
        {
            case "Chrome":
                _driver = SetupChromeDriver();
                break;
            case "Firefox":
                _driver = SetupFirefoxDriver();
                break;
            default:
                _driver = SetupChromeDriver();
                break;
        }
        _scenarioContext.ScenarioContainer.RegisterInstanceAs(_driver);
    }
    ...
}

It's also possible to use the in built contexts per test framework if desired (doesn't apply to xUnit, which is why ScenarioContext is recommended):

MsTest

var browser = TestContext.Properties["Browser"];


NUnit

var categories = TestContext.CurrentContext.Test.Properties["Category"];
var browser = categories.First(a => a.ToString().StartsWith("Browser").ToString().Split(':')[1];

See the integration test projects for full example.

4. Configuration

4.1 SpecFlow v3+

specflow.json
The default variant key is 'Variant' if nothing specific is set. This means the tag @Variant:Chrome will be treated as a variant, where 'Chrome' is the variant value. However, the variant key can be customised in the specflow.json file:

{
  "pluginparameters": {
    "variantkey": "Browser"
  }
}

The above means that only tags that begin with @Browser: will be treated as variants.

An example can be found here

app.config
If using app.config (applicable only for .net framework), the custom variant key can be set in the following generator element and path attribute:

<configSections>
  <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
</configSections>
<specFlow>
  <generator path="VariantKey:Browser" />
</specFlow>

This isn't the ideal element to use but was the best possibility we had, the path value is only treated as a variant if it starts with 'VariantKey:' meaning the generator element can be still be used as originally intended.

An example can be found here

4.2 SpecFlow v2.4 (app.config)

Specify the plugin name and ensure the type is set to 'Generator'. The variant key can also be a custom value, the default key is 'Variant' if no parameters value is specified.

e.g.

<specFlow>
  <unitTestProvider name="xunit" />
  <plugins>
    <add name="SpecFlow.Contrib.Variants" type="Generator" parameters="Browser" />
  </plugins>
</specFlow>

The above will ensure the plugin is used and that 'Browser' is set as the variant key. This means any tags starting with @Browser: will be treated as variants.

A colon should be used as the seperator between the variant key and value. For example @Browser:Chrome will mean 'Chrome' is the variant value.

The unitTestProvider can either be xunit, mstest or nunit.

License

This project uses the MIT license.

specflow.contrib.variants's People

Contributors

dependabot[bot] avatar totaltest avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

specflow.contrib.variants's Issues

Could not load TechTalk.SpecFlow.Generator

We wanted to use your plugin together with xunit, playwright and net6 and the scenario generation for multiple tests works well, but as soon as I run a test, I get this error when I start the virtual service.

One or more errors occurred. (Unable to load one or more of the requested types.
Could not load file or assembly 'TechTalk.SpecFlow.Generator, Version=3.9.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41'. The system cannot find the file specified.

I can not seam to find this nuget or a class of it anywhere. I also found some issues on SO that mention to rebuild and reinstall the nuget but all of that did not help. Does someone else also have this issue and could point me to a solution? It feels like some nuspec is configured wrong... If I uninstall the dependency to
SpecFlow.Contrib.Variants the test cases dont work anymore but the I do not the the exception above.

Variant not displayed in test explorer VS Mac 2022

Hi,
So, I have been trying to figure out what is going on and where the problem lies.
I updated my projects to net7 and lates VS Mac and updated all nugets to the latest net7 compatible.
After that I found that my UI project tests no longer displayed the variant in the explorer.

Please see my old stack question for some screen shots.

It appears the "old" way I note in my stack post (adding a scenarios table) works still, but the variant plugin does not.

Is this an issue with your code or something the VS people removed?

Thanks for any info.

featureContext returns null value for variant

Hi there,

I am trying to use this to get a variant of a feature tag on one of my specflow tests but it keeps giving me null.

Using SpecFlow.NUnit 3.5.5 and SpecFlow.Contribs.Variants 3.5.5

I have an example feature like

@Variant:Android_HighSpec
@Variant:iOS_HighSpec
@Variant:Mac_HighSpec
@Variant:Windows_HighSpec
Feature: Adding Numbers

Scenario: Add two numbers
Given I have the calculator open
When I add 2 + 3
Then the result is 5

Then in my FeatureHooks I have something like...

[BeforeFeature(Order = 0)]
public static void BeforeFeature(FeatureContext featureContext, IObjectContainer container)
{
// hooks code i want to run...
featureContext.TryGetValue("Variant", out string variant);
}

variant always returns null though no matter what :(

I have set my specflow.json accordingly too.

I just tested same setup but with scenarioContext instead of featureContext and scenarioContext correctly got the variant value, but featureContext does not.

using .net core 3.1

Tables in scenario steps cause code generator to emit repeated table header rows rather than content rows

Steps to reproduce

  • Create a feature containing a scenario with a table
  • Build the project
  • Inspect the generated code

Expected
The generated code builds a table with one header and a row for each input content row

Actual
The generated code builds a table with one header but repeats the header for each input data row rather than using the values in the content row

Example:

Scenario: Add some numbers
    Given these numbers:
        | left | right |
        | 1    | 1     |
        | 2    | 2      |
    When added
    Then the result is:
        | answer |
        | 2       |
        | 4       |

produces:

[Xunit.FactAttribute(DisplayName="Add some numbers")]
        [Xunit.TraitAttribute("FeatureTitle", "Data Store operations")]
        [Xunit.TraitAttribute("Description", "Add some numbers")]
        public virtual void AddSomeNumbers()
        {
            TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Add some numbers", null, ((string[])(null)));
            this.ScenarioInitialize(scenarioInfo);
            this.ScenarioStart();
            TechTalk.SpecFlow.Table table9 = new TechTalk.SpecFlow.Table(new string[] {
                        "left",
                        "right"});
            table9.AddRow(new string[] {
                        "left",
                        "right"});
            table9.AddRow(new string[] {
                        "left",
                        "right"});
            testRunner.Given("these numbers:", ((string)(null)), table9, "Given ");
            testRunner.When("added", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
            TechTalk.SpecFlow.Table table10 = new TechTalk.SpecFlow.Table(new string[] {
                        "answer"});
            table10.AddRow(new string[] {
                        "answer"});
            table10.AddRow(new string[] {
                        "answer"});
            testRunner.Then("the result is:", ((string)(null)), table10, "Then ");
            this.ScenarioCleanup();
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.