TestAutomatisering & PerformanceTesten

SpecFlow Extensies voor Dummies, de code

Hieronder vind je de code die behoort bij het artikel SpecFlow Extensies voor Dummies

.

.

 Feature File:

Feature: Table Transformations
  In order to have convenient datatypes
  As a SpecFlow user
  I want to transform tables into dictionaries or datatables

Scenario: Transform a vertical table into a dictionary
  Given I have the following table:
    | Movie      | Rating |
    | The Matrix | 8.5    |
    | It         | 8.1    | 
  When I transform this table into a dictionary
  Then the dictionary should have a key "The Matrix" with a value "8.5"
  And the dictionary should have a key "It" with a value "8.1"

Scenario: Transform a table into a datatable
  Given I have the following table:
    | Movie      | Rating | Director          |
    | The Matrix | 8.5    | The Wachowskis    |
    | It         | 8.1    | Tommy Lee Wallace |
  When I transform this table into a datatable
  Then the datatable should have the following record:
    | Column   | Value          |
    | Movie    | The Matrix     |
    | Rating   | 8.5            |
    | Director | The Wachowskis |
  And the datatable should have the following record:
    | Column   | Value             |
    | Movie    | It                |
    | Rating   | 8.1               |
    | Director | Tommy Lee Wallace | 

Steps Definition:

using System.Collections.Generic;
using System.Linq;
using TechTalk.SpecFlow;
using SpecFlowExamples.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;

namespace SpecFlowExamples.Steps
{
    [Binding]
    public class TableSteps
    {
        [Given(@"I have the following table:")]
        public void GivenIHaveTheFollowingTable(Table table)
        {
            ScenarioContext.Current.Add("table", table);
        }

        [When(@"I transform this table into a dictionary")]
        public void WhenITransformThisTableIntoADictionary()
        {
            var table = (Table)ScenarioContext.Current["table"];
            var dictionary = table.ToDictionary();
            ScenarioContext.Current.Add("dictionary", dictionary);
        }

        [Then(@"the dictionary should have a key ""(.*)"" with a value ""(.*)""")]
        public void ThenTheDictionaryShouldHaveAKeyWithAValue(string key, string value)
        {
            var dictionary = (Dictionary<string, string>)ScenarioContext.Current["dictionary"];
            Assert.IsTrue(dictionary.ContainsKey(key), "The dictionary contains key " + key);
            Assert.AreEqual(value, dictionary[key]);
        }
 
        [When(@"I transform this table into a datatable")]
        public void WhenITransformThisTableIntoADatatable()
        {
            var table = (Table)ScenarioContext.Current["table"];
            var dataTable = table.ToDataTable();
            ScenarioContext.Current.Add("datatable", dataTable);
        }

        [Then(@"the datatable should have the following record:")]
        public void ThenTheDatatableShouldHaveTheFollowingRecord(Table table)
        {
            var dataTable = (DataTable)ScenarioContext.Current["datatable"];
            var expectedRecord = table.ToDictionary();
            foreach (DataRow row in dataTable.Rows)
            {
                var matchingRowFound = true;
                foreach (var keyValuePair in expectedRecord)
                {
                    var column = keyValuePair.Key;
                    var value = keyValuePair.Value;
                    if (!value.Equals(row[column]))
                    {
                        matchingRowFound = false;
                        break;
                    }
                }
                if (matchingRowFound)
                    return;
            }
            Assert.Fail("The table does not contain the record.");
        }
    }
}

SpecFlow.Table Extension:

using System.Collections.Generic;
using System.Linq;
using TechTalk.SpecFlow;
using SpecFlowExamples.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;

namespace SpecFlowExamples.Steps
{
    [Binding]
    public class TableSteps
    {
        [Given(@"I have the following table:")]
        public void GivenIHaveTheFollowingTable(Table table)
        {
            ScenarioContext.Current.Add("table", table);
        }

        [When(@"I transform this table into a dictionary")]
        public void WhenITransformThisTableIntoADictionary()
        {
            var table = (Table)ScenarioContext.Current["table"];
            var dictionary = table.ToDictionary();
            ScenarioContext.Current.Add("dictionary", dictionary);
        }

        [Then(@"the dictionary should have a key ""(.*)"" with a value ""(.*)""")]
        public void ThenTheDictionaryShouldHaveAKeyWithAValue(string key, string value)
        {
            var dictionary = (Dictionary<string, string>)ScenarioContext.Current["dictionary"];
            Assert.IsTrue(dictionary.ContainsKey(key), "The dictionary contains key " + key);
            Assert.AreEqual(value, dictionary[key]);
        }
 
        [When(@"I transform this table into a datatable")]
        public void WhenITransformThisTableIntoADatatable()
        {
            var table = (Table)ScenarioContext.Current["table"];
            var dataTable = table.ToDataTable();
            ScenarioContext.Current.Add("datatable", dataTable);
        }

        [Then(@"the datatable should have the following record:")]
        public void ThenTheDatatableShouldHaveTheFollowingRecord(Table table)
        {
            var dataTable = (DataTable)ScenarioContext.Current["datatable"];
            var expectedRecord = table.ToDictionary();
            foreach (DataRow row in dataTable.Rows)
            {
                var matchingRowFound = true;
                foreach (var keyValuePair in expectedRecord)
                {
                    var column = keyValuePair.Key;
                    var value = keyValuePair.Value;
                    if (!value.Equals(row[column]))
                    {
                        matchingRowFound = false;
                        break;
                    }
                }
                if (matchingRowFound)
                    return;
            }
            Assert.Fail("The table does not contain the record.");
        }
    }
}
Dit bericht is geplaatst in Testautomatisering en getagd in SpecFlow Test Automatisering Testautomatisering

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

Nieuws

Blijf op de hoogte

TNW 2023

26/06/2023

Het Next Web (TNW) Conference is een jaarlijks evenement dat professionals uit de wereld van technologie, innovatie en ondernemerschap samenbrengt. Dit jaar vond TNW 2023 plaats in Amsterdam (eigenlijk Zaandam). Ik kreeg de kans om aanwezig te zijn en wil graag mijn ervaringen delen in deze blogpost. Het centrale thema van dit tweedaagse evenement was […]

06/06/2023

Work hard, play hard! Dat laatste hebben we vrijdag 2 juni letterlijk genomen. We zijn naar een museum geweest, het Nationaal Videogame Museum in het stadshart van Zoetermeer. Voor gamefanaten is dit echt een feestje. We kregen een interessante rondleiding waarbij we van alles te weten kwamen over de ontwikkeling van videogames, vanaf de vroege […]

testRigor

17/05/2023

Zijn we klaar voor AI-gestuurde testautomatiseringstools? Een blik op testRigor. Bij PerformanceArchitecten vinden we het belangrijk om continu onze kennis te ontwikkelen en bij te houden. Daarom organiseren we elke maand een kennismiddag waarbij we ons verdiepen in verschillende onderwerpen die relevant zijn voor ons werk. Onze laatste kennismiddag stond in het teken van testRigor. […]

SRE – Site Reliability Engineering

19/04/2023

Tijdens de laatste maandelijkse kennismiddag van PerformanceArchitecten hebben we ons verdiept in het onderwerp SRE (Site Reliability Engineering). SRE combineert software engineering en operationele principes om grootschalige en complexe systemen te bouwen en beheren.   Ter introductie hebben we verschillende video’s bekeken waarin de definitie van SRE werd gegeven en het verschil tussen SRE en […]

NeoLoad RealBrowser

02/04/2023

Bij PerformanceArchitecten vinden we het belangrijk om continu onze kennis te ontwikkelen en bij te houden. Daarom organiseren we elke maand een kennismiddag, waarbij we ons verdiepen in verschillende onderwerpen die relevant zijn voor ons werk. Onze laatste kennismiddag stond in het teken van het RealBrowser “protocol” binnen het performancetesttool NeoLoad van Tricentis. Voor degenen […]

JMeter en Groovy

06/03/2023

Als je binnen JMeter iets wilt doen wat niet standaard in de tool zit, gebruik je plugins of ga je zelf code schrijven. In het geval dat je zelf code gaat schrijven, wordt aangeraden de groovy functie en JSR223 sampler te gebruiken. Ik wil hieronder graag bespreken welke mogelijkheden deze 2 opties je bieden, maar […]

Kennismiddag: ChatGPT

19/02/2023

Tijdens de maandelijkse kennismiddag op 14 februari bespraken collega’s de ontwikkelingen rondom ChatGPT, een tool die AI gegenereerde resultaten kan opleveren. Het werd onderzocht of ChatGPT ondersteuning kon bieden bij SEO en scripting. Hoewel de tool potentieel heeft, is verdere input en domeinkennis nog steeds nodig. ChatGPT is geen perfecte oplossing, maar kan wel handig zijn bij de uitvoering van werk. Het was een geslaagde middag en er wordt uitgekeken naar nieuwe onderwerpen voor de volgende kennismiddag.

16/09/2022

Performancetesten overdragen naar teams We zien bij onze klanten een duidelijke verschuiving van verantwoordelijkheden van centrale teams naar DevOps teams. “You build it, you run it” zorgt ervoor dat teams niet alleen zelf software moeten bouwen, testen en in productie brengen, maar ook verantwoordelijk zijn voor hoe de applicatie in productie draait. Eén van de […]

Azure Bicep

09/12/2021

Introductie Binnen de Azure omgeving is er veel mogelijk zoals bijvoorbeeld het runnen van pipelines en het creëren van infrastructuur. Maar om van deze mogelijkheden gebruik te kunnen maken zijn er uiteraard resources nodig. Deze resources zijn manueel via de portal aan te maken, maar ook door gebruik te maken van ARM (Azure Resource Manager) […]

Azure DevOps pipeline performance quality gate

31/10/2021

In mijn vorige blogs heb ik geschreven over Gatling simulaties in een executable jar en Azure DevOps pipeline met gatling. In het laatste blog had ik al aangegeven dat ik de performancetest onderdeel wilde laten zijn van de CD pipeline door het toevoegen van quality gates. Oftewel, ik wil dat de resultaten van de performance […]

Introductie in Pytest

07/04/2021

Inleiding Voor mijn huidige project ben ik bezig met het aspect kwaliteit in combinatie met Big Data. In zo’n modern project wordt hierbij ook AI gebruikt. Het inrichten van AI wordt vaak niet gedaan door ontwikkelaars, maar door data scientists die een omgeving nodig hebben wat het AI werk kan doen. De programmeertaal Python is […]

Website performance, belangrijker dan je denkt

27/11/2020

Inleiding We kennen het allemaal: je bent aan het surfen om iets te kopen. Duurt het langer dan 1 à 2 seconden dan haak je al snel af. Desondanks is performance is vaak onderbelicht in web-ontwikkeling. De nadruk ligt vaak op functionaliteit en minder op performance. Dat terwijl een slechte performance behoorlijke impact heeft op […]