Overview
In this tutorial, we will create a simple Specflow test in C#, using Nunit as test runner.
Notes
Although this tutorial explores a way of managing Specflow tests "in JIRA", it does no take advantage of Xray's Cucumber features.
Therefore, in this case, JIRA isn't used to make the BDD specification; only to abstract the Tests. Tests in JIRA wil be created as Generic Tests and not Cucumber Tests. This is, since the semantic of Cucumber tests is lost, such are also, for example, the Scenario Outline examples related results.
Description
Specflow is a tool used for BDD in C#.
In this case, we'll start by writing The test case validates a Calculator class and exploits some Nunit features, such as the ability of validating the same Test against multiple input values, and also the possibility of linking Tests with requirements in JIRA through the usage of Nunit's Test attributes.
Feature: Calculator In order to avoid silly mistakes As a math idiot I want to be told the arithmetic operation of two numbers @mytag Scenario: Add two numbers Given I have entered 50 into the calculator And I have also entered 70 into the calculator When I press add Then the result should be 120 on the screen Scenario: Multiply two numbers Given I have entered 2 into the calculator And I have also entered 3 into the calculator When I press multiply Then the result should be 6 on the screen Scenario Outline: Amazing addition of two numbers Given I have entered <input_1> into the calculator And I have also entered <input_2> into the calculator When I press add Then the result should be <output> on the screen Examples: | input_1 | input_2 | output | | 20 | 30 | 50 | | 30 | 50 | 80 |
using Microsoft.VisualStudio.TestTools.UnitTesting; using TechTalk.SpecFlow; [Binding] public sealed class CalculatorSteps { private int result { get; set; } private Calculator calculator = new Calculator(); [Given(@"I have entered (.*) into the calculator")] public void GivenIHaveEnteredIntoTheCalculator(int number) { calculator.FirstNumber = number; } [Given(@"I have also entered (.*) into the calculator")] public void GivenIHaveAlsoEnteredIntoTheCalculator(int number) { calculator.SecondNumber = number; } [When(@"I press add")] public void WhenIPressAdd() { result = calculator.Add(); } [When(@"I press multiply")] public void WhenIPressMultiply() { result = calculator.Multiply(); } [Then(@"the result should be (.*) on the screen")] public void ThenTheResultShouldBeOnTheScreen(int expectedResult) { Assert.AreEqual(expectedResult, result); } }
After running successfuly the Scenarios and generating the NUnit XML report (e.g. TestResult.xml), it can be imported to Xray (either by the REST API or through "Import Execution Results" action within the Test Execution).
nunit3-console bin\Debug\UnitTestProject2.dll
curl -H "Content-Type: multipart/form-data" -u admin:admin -F "file=@TestResult.xml" "http://localhost:8080/rest/raven/1.0/import/execution/nunit?projectKey=CALC"
Nunit's Test Case is mapped to a Generic Test in JIRA, and the "Generic Test Definition" field contains the name of the class, and the method name that implements the Test Case.
The Execution Details of the Generic Test contains information about the Test Suite, which in this case corresponds to the Test Case class.
References