Sunday, March 8, 2020

SpecFlow

What is SpecFlow?

SpecFlow is a testing framework that supports Behaviour Driven Development (BDD). It lets us define application behavior in plain meaningful English text using a simple grammar defined by a language called Gherkin. 
Use SpecFlow to define, manage and automatically execute human-readable acceptance tests in .NET projects. Writing easily understandable tests is a cornerstone of the BDD paradigm and also helps build up a living documentation of your system.
SpecFlow integrates with Visual Studio, but can be also used from the command line (e.g. on a build server). SpecFlow supports popular testing frameworks: MSTest v2, NUnit 3 and xUnit 2.
Image result for specflow

SpecFlow is inspired by Cucumber framework in the Ruby on Rails world. Cucumber uses plain English in the Gherkin format to express user stories. Once the user stories and their expectations are written, the Cucumber gem is used to execute those stores. SpecFlow brings the same concept to the .NET world and allows the developer to express the feature in plain English language. It also allows to write specification in human-readable Gherkin format.

Test-Driven Development

TDD is an iterative development process. Each iteration starts with a set of tests written for a new piece of functionality. These tests are supposed to fail during the start of iteration as there will be no application code corresponding to the tests. In the next phase of the iteration, Application code is written with an intention to pass all the tests written earlier in the iteration. Once the application code is ready tests are run.

Any failures in the test run are marked and more Application code is written/re-factored to make these tests pass. Once application code is added/re-factored the tests are run again. This cycle keeps on happening until all the tests pass. Once all the tests pass we can be sure that all the features for which tests were written have been developed.

Benefits of TDD
  • Unit test proves that the code actually works
  • Can drive the design of the program
  • Refactoring allows improving the design of the code
  • Low-Level regression test suite
  • Test first reduce the cost of the bugs
Drawbacks of TDD
  • Developer can consider it as a waste of time
  • The test can be targeted on verification of classes and methods and not on what the code really should do
  • Test become part of the maintenance overhead of a project
  • Rewrite the test when requirements change
Behavior Driven Development

Behavior Driven testing is an extension of TDD. Like in TDD in BDD also we write tests first and the add application code. BDD is popular and can be utilized for Unit level test cases and for UI level test cases. Tools like RSpec (for Ruby) or in .NET something like MSpec or SpecUnit is popular for Unit Testing following BDD approach.  Alternatively, you can write BDD-style specifications about UI interactions. The major difference that we get to see here are

  • Tests are written in plain descriptive English type grammar
  • Tests are explained as behavior of application and are more user-focused
  • Using examples to clarify requirements.
This difference brings in the need to have a language that can define, in an understandable format.

Features of BDD
  • Shifting from thinking in “tests” to thinking in “behavior”
  • Collaboration between Business stakeholders, Business Analysts, QA Team and developers
  • Ubiquitous language, it is easy to describe
  • Driven by Business Value
  • Extends Test-Driven Development (TDD) by utilizing natural language that non-technical stakeholders can understand

Data Driven Test approach

What is Data Driven Testing?

Data Driven Testing is a test automation framework that stores test data in a table or spreadsheet format. This allows automation engineers to have a single test script that can execute tests for all the test data in the table.
In this framework, input values are read from data files and are stored into a variable in test scripts. DDT (Data Driven testing) enables building both positive and negative test cases into a single test.

In Data-driven test automation framework, input data can be stored in single or multiple data sources like xls, XML, csv, and databases.


Why Data Driven Testing?

Frequently we have multiple data sets which we need to run the same tests on. To create an individual test for each data set is a lengthy and time-consuming process.

Data Driven Testing framework resolves this problem by keeping the data separate from Functional tests. The same test script can execute for different combinations of input test data and generate test results.

For example, we want to test the login system with multiple input fields with 1000 different data sets.
To test this, you can take following different approaches:

Approach 1. Create 1000 scripts one for each data-set and runs each test separately one by one.

Approach 2. Manually change the value in the test script and run it several times.

Approach 3. Import the data from the excel sheet. Fetch test data from excel rows one by one and execute the script.

In the given three scenarios first two are laborious and time-consuming. Therefore, it is ideal to follow the third approach. Thus, the third approach is nothing but a Data-Driven framework.

Best practices of Data Driven testing:
  • Below given are Best testing practices for Data-Driven testing:
  • Use Data to Drive Dynamic Assertions
  • It is ideal to use realistic information during the data-driven testing process
  • Test flow navigation should be coded inside the test script
  • Drive virtual APIs with meaningful data
  • Test positive as well as negative outcomes
  • Re-purpose Data Driven Functional Tests for Security and Performance

Advantages of Data-Driven testing
  • Allows to test application with multiple sets of data values during Regression testing
  • Test data and verification data can be organized in just one file, and it is separate from the test case logic.
  • Actions and Functions can be reused in different tests. 
  • Some tools generate test data automatically. This is useful when large volumes of random test data are necessary, which helps to save the time.
  • Allows developers and testers to have clear separation for the logic of their test cases/scripts from the test data.
  • The same test cases can be executed several times which helps to reduce test case and scripts.
  • Any changes in the test script do not effect the test data.
Disadvantages of Data Driven testing

  • Quality of the test is depended on the automation skills of the Implementing team
  • Data validation is a time-consuming task when testing large amount of data.
  • Maintenance is a big issue as large amount of coding needed for Data-Driven testing.
  • High-level technical skills are required. A tester may have to learn an entirely new scripting language.
  • A text editor like Notepad is required to create and maintain data files.

Implementation of NUnit Testing Framework

What is Unit Testing?

In this IT world a unit refers to simply a smallest piece of code which takes an input ,does certain operation, and gives an output. And testing this small piece of code is called Unit Testing.


A lot of unit test frameworks are available for .Net nowadays, if we check in Visual Studio we have MS-Test from Microsoft integrated in Visual Studio. Some 3rd party frameworks are:

  • NUnit
  • MbUnit
What is NUnit Testing?

NUnit is a unit-testing framework for .NET applications in which the entire application is isolated into diverse modules. Each module is tested independently to ensure that the objective is met. The NUnit Framework caters a range of attributes that are used during unit tests. They are used to define Test -Fixtures, Test methods, Expected Exception and Ignore methods.
Image result for nunit testing framework c#

Important NUnit Annotations
Now let’s take a look at the annotations and how to use them. The annotations are very easy to use: just add the annotation between brackets before the method declaration. With the annotation you can define the test: behavior (specifying Setup or TearDown method), assertions for example performance assertions like MaxTime method, and information like the Category method.

Annotation
Usage
Category
Specifies one or more categories for the test
Culture
Specifies cultures for which a test or fixture should be run
Indicates
Indicates that a test should be skipped unless explicitly run
Ignore
Indicates that a test shouldn't be run for some reason
MaxTime
Specifies the maximum time in milliseconds for a test case to succeed
OneTimeSetUp
Identifies methods to be called once prior to any child tests
OneTimeTearDown
Identifies methods to be called once after all child tests
Platform
Specifies platforms for which a test or fixture should be run
Random
Specifies generation of random values as arguments to a parameterized test
Repeat
Specifies that the decorated method should be executed multiple times
Retry
Causes a test to rerun if it fails, up to a maximum number of times
TearDown
Indicates a method of a TestFixture called immediately after each test method
Test
Marks a method of a TestFixture that represents a test
TestCase
Marks a method with parameters as a test and provides inline arguments
Timeout
Provides a timeout value in milliseconds for test cases
SetUp
Indicates a method of a TestFixture called immediately before each test method
TestFixture
The TestFixture attribute is an indication that a class contains test methods.
Why should I use the NUnit framework?
  • NUnit runs very well with .NET programming languages.
  • It is open source and it is free.
  • It is easy to integrate it with testing projects.
  • NUnit works with many integrated runners including Resharper and TestDriven .NET.
  • NUnit has frequent version updates.
  • NUnit has a graphical user interface.
  • Very easy integration with Visual Studio and other IDEs.

Featured Posts