<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jack Vanlightly</title>
    <description>The latest articles on DEV Community by Jack Vanlightly (@vanlightly).</description>
    <link>https://dev.to/vanlightly</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F52972%2Fb48ef2c7-7646-4470-b0c7-3f32fbffef15.jpeg</url>
      <title>DEV Community: Jack Vanlightly</title>
      <link>https://dev.to/vanlightly</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vanlightly"/>
    <language>en</language>
    <item>
      <title>Continuous Testing and ATDD</title>
      <dc:creator>Jack Vanlightly</dc:creator>
      <pubDate>Mon, 08 Jan 2018 17:42:05 +0000</pubDate>
      <link>https://dev.to/vanlightly/continuous-testing-and-atdd-4op8</link>
      <guid>https://dev.to/vanlightly/continuous-testing-and-atdd-4op8</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/Vanlightly/for-sharing-code/tree/master/testing/specflow/MyApp"&gt;Code on GitHub.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Software and code quality requires more than just engineering discipline, it requires testing and measurement. In this article we’ll take a look at how Acceptance Test Driven Development (ATDD) and Continuous Testing go hand-in-hand to improve code quality, reduce time to market and reduce overall risk.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Acceptance_test%E2%80%93driven_development"&gt;ATDD&lt;/a&gt; has been around for awhile now but you’d be surprised by the number of people who still don’t know it. At its heart it means that requirements are executable tests. The business can define requirements, which can be executed by a test automation test framework to demonstrably prove that the delivered software meets the functional requirements. That sounds pretty abstract but don’t worry, we’ll end this post with working code and you’ll see for yourself what how this ends up as code.&lt;/p&gt;

&lt;p&gt;The amazing thing about ATDD is that everyone speaks the same language, Gherkin, and that we can automate the testing of the delivered software against those requirements. We’ll cover what Gherkin is too and how is gets translated into code. &lt;/p&gt;

&lt;p&gt;But making sure the software does what the client has asked is not the end of the story. Code quality is something that the client often does not see and is difficult to measure (see this resource by SeaLights about &lt;a href="https://www.sealights.io/test-metrics/measuring-software-quality-a-practical-guide/"&gt;measuring code quality&lt;/a&gt;). Sometimes it surfaces as a bug or instability, but often it simply shows itself by longer delivery times. It’s hard to measure and therefore hard to prioritize. If you can’t measure it or even define it then it can end up being ignored leading to greater and greater problems. That is where Continuous Testing comes in.&lt;/p&gt;

&lt;p&gt;Continuous Testing is the next evolution of automated testing. It means we know the current state of the quality of the code being delivered. It combines test executions, test coverage and a bunch of other metrics into a holistic view of your software. It also means that those automated tests of yours are integrated into your CI/CD pipeline and are run continuously.&lt;/p&gt;

&lt;p&gt;Used together you know that your software delivers the functionality requested by the client and that it meets the code quality standard necessary to make the software maintainable going forward.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Case For ATDD
&lt;/h2&gt;

&lt;p&gt;Your colleagues working in the day-to-day business side of your company do not speak the same language as you the developer. When your company asks for a new application or a new feature, they do so in their own language with business terminology mixed in. They have no idea that what they request is ambiguous or infeasible. So requirements end up ambiguous and lacking in the detail required to turn them into software. Not to mention the forgotten requirements. &lt;/p&gt;

&lt;p&gt;ATDD addresses this by writing down all requirements in a syntax called Gherkin. Gherkin is a language that is understandable by both the business and the developer. It is normal language but in a structure that allows it to be mapped to code.&lt;/p&gt;

&lt;p&gt;Because we can map it to code, we can automate it and create repeatable, automated tests that prove that the software conforms to a functional requirement. Non-functional requirements are another matter and out of scope. So ATDD brings all parties together in a common vision of the project and tools automate the conformance of this vision.&lt;/p&gt;

&lt;p&gt;I have worked with ATDD in two different teams and in my experience:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your developers are happy because they have rigorous requirements that have no room for misinterpretation or vagueness.&lt;/li&gt;
&lt;li&gt;your QA is happy because the boring and repetitive manual testing work has been automated, allowing them to work on exploratory and load testing.&lt;/li&gt;
&lt;li&gt;your client is happy because the software developed will more closely match what they expect and the overall quality is higher. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also time to market is quicker because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are less back and forths between developers and QA&lt;/li&gt;
&lt;li&gt;Test automation eliminates a lot of slow, manual testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article we are going to build some automated acceptance tests using Visual Studio 2017, MS Test and SpecFlow. The end result will be integration tests that can be executed like any other automated test in a testing pipeline. The differences are that the tests will be the requirements and written in plain English. &lt;/p&gt;

&lt;h2&gt;
  
  
  SpecFlow and Gherkin
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/cucumber/cucumber/wiki/Gherkin"&gt;Gherkin&lt;/a&gt; is the syntax we’ll use to describe our requirements. It allows us to use normal language that the business can understand but that can be hooked up to a test automation framework for execution. While we cover &lt;a href="http://specflow.org/"&gt;SpecFlow&lt;/a&gt; in this article, the important takeaway is how we use Gherkin. Gherkin is the syntax that all the ATDD frameworks use.&lt;/p&gt;

&lt;p&gt;If you have ever used the Arrange, Act, Assert (AAA) pattern for clean unit tests then Gherkin will come easily to you. Gherkin uses Given, When, Then which map onto to AAA directly. The Given describes the current state of the system, When describes the action to be taken and Then describes the expected result.&lt;/p&gt;

&lt;p&gt;Challenge: We have been tasked with creating the logic for our site login. Users login with a username and a password. In addition to testing that this basic login functionality works, we want to rate limit login attempts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1 - Add the SpecFlow extension
&lt;/h3&gt;

&lt;p&gt;As we are using Visual Studio 2017, we need to open the Extensions and Updates window and search for “SpecFlow for Visual Studio 2017” and add it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VUYjY25R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6oxd43uf4brudpqf2dt6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VUYjY25R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6oxd43uf4brudpqf2dt6.PNG" alt="SpecFlow for Visual Studio 2017"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2 - Add a test project
&lt;/h3&gt;

&lt;p&gt;Add a new Unit Test Project then add the NuGet package SpecFlow to the project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nBFOkWFQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ixabe15nfv5i0vsnxi3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nBFOkWFQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ixabe15nfv5i0vsnxi3p.png" alt="Add SpecFlow NuGet package"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally ensure that in the app.config we use the Mstest test framework.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;unitTestProvider name="Mstest" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could use xUnit or nUnit also.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3 - Create our first feature
&lt;/h3&gt;

&lt;p&gt;SpecFlow stores all the Gherkin tests in feature files. Go ahead click Add New Item and add a feature file to the project called BasicLogin.feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mO-q10xk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jlogbmaa6hi5l2ri3niz.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mO-q10xk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jlogbmaa6hi5l2ri3niz.PNG" alt="Add feature file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Replace the existing text with a new feature description and a single gherkin scenario:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature: LoginSuccess
Login usernames and passwords are evaluated against stored usernames and passwords and a login result is returned.

@loginSuccess
Scenario: Correct username and password produces a success response
    Given that the user 'john' exists and his password is 'monkey'
    When the username 'john' with password 'monkey' is supplied
    Then the result should be 'Success'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the scenario clearly describes the functional behaviour expected. This is easy for a business analyst and a programmer to understand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4 - Generate C# steps class
&lt;/h3&gt;

&lt;p&gt;Now we need to turn that Gherkin into executable code. Right click on some text in the feature file and click Generate Step Definitions. In the new window click Generate.&lt;/p&gt;

&lt;p&gt;You will now have a C# steps class without any logic, but with methods and their arguments bound to the gherkin Given, When, Then lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Binding&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BasicLoginSteps&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Given&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"that the user '(.*)' exists and his password is '(.*)'"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GivenThatTheUserExistsAndHisPasswordIs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ScenarioContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Pending&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;When&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"the username '(.*)' with password '(.*)' is supplied"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;WhenTheUsernameWithPasswordIsSupplied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ScenarioContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Pending&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"the result should be '(.*)'"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ThenTheResultShouldBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ScenarioContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Pending&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can run our Gherkin scenario and SpecFlow has hooked it all up and we get string arguments with the values of our Gherkin lines!&lt;/p&gt;

&lt;p&gt;Now we need to do something with it. In this toy example, credentials are stored in an in memory dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Binding&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BasicLoginSteps&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Given&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"that the user '(.*)' exists and his password is '(.*)'"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GivenThatTheUserExistsAndHisPasswordIs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;CredentialRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Credentials&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;CredentialRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Credentials&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;When&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"the username '(.*)' with password '(.*)' is supplied"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;WhenTheUsernameWithPasswordIsSupplied&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SutFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateLoginService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;ScenarioContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"the result should be '(.*)'"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ThenTheResultShouldBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AreEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ScenarioContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;Given&lt;/em&gt; method we store those credentials in our credential store. If this were real, here we would add those credentials to our real credential store, or mock them.&lt;/p&gt;

&lt;p&gt;In the &lt;em&gt;When&lt;/em&gt; method we execute the login code with the username and password passed from the Gherkin scenario. We then store the result in the ScenarioContext. Because our test runs across multiple methods, and even multiple classes, we can store state in the ScenarioContext.&lt;/p&gt;

&lt;p&gt;In the &lt;em&gt;Then&lt;/em&gt; method we compare what we stored in the ScenarioContext with what the Gherkin scenario expects.&lt;/p&gt;

&lt;p&gt;What is really nice about this is that I can now add a few more Gherkin scenarios and not change this C# code at all. Which is great for productivity and clean code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5 - Add more scenarios without the need for more C
&lt;/h3&gt;

&lt;p&gt;We now add three more login scenarios without changing our steps class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@loginSuccess
Scenario: Correct username with different case and password produces a success response
    Given that the user 'john' exists and his password is 'monkey'
    When the username 'John' with password 'monkey' is supplied
    Then the result should be 'Success'

@loginFailure
Scenario: User does not exist
    Given that the user 'john' exists and his password is 'monkey'
    When the username 'jane' with password '12345' is supplied
    Then the result should be 'UserDoesNotExist'

@loginFailure
Scenario: Password is wrong
    Given that the user 'john' exists and his password is 'monkey'
    When the username 'john' with password '12345' is supplied
    Then the result should be 'PasswordWrong'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6 - More Gherkin Syntax - Backgrounds
&lt;/h3&gt;

&lt;p&gt;So far we have seen the feature description and scenarios. Each scenario has a @tag for organising the tests, and a Given, When, Then structure.&lt;/p&gt;

&lt;p&gt;Now we are going to look at Backgrounds. Sometimes you end up with the same Given over and over again. In these cases you can reduce duplication by adding a Background to your feature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature: RateLimiting
    Login attempts of a given user must be limited to a certain amount X within a time period Y. 
    This is important to avoid brute force attacks on our users.

Background:
    Given that the user 'john' exists and his password is 'monkey'
    And that the user 'mick' exists and his password is '123456'

@rateLimiting
Scenario: Number of attempts less than limit
    Given the limit period is 10 seconds
    And the limit in that period is 3
    When 2 logins with 0 seconds delay are attempted with user 'john' and password '123'
    Then the results should be 'WrongPassword,WrongPassword'

@rateLimiting
Scenario: Attempts inside rate limit
    Given the limit period is 10 seconds
    And the limit in that period is 3
    When 4 logins with 4 seconds delay are attempted with user 'john' and password '123'
    Then the results should be 'WrongPassword,WrongPassword,WrongPassword,WrongPassword'

@rateLimiting
Scenario: Attempts exceeds limit
    Given the limit period is 10 seconds
    And the limit in that period is 3
    When 4 logins with 0 seconds delay are attempted with user 'john' and password '123'
    Then the results should be 'WrongPassword,WrongPassword,ReachedRateLimit,ReachedRateLimit'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we need the user John for each scenario we put it in the feature Background and avoid the repetition. When we generate the steps class you’ll notice that the Background givens are not included as they already exist in the BasicLoginSteps.cs.&lt;/p&gt;

&lt;p&gt;Also new in this feature is the use of comma separated list values. But it doesn’t get parsed into a List by default. The C# for the When with the CSV value are bound to a string argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"the results should be '(.*)']"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ThenTheResultsShouldBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;p0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ScenarioContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Pending&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be modified to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Then(@"the results should be '(.*)']")]
public void ThenTheResultsShouldBe(List&amp;lt;string&amp;gt; p0)
{
    ScenarioContext.Current.Pending();
}

[StepArgumentTransformation]
public List&amp;lt;String&amp;gt; TransformToListOfString(string commaSeparatedList)
{
    return commaSeparatedList.Split(',').ToList();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You want that TransformToListOfString method in a separate helper class as it will be need by multiple steps classes. Just remember to add the [Binding] attribute to the class so that SpecFlow can find it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7 - More Gherkin Syntax - Tables
&lt;/h3&gt;

&lt;p&gt;When we have very data driven acceptance tests, a table is more readable and generally easier to work with.&lt;/p&gt;

&lt;p&gt;We can use tables in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make table arguments for our bound methods&lt;/li&gt;
&lt;li&gt;Use tables for executing a scenario multiple times with different variables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s make a table based version of our BasicLogin feature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Feature: BasicLoginWithTables
    When correct usernames and passwords are supplied a login success
    response is returned

Background:
    Given the following users exist:
    | User | Password |
    | john | monkey   |
    | mick | 123456   |
    | joe  | 654321   |

@TableBased
Scenario: Usernames and passwords produce different responses
    When the username &amp;lt;username&amp;gt; with password &amp;lt;password&amp;gt; is supplied
    Then the result should be &amp;lt;result&amp;gt;

Examples:
    | username | password | result           |
    | john     | monkey   | Success          |
    | john     | 12345    | WrongPassword    |
    | jane     | 123      | UserDoesNotExist |

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The background creates the three users and their passwords. The scenario gets executed three times, with the values specified in the Examples table. This makes some tests even easier to read and faster to write.&lt;/p&gt;

&lt;p&gt;The C# behind the scenario is the same as our original steps class because it still works with individual variables. Whereas the Given in the Background accepts a Table as a method argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Given&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"the following users exist:"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GivenTheFollowingUsersExist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ScenarioContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Pending&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we create a UserRecord class, we can cast this table to a List. You need to add the line using TechTalk.SpecFlow.Assist; to your class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Given&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"the following users exist:"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GivenTheFollowingUsersExist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;CredentialRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Credentials&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateSet&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;UserRecord&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;CredentialRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Credentials&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 8 - Add your tests to your testing pipeline.
&lt;/h3&gt;

&lt;p&gt;Because the SpecFlow tests are treated like any other unit or integration tests they integrate into your existing testing pipeline like any other test. You can now get immediate feedback every time there is a code check-in. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Continuous Testing is a must in modern software development. With the use of Acceptance Test Driven Development you see major improvements in code quality and reduced time to market. Frameworks like SpecFlow make it so easy to do! Combine that with other code quality indicators, code coverage and other testing metrics and you reduce the risk involved with each software project. Developers are happier, QA is happier and your client is happier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Vanlightly/for-sharing-code/tree/master/testing/specflow/MyApp"&gt;Code on GitHub.&lt;/a&gt; &lt;/p&gt;

</description>
      <category>testing</category>
    </item>
  </channel>
</rss>
