<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pvasude</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pvasude"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Pvasude"/>
	<updated>2026-06-30T18:06:59Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71102</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71102"/>
		<updated>2012-11-20T03:27:46Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Memento, directory of sites=&lt;br /&gt;
The objective of this wiki is to provide a directory of sites that one would refer to while learning about the Memento pattern. This wiki will be giving an overview of the different features of the Memento pattern along with links to useful websites that explain each of these features in more detail.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&amp;lt;ref name=&amp;quot;implementation&amp;quot;&amp;gt; http://www.oodesign.com/memento-pattern.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example &amp;lt;ref name=&amp;quot;ex&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Memento_pattern&amp;lt;/ref&amp;gt; shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
he saved state is inserted to the Caretaker object and than &lt;br /&gt;
you can change the state of the Originator to whatever you need. In order to &lt;br /&gt;
restore the Originator state you use the restore method and pass it the Caretaker’s &lt;br /&gt;
saved Memento.&lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Example] Memento Pattern Example &lt;br /&gt;
*[http://stackoverflow.com/questions/8994433/how-is-the-memento-pattern-implemented-in-c4 Implementation] Memento Pattern Discussion &lt;br /&gt;
*[http://adapower.com/index.php?Command=Class&amp;amp;ClassID=Patterns&amp;amp;CID=271 Description of Memento Pattern] in [[Ada (programming language)|Ada]]&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Memento UML Class Diagram] with C# and .NET code samples&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/memento SourceMaking Tutorial]&lt;br /&gt;
*[http://www.nileshgule.com/2012/08/memento-design-pattern.html Memento Design Pattern using C# ] by Nilesh Gule&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71099</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71099"/>
		<updated>2012-11-20T03:25:40Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Memento, directory of sites=&lt;br /&gt;
The objective of this wiki is to provide a directory of sites that one would refer to while learning about the Memento pattern. This wiki will be giving an overview of the different features of the Memento pattern along with links to useful websites that explain each of these features in more detail.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example &amp;lt;ref name=&amp;quot;adapter&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Memento_pattern&amp;lt;/ref&amp;gt; shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
he saved state is inserted to the Caretaker object and than &lt;br /&gt;
you can change the state of the Originator to whatever you need. In order to &lt;br /&gt;
restore the Originator state you use the restore method and pass it the Caretaker’s &lt;br /&gt;
saved Memento.&lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Example] Memento Pattern Example &lt;br /&gt;
*[http://stackoverflow.com/questions/8994433/how-is-the-memento-pattern-implemented-in-c4 Implementation] Memento Pattern Discussion &lt;br /&gt;
*[http://adapower.com/index.php?Command=Class&amp;amp;ClassID=Patterns&amp;amp;CID=271 Description of Memento Pattern] in [[Ada (programming language)|Ada]]&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Memento UML Class Diagram] with C# and .NET code samples&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/memento SourceMaking Tutorial]&lt;br /&gt;
*[http://www.nileshgule.com/2012/08/memento-design-pattern.html Memento Design Pattern using C# ] by Nilesh Gule&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71097</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71097"/>
		<updated>2012-11-20T03:22:08Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Memento, directory of sites=&lt;br /&gt;
The objective of this wiki is to provide a directory of sites that one would refer to while learning about the Memento pattern. This wiki will be giving an overview of the different features of the Memento pattern along with links to useful websites that explain each of these features in more detail.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
he saved state is inserted to the Caretaker object and than &lt;br /&gt;
you can change the state of the Originator to whatever you need. In order to &lt;br /&gt;
restore the Originator state you use the restore method and pass it the Caretaker’s &lt;br /&gt;
saved Memento.&lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Example] Memento Pattern Example &lt;br /&gt;
*[http://stackoverflow.com/questions/8994433/how-is-the-memento-pattern-implemented-in-c4 Implementation] Memento Pattern Discussion &lt;br /&gt;
*[http://adapower.com/index.php?Command=Class&amp;amp;ClassID=Patterns&amp;amp;CID=271 Description of Memento Pattern] in [[Ada (programming language)|Ada]]&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Memento UML Class Diagram] with C# and .NET code samples&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/memento SourceMaking Tutorial]&lt;br /&gt;
*[http://www.nileshgule.com/2012/08/memento-design-pattern.html Memento Design Pattern using C# ] by Nilesh Gule&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Memento_pattern&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71096</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71096"/>
		<updated>2012-11-20T03:21:38Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Memento, directory of sites=&lt;br /&gt;
The objective of this wiki is to provide a directory of sites that one would refer to while learning about the Memento pattern. This wiki will be giving an overview of the different features of the Memento pattern along with links to useful websites that explain each of these features in more detail.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
he saved state is inserted to the Caretaker object and than &lt;br /&gt;
you can change the state of the Originator to whatever you need. In order to &lt;br /&gt;
restore the Originator state you use the restore method and pass it the Caretaker’s &lt;br /&gt;
saved Memento.&lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
== External Links == &lt;br /&gt;
==External links==&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Example] Memento Pattern Example &lt;br /&gt;
*[http://stackoverflow.com/questions/8994433/how-is-the-memento-pattern-implemented-in-c4 Implementation] Memento Pattern Discussion &lt;br /&gt;
*[http://adapower.com/index.php?Command=Class&amp;amp;ClassID=Patterns&amp;amp;CID=271 Description of Memento Pattern] in [[Ada (programming language)|Ada]]&lt;br /&gt;
*[http://dofactory.com/Patterns/PatternMemento.aspx Memento UML Class Diagram] with C# and .NET code samples&lt;br /&gt;
*[http://sourcemaking.com/design_patterns/memento SourceMaking Tutorial]&lt;br /&gt;
*[http://www.nileshgule.com/2012/08/memento-design-pattern.html Memento Design Pattern using C# ] by Nilesh Gule&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
http://en.wikipedia.org/wiki/Memento_pattern&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71090</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71090"/>
		<updated>2012-11-20T03:14:30Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Memento, directory of sites=&lt;br /&gt;
The objective of this wiki is to provide a directory of sites that one would refer to while learning about the Memento pattern. This wiki will be giving an overview of the different features of the Memento pattern along with links to useful websites that explain each of these features in more detail.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
he saved state is inserted to the Caretaker object and than &lt;br /&gt;
you can change the state of the Originator to whatever you need. In order to &lt;br /&gt;
restore the Originator state you use the restore method and pass it the Caretaker’s &lt;br /&gt;
saved Memento.&lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71088</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71088"/>
		<updated>2012-11-20T03:13:52Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Memento, directory of sites=&lt;br /&gt;
The objective of this wiki is to provide a directory of sites that one would refer to while learning about the Memento pattern. This wiki will be giving an overview of the different features of the Memento pattern along with links to useful websites that explain each of these features in more detail.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
he saved state is inserted to the Caretaker object and than &lt;br /&gt;
you can change the state of the Originator to whatever you need. In order to &lt;br /&gt;
restore the Originator state you use the restore method and pass it the Caretaker’s &lt;br /&gt;
saved Memento.&lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71086</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71086"/>
		<updated>2012-11-20T03:11:04Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Usage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
he saved state is inserted to the Caretaker object and than &lt;br /&gt;
you can change the state of the Originator to whatever you need. In order to &lt;br /&gt;
restore the Originator state you use the restore method and pass it the Caretaker’s &lt;br /&gt;
saved Memento.&lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71085</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71085"/>
		<updated>2012-11-20T03:10:26Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;br /&gt;
&lt;br /&gt;
== Summary == &lt;br /&gt;
To sum up the post, the memento pattern is is used for saving encapsulated object state &lt;br /&gt;
in an external object. The state can then be restored by demand. &lt;br /&gt;
As I wrote the places that you’ll want to use the pattern are in scientific &lt;br /&gt;
computing or in computer games but it can be helpful also in other places. &lt;br /&gt;
The next post in the series will explain the state pattern.&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71083</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71083"/>
		<updated>2012-11-20T03:07:45Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&amp;lt;br&amp;gt;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&amp;lt;br&amp;gt;&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71080</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71080"/>
		<updated>2012-11-20T03:06:07Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
&lt;br /&gt;
=== Usage === &lt;br /&gt;
&lt;br /&gt;
   Originator&amp;lt;string&amp;gt; org = new Originator&amp;lt;string&amp;gt;();&lt;br /&gt;
   org.State = &amp;quot;Old State&amp;quot;;&lt;br /&gt;
   // Store internal state in the caretaker object&lt;br /&gt;
   Caretaker&amp;lt;string&amp;gt; caretaker = new Caretaker&amp;lt;string&amp;gt;();&lt;br /&gt;
   caretaker.Memento = org.SaveMemento();&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the old state: {0}&amp;quot;, org.State);&lt;br /&gt;
   org.State = &amp;quot;New state&amp;quot;;&lt;br /&gt;
   Console.WriteLine(&amp;quot;This is the new state: {0}&amp;quot;, org.State);&lt;br /&gt;
   // Restore saved state from the caretaker&lt;br /&gt;
   org.RestoreMemento(caretaker.Memento);&lt;br /&gt;
   Console.WriteLine(&amp;quot;Old state was restored: {0}&amp;quot;, org.State);&lt;br /&gt;
   // Wait for user&lt;br /&gt;
   Console.Read();&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71078</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71078"/>
		<updated>2012-11-20T03:05:07Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
The given example shows the three parts of the pattern: the Originator, the Memento &lt;br /&gt;
and the Caretaker. The Caretaker is the repository for the Memento. You can also see that once the Memento object is created you can’t change the saved state and in order &lt;br /&gt;
to save a new Memento you need to create it again.&lt;br /&gt;
Implementation of the pattern in C#. &lt;br /&gt;
&lt;br /&gt;
    region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71077</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71077"/>
		<updated>2012-11-20T03:03:03Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;br /&gt;
&lt;br /&gt;
=== Example === &lt;br /&gt;
&lt;br /&gt;
region Originator&lt;br /&gt;
    public class Originator&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Methods&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Creates a new memento to hold the current&lt;br /&gt;
        /// state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;returns&amp;gt;The created memento&amp;lt;/returns&amp;gt;&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; SaveMemento()&lt;br /&gt;
        {&lt;br /&gt;
            return (new Memento&amp;lt;T&amp;gt;(State));&lt;br /&gt;
        }&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Restores the state which is saved in the given memento&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;memento&amp;quot;&amp;gt;The given memento&amp;lt;/param&amp;gt;&lt;br /&gt;
        public void RestoreMemento(Memento&amp;lt;T&amp;gt; memento)&lt;br /&gt;
        {           &lt;br /&gt;
            State = memento.State;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Memento&lt;br /&gt;
    public class Memento&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public T State { get; private set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
        #region Ctor&lt;br /&gt;
        /// &amp;lt;summary&amp;gt;&lt;br /&gt;
        /// Construct a new memento object with the&lt;br /&gt;
        /// given state&lt;br /&gt;
        /// &amp;lt;/summary&amp;gt;&lt;br /&gt;
        /// &amp;lt;param name=&amp;quot;state&amp;quot;&amp;gt;The given state&amp;lt;/param&amp;gt;&lt;br /&gt;
        public Memento(T state)&lt;br /&gt;
        {&lt;br /&gt;
            State = state;&lt;br /&gt;
        }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;br /&gt;
    #region Caretaker&lt;br /&gt;
    public class Caretaker&amp;lt;T&amp;gt;&lt;br /&gt;
    {&lt;br /&gt;
        #region Properties&lt;br /&gt;
        public Memento&amp;lt;T&amp;gt; Memento { get; set; }&lt;br /&gt;
        #endregion&lt;br /&gt;
    }&lt;br /&gt;
    #endregion&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71075</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71075"/>
		<updated>2012-11-20T02:54:57Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.JPG]]&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Momento.JPG&amp;diff=71074</id>
		<title>File:Momento.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Momento.JPG&amp;diff=71074"/>
		<updated>2012-11-20T02:54:11Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: uploaded a new version of &amp;amp;quot;File:Momento.JPG&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71069</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71069"/>
		<updated>2012-11-20T02:50:37Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Momento.jpg|200px|thumb|left|alt text]]&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Momento.JPG&amp;diff=71067</id>
		<title>File:Momento.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Momento.JPG&amp;diff=71067"/>
		<updated>2012-11-20T02:50:03Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71063</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71063"/>
		<updated>2012-11-20T02:47:50Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Implementation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71061</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71061"/>
		<updated>2012-11-20T02:47:30Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&lt;br /&gt;
&amp;lt;b&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71057</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71057"/>
		<updated>2012-11-20T02:45:24Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
The memento pattern is a pattern that provides the ability to restore an object to its previous state(via rollback). The memento pattern is implemented with two objects: the originator and a caretaker. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.  &lt;br /&gt;
&lt;br /&gt;
== Motivation == &lt;br /&gt;
It is sometimes necessary to capture the internal state of an object at some point and have the ability to restore the object to that state later in time. Such a case is useful in case of error or failure. Consider the case of a calculator object with an undo operation such a calculator could simply maintain a list of all previous operation that it has performed and thus would be able to restore a previous calculation it has performed. This would cause the calculator object to become larger, more complex, and heavyweight, as the calculator object would have to provide additional undo functionality and should maintain a list of all previous operations. This functionality can be moved out of the calculator class, so that an external (let's call it undo manager class) can collect the internal state of the calculator and save it. However providing the explicit access to every state variable of the calculator to the restore manager would be impractical and would violate the encapsulation principle.&lt;br /&gt;
&lt;br /&gt;
== Implementation == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Memento&amp;lt;/b&amp;gt;&lt;br /&gt;
Stores internal state of the Originator object. The state can include any number of state variables.&lt;br /&gt;
The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state.&lt;br /&gt;
&amp;lt;b&amp;gt;Originator&amp;lt;/b&amp;gt;&lt;br /&gt;
Creates a memento object capturing the originators internal state.&lt;br /&gt;
Use the memento object to restore its previous state.&lt;br /&gt;
Mb&amp;gt;Caretaker&amp;lt;/b&amp;gt;&lt;br /&gt;
Responsible for keeping the memento.&lt;br /&gt;
The memento is opaque to the caretaker, and the caretaker must not operate on it.&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71007</id>
		<title>CSC/ECE 517 Fall 2012/ch2b 2w62 pv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2b_2w62_pv&amp;diff=71007"/>
		<updated>2012-11-20T01:17:11Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: Created page with &amp;quot;Test&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Test&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=71005</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=71005"/>
		<updated>2012-11-20T01:16:32Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE_517_Fall_2012/Table_Of_Contents]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w3_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w7_ma]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w6_ar]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w32_mk]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w10_rc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w70_sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w67_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w40_sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w22_sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w-1w65_am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w59_bc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w60_ns]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w47 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w69_as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w39_ka]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w36_av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w37_ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w43_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w53_iv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w63_sp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w49_ps]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w52_sj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w28_dh]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 2w41 dc]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w59_nm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w61_ps]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w57]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w42_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_1w61_ns]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w51_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2b_2w45_pg]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_2w48_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b_1w70_nl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w64 bg]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2b 2w62 pv]]&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65098</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65098"/>
		<updated>2012-09-15T02:12:36Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite. Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for Ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non-programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The directory structure looks like this&lt;br /&gt;
[[File:Dirstructure.JPG|400px|thumb|center|Directory structure]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are few steps which have to be followed before you will be able to run cucumber with Rails. Lets walk through the steps by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems should be added only to the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The step_definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last command creates controllers for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go to the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Advantage of Cucumber]&amp;lt;/ref&amp;gt; helps facilitate the discovery and use of a ubiquitous language within the team. Cucumber tests interact directly with the developers’ code, but they’re written in a high level language that business stakeholders can understand. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone’s different little dialects is&lt;br /&gt;
gone, and the chances of misunderstandings are greatly reduced. What makes Cucumber stand out from the crowd of other testing tools is that it has been designed specifically to ensure the acceptance tests can easily be read—and written—by anyone on the team.In practice, this means that your documentation, rather than being something that’s written once and then gradually goes out of date, becomes a living thing that reflects the true state of the project&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65094</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65094"/>
		<updated>2012-09-15T02:11:36Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite. Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for Ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non-programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The directory structure looks like this&lt;br /&gt;
[[File:Dirstructure.JPG|400px|thumb|left|Directory structure]]&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are few steps which have to be followed before you will be able to run cucumber with Rails. Lets walk through the steps by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems should be added only to the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The step_definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last command creates controllers for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go to the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Advantage of Cucumber]&amp;lt;/ref&amp;gt; helps facilitate the discovery and use of a ubiquitous language within the team. Cucumber tests interact directly with the developers’ code, but they’re written in a high level language that business stakeholders can understand. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone’s different little dialects is&lt;br /&gt;
gone, and the chances of misunderstandings are greatly reduced. What makes Cucumber stand out from the crowd of other testing tools is that it has been designed specifically to ensure the acceptance tests can easily be read—and written—by anyone on the team.In practice, this means that your documentation, rather than being something that’s written once and then gradually goes out of date, becomes a living thing that reflects the true state of the project&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65091</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65091"/>
		<updated>2012-09-15T02:10:30Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite. Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for Ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The directory structure looks like this&lt;br /&gt;
[[File:Dirstructure.JPG|400px|thumb|left|Directory structure]]&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are few steps which have to be followed before you will be able to run cucumber with Rails. Lets walk through the steps by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems should be added only to the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The step_definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last command creates controllers for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go to the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Advantage of Cucumber]&amp;lt;/ref&amp;gt; helps facilitate the discovery and use of a ubiquitous language within the team. Cucumber tests interact directly with the developers’ code, but they’re written in a high level language that business stakeholders can understand. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone’s different little dialects is&lt;br /&gt;
gone, and the chances of misunderstandings are greatly reduced. What makes Cucumber stand out from the crowd of other testing tools is that it has been designed specifically to ensure the acceptance tests can easily be read—and written—by anyone on the team.In practice, this means that your documentation, rather than being something that’s written once and then gradually goes out of date, becomes a living thing that reflects the true state of the project&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65089</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65089"/>
		<updated>2012-09-15T02:09:56Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite. Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The directory structure looks like this&lt;br /&gt;
[[File:Dirstructure.JPG|200px|thumb|center|Directory structure]]&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are few steps which have to be followed before you will be able to run cucumber with Rails. Lets walk through the steps by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems should be added only to the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The step_definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last command creates controllers for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go to the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Advantage of Cucumber]&amp;lt;/ref&amp;gt; helps facilitate the discovery and use of a ubiquitous language within the team. Cucumber tests interact directly with the developers’ code, but they’re written in a high level language that business stakeholders can understand. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone’s different little dialects is&lt;br /&gt;
gone, and the chances of misunderstandings are greatly reduced. What makes Cucumber stand out from the crowd of other testing tools is that it has been designed specifically to ensure the acceptance tests can easily be read—and written—by anyone on the team.In practice, this means that your documentation, rather than being something that’s written once and then gradually goes out of date, becomes a living thing that reflects the true state of the project&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65088</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=65088"/>
		<updated>2012-09-15T02:09:32Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite. Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The directory structure looks like this&lt;br /&gt;
[[File:File.png|200px|thumb|center|Directory structure]]&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are few steps which have to be followed before you will be able to run cucumber with Rails. Lets walk through the steps by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems should be added only to the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The step_definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last command creates controllers for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go to the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Advantage of Cucumber]&amp;lt;/ref&amp;gt; helps facilitate the discovery and use of a ubiquitous language within the team. Cucumber tests interact directly with the developers’ code, but they’re written in a high level language that business stakeholders can understand. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone’s different little dialects is&lt;br /&gt;
gone, and the chances of misunderstandings are greatly reduced. What makes Cucumber stand out from the crowd of other testing tools is that it has been designed specifically to ensure the acceptance tests can easily be read—and written—by anyone on the team.In practice, this means that your documentation, rather than being something that’s written once and then gradually goes out of date, becomes a living thing that reflects the true state of the project&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Dirstructure.JPG&amp;diff=65085</id>
		<title>File:Dirstructure.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Dirstructure.JPG&amp;diff=65085"/>
		<updated>2012-09-15T02:07:25Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64503</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64503"/>
		<updated>2012-09-14T21:56:44Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Summary */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Advantage of Cucumber]&amp;lt;/ref&amp;gt; helps facilitate the discovery and use of a ubiquitous language within the team. Cucumber tests interact directly with the developers’ code, but they’re written in a high level language that business stakeholders can understand. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone’s different little dialects is&lt;br /&gt;
gone, and the chances of misunderstandings are greatly reduced. What makes Cucumber stand out from the crowd of other testing tools is that it has been designed specifically to ensure the acceptance tests can easily be read—and written—by anyone on the team.In practice, this means that your documentation, rather than being something that’s written once and then gradually goes out of date, becomes a living thing that reflects the true state of the project&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64495</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64495"/>
		<updated>2012-09-14T21:55:42Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Reference */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Summary=&lt;br /&gt;
&lt;br /&gt;
Cucumber helps facilitate the discovery and use of a ubiquitous language within the team. Cucumber tests interact directly with the developers’ code, but they’re written in a high level language that business stakeholders can understand. When the team uses this language consistently in their conversations, documentation, and code, the friction of translating between everyone’s different little dialects is&lt;br /&gt;
gone, and the chances of misunderstandings are greatly reduced. What makes Cucumber stand out from the crowd of other testing tools is that it has been designed specifically to ensure the acceptance tests can easily be read—and written—by anyone on the team.In practice, this means that your documentation, rather than being something that’s written once and then gradually goes out of date, becomes a living thing that reflects the true state of the project&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64474</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64474"/>
		<updated>2012-09-14T21:46:11Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing &amp;lt;ref&amp;gt;[http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2010/ch1_1f_vn Unit Testing in Ruby]&amp;lt;/ref&amp;gt; is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64472</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64472"/>
		<updated>2012-09-14T21:44:54Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cucumber&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt; easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64468</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64468"/>
		<updated>2012-09-14T21:44:02Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures.&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64463</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64463"/>
		<updated>2012-09-14T21:41:33Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Failed steps */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|center|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64462</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64462"/>
		<updated>2012-09-14T21:41:13Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Cucumber Testing Stack */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64432</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64432"/>
		<updated>2012-09-14T21:23:56Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Lets walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64431</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64431"/>
		<updated>2012-09-14T21:23:37Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
Let walk through a example for creating a Hello World Ruby application and testing it using cucumber&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64430</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64430"/>
		<updated>2012-09-14T21:22:22Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Cucumber Installation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To install cucumber&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt; we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64421</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64421"/>
		<updated>2012-09-14T21:19:32Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber is  more descriptive and allows business stakeholder to read and write test specification.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64417</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64417"/>
		<updated>2012-09-14T21:18:03Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since it was designed with the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64415</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64415"/>
		<updated>2012-09-14T21:17:15Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec and provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64414</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64414"/>
		<updated>2012-09-14T21:16:50Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native languages &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64393</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64393"/>
		<updated>2012-09-14T21:06:39Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
The required cucumber files are generated using the following command. The output below the command shows the folders and files that gets created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64392</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64392"/>
		<updated>2012-09-14T21:05:11Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file, run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64391</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64391"/>
		<updated>2012-09-14T21:04:44Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
After modifying the gem file . Run bundle update &lt;br /&gt;
&lt;br /&gt;
      bundle update&lt;br /&gt;
&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64390</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64390"/>
		<updated>2012-09-14T21:03:42Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Add the following lines to your gemfile. These gems are required only for the test environment. The gems database_cleaner , rspec-rails and webrat are required for cucumber to function properly.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64385</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64385"/>
		<updated>2012-09-14T20:59:49Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Install the required cucumber gems&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We would have to then give “bundle install” command to install these new gems. We would then have to update the Gemfile to include these gems in the test group since they will be needed only in the test environment. There are a few other gems which are required for cucumber namely database_cleaner , rspec-rails and webrat.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64384</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64384"/>
		<updated>2012-09-14T20:58:49Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Install the following gems required by Cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We would have to then give “bundle install” command to install these new gems. We would then have to update the Gemfile to include these gems in the test group since they will be needed only in the test environment. There are a few other gems which are required for cucumber namely database_cleaner , rspec-rails and webrat.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64381</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64381"/>
		<updated>2012-09-14T20:56:50Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For using cucumber we need to have the following gems &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We would have to then give “bundle install” command to install these new gems. We would then have to update the Gemfile to include these gems in the test group since they will be needed only in the test environment. There are a few other gems which are required for cucumber namely database_cleaner , rspec-rails and webrat.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64380</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64380"/>
		<updated>2012-09-14T20:55:22Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Rails Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Lets walk through it by creating a new rails application.&lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
and the new applications with its folders get created.&lt;br /&gt;
&lt;br /&gt;
we would have to install the gems required for running Cucumber with Rails and there are couple of gems that are required and they can be installed with the following command : &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We would have to then give “bundle install” command to install these new gems. We would then have to update the Gemfile to include these gems in the test group since they will be needed only in the test environment. There are a few other gems which are required for cucumber namely database_cleaner , rspec-rails and webrat.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64379</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w3 pl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w3_pl&amp;diff=64379"/>
		<updated>2012-09-14T20:53:37Z</updated>

		<summary type="html">&lt;p&gt;Pvasude: /* Testing a Sample Ruby Application using Cucumber */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;='''Unit-Testing Frameworks for Ruby: Cucumber'''=&lt;br /&gt;
&lt;br /&gt;
This wiki-page serves as a knowledge source for understanding Unit-Testing Frameworks available for Ruby particularly Cucumber .&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Unit testing is a software development process for testing individual units of source code independently for proper operation &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Unit_testing Unit Testing]&amp;lt;/ref&amp;gt;. An unit testing framework helps the process of unit testing by automating the test cases written to ensure the correctness of the system. They are often third-party products that are not offered as part of the suite.Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit &amp;lt;ref&amp;gt;[http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing Ruby Unit Testing Framework]&amp;lt;/ref&amp;gt;. Other testing frameworks available for ruby are Shoulda, RSpec and Cucumber.&lt;br /&gt;
&lt;br /&gt;
=Cucumber=&lt;br /&gt;
Cucumber is one of the latest unit test frameworks to have come for Ruby as part of the RSpec family of tools. Cucumber itself is written in the Ruby programming language and adheres to Behaviour Driven Development(BDD). Behaviour Driven Development is an Agile Development process that comprises aspects of Acceptance Test Driven Planning , Domain Driven Design and Test Driven Development (TDD).  Cucumber is designed specifically to ensure that the acceptance tests can easily be read and written by anyone.&lt;br /&gt;
&lt;br /&gt;
==Behaviour Driven Development(BDD)==&lt;br /&gt;
&lt;br /&gt;
Behaviour Driven Development&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Behavior-driven_development BDD]&amp;lt;/ref&amp;gt; preserves the basic iterative (fail-pass) workflow of TDD, but stresses on specifying behaviors that are understandable to people (say from non programming background). In this approach we write tests in a natural language that even a non-programmer can understand.&lt;br /&gt;
&lt;br /&gt;
==Acceptance Test==&lt;br /&gt;
&lt;br /&gt;
Acceptance Test&amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Acceptance_testing Acceptance Test]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Cucumber Book]&amp;lt;/ref&amp;gt; Drive Planning is a practice that stresses on the importance of identifying the functions and requirements of the software beforehand so that we exactly know when the development phase ends. This practice results in fewer bugs , shorter delivery times and greater customer satisfaction.&lt;br /&gt;
&lt;br /&gt;
===Cucumber Acceptance Test===&lt;br /&gt;
&lt;br /&gt;
      &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Sign up&lt;br /&gt;
      Sign up should be quick and friendly.&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Successful sign up&lt;br /&gt;
      New users should get a confirmation email and be greeted personally by the site once signed in.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      When I sign up with valid details&lt;br /&gt;
      Then I should receive a confirmation email&lt;br /&gt;
      And I should see a personalized greeting message&amp;lt;br&amp;gt;&lt;br /&gt;
      &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Duplicate email&lt;br /&gt;
      Where someone tries to create an account for an email address that already exists.&lt;br /&gt;
      Given I have chosen to sign up&lt;br /&gt;
      But I enter an email address that has already registered&lt;br /&gt;
      Then I should be told that the email is already registered&lt;br /&gt;
      And I should be offered the option to recover my password&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Acceptance tests written in this style are more than just tests; they are executable specifications. As of January 2012, Cucumber was the second most popular testing framework after RSpec for Ruby&lt;br /&gt;
&lt;br /&gt;
==How Cucumber works== &lt;br /&gt;
&lt;br /&gt;
Cucumber is a command-line tool. It reads in specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against the system. The feature files should be written according to specific syntax rules called Gherkin. Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. Cucumber takes customer understandable user stories as input and does integration and acceptance tests on the system with those as its input. &lt;br /&gt;
&lt;br /&gt;
===Feature===&lt;br /&gt;
A Feature is a high level statement of the what the test case does and there is only one feature per user story. For every feature that is supported by the system , a separate feature file has to be written and included in the features directory. The feature files has .feature file extension.&lt;br /&gt;
&lt;br /&gt;
===Scenario===&lt;br /&gt;
A Scenario consists of one or more steps which describe about the various actions performed by the user with respect to this particular feature.  There can be one or more scenarios per feature in a user story. A feature usually contains 3 to 8 steps in it.  The steps of the scenario always begin with one of the below mentioned key words : &lt;br /&gt;
&lt;br /&gt;
1. &amp;lt;b&amp;gt;Given&amp;lt;/b&amp;gt; : Steps that begin with given represent the state of the world before an event.(preconditions)&amp;lt;br&amp;gt;&lt;br /&gt;
2. &amp;lt;b&amp;gt;When&amp;lt;/b&amp;gt; : Steps that represent the event. &amp;lt;br&amp;gt;&lt;br /&gt;
3. &amp;lt;b&amp;gt;Then&amp;lt;/b&amp;gt; : Steps that represent the expected outcome. &amp;lt;br&amp;gt; &lt;br /&gt;
4. &amp;lt;b&amp;gt;And and But&amp;lt;/b&amp;gt; : Steps that extend the previous step.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Step and Step Definitions===&lt;br /&gt;
When testing with Cucumber , The user stories are all kept in one set of files called Steps.The Steps are compared with Step Definitions which are files written in ruby which gives the rules based on which the steps are executed. Step definitions are analogous to  method definitions and steps of scenarios are analogous to method calls. Cucumber uses regular expressions to match the English phrases in the steps of scenarios to step definitions.&lt;br /&gt;
&lt;br /&gt;
When the cucumber test framework is run , The steps present in the feature file can be classified as follows based on the result of regex matching with the step definitions.&amp;lt;ref&amp;gt;[https://github.com/cucumber/cucumber/wiki/Step-Definitions Step Definitions in Ruby]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Successful steps====&lt;br /&gt;
When Cucumber finds a matching Step Definition it will execute it. If the block in the step definition doesn’t raise an Exception, the step is marked as successful (green). What you return from a Step Definition has no significance what so ever.&lt;br /&gt;
&lt;br /&gt;
====Undefined steps====&lt;br /&gt;
When Cucumber can’t find a matching Step Definition the step gets marked as yellow, and all subsequent steps in the scenario are skipped. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Pending steps====&lt;br /&gt;
When a Step Definition’s Proc invokes the #pending method, the step is marked as yellow (as with undefined ones), reminding you that you have work to do. If you use --strict this will cause Cucumber to exit with 1.&lt;br /&gt;
&lt;br /&gt;
====Failed steps====&lt;br /&gt;
When a Step Definition’s Proc is executed and raises an error, the step is marked as red. Returning nil or false will not cause a step definition to fail.&lt;br /&gt;
&lt;br /&gt;
Cucumber tries to run the steps based on the definitions and thus colors steps based on the execution of the steps. The colors for the steps assigned are as follows : &amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
1. Green : for passing.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Yellow : for not yet implemented.&amp;lt;br&amp;gt; &lt;br /&gt;
3. Red : for failing.&amp;lt;br&amp;gt; &lt;br /&gt;
4. Blue : Once a step fails all the successive steps are marked in blue.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Cucumber Testing Stack==&lt;br /&gt;
/* Some Content */&lt;br /&gt;
&lt;br /&gt;
[[File:Cucumber.JPG|500px|thumb|right|Cucumber Testing Stack]]&lt;br /&gt;
&lt;br /&gt;
==Advantages of Cucumber==&lt;br /&gt;
*Specifications can be written in  more than forty different spoken languages.&lt;br /&gt;
*Cucumber helps reduce the gap between technical and non-technical members of a software team.&lt;br /&gt;
*Cucumber tests can be easily read and written by business stakeholders.&lt;br /&gt;
*Writing Cucumber tests are quick, easy, efficient and understandable.&lt;br /&gt;
&lt;br /&gt;
=Cucumber Installation=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://cuke4ninja.com/sec_ruby_install_cucumber.html Installing Cucumber]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To install cucumber we should make sure that the Environment path is set to “..../Ruby/bin”. Then open the command prompt and run  &lt;br /&gt;
     &lt;br /&gt;
     gem install cucumber &lt;br /&gt;
&lt;br /&gt;
Once cucumber gem is installed then we would be able to check whether it is installed by issuing the following command: &lt;br /&gt;
&lt;br /&gt;
     cucumber --version&lt;br /&gt;
&lt;br /&gt;
this should give a version number like “1.2.1” &lt;br /&gt;
&lt;br /&gt;
Cucumber uses Rspec for assertions and hence we need Rspec installed to use Cucumber. Use the following command to install rspec &lt;br /&gt;
&lt;br /&gt;
     gem install rspec&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Ruby Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://pragprog.com/book/hwcuc/the-cucumber-book Sample Cucumber Application]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 1 : Create a new directory &lt;br /&gt;
   &lt;br /&gt;
     mkdir HelloCucumber &lt;br /&gt;
&lt;br /&gt;
Step 2 :  Install Cucumber and Rspec &lt;br /&gt;
&lt;br /&gt;
     gem install cucumber &lt;br /&gt;
     gem instal rspec&lt;br /&gt;
&lt;br /&gt;
Step 3 : Create a directory features and create a file basic.feature in the features directory&lt;br /&gt;
&lt;br /&gt;
      mkdir features&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/basic.feature&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 4: Create step_definitions directory inside features directory. Create basic_step.rb file inside step_definitions.&lt;br /&gt;
&lt;br /&gt;
      mkdir features/step_definitions&lt;br /&gt;
&lt;br /&gt;
Content of &amp;lt;b&amp;gt;features/step_definitions/basic_step.rb&amp;lt;/b&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      require 'rspec/expectations'&amp;lt;br&amp;gt;&lt;br /&gt;
      Given /The Action is ([A-z]*)/ do |action|&lt;br /&gt;
         @action = action&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      When /The Subject is ([A-z]*)/ do |subject|&lt;br /&gt;
        @subject = subject&lt;br /&gt;
      end&amp;lt;br&amp;gt;&lt;br /&gt;
      Then /The Greeting is (.*)/ do |greeting|&lt;br /&gt;
        greeting.should == &amp;quot;#{@action}, #{@subject}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
Cucumber uses regular expression after the keywords Given, When and Then to map lines in a feature file to step definitions.&lt;br /&gt;
&lt;br /&gt;
Step 5: Running cucumber command you will see the following output &lt;br /&gt;
&lt;br /&gt;
     Feature: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     Scenario: Hello World Scenario    # features\basic.feature:3&lt;br /&gt;
     Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
     When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
     Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
     &lt;br /&gt;
     1 scenario (1 passed)&lt;br /&gt;
     3 steps (3 passed)&lt;br /&gt;
     0m0.016s&lt;br /&gt;
     Step 6 : Modify the basic.features file to include 2 scenario&lt;br /&gt;
&lt;br /&gt;
Step 6 : Modify the basic.features file to include 2 scenarios &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;b&amp;gt;Feature&amp;lt;/b&amp;gt;: Hello World Feature&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario&lt;br /&gt;
     Given The Action is Hello&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&amp;lt;br&amp;gt;&lt;br /&gt;
     &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario&lt;br /&gt;
     Given The Action is Bye&lt;br /&gt;
     When The Subject is World&lt;br /&gt;
     Then The Greeting is Hello, World&lt;br /&gt;
&lt;br /&gt;
Step 7 : Running the cucumber command you will see the following output&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Hello World Scenario      # features\basic.feature:3&lt;br /&gt;
    Given The Action is Hello         # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World         # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&amp;lt;br&amp;gt;&lt;br /&gt;
    &amp;lt;b&amp;gt;Scenario&amp;lt;/b&amp;gt;: Bye World Scenario       # features\basic.feature:8&lt;br /&gt;
    Given The Action is Bye          # features/step_definitions/basic_steps.rb:3&lt;br /&gt;
    When The Subject is World        # features/step_definitions/basic_steps.rb:7&lt;br /&gt;
    Then The Greeting is Hello, World # features/step_definitions/basic_steps.rb:11&lt;br /&gt;
      expected: &amp;quot;Bye, World&amp;quot;&lt;br /&gt;
           got: &amp;quot;Hello, World&amp;quot; (using ==) (RSpec::Expectations::ExpectationNotMetError)&lt;br /&gt;
      ./features/step_definitions/basic_steps.rb:12:in `/The Greeting is (.*)/'&lt;br /&gt;
      features\basic.feature:11:in `Then The Greeting is Hello, World'&amp;lt;br&amp;gt;&lt;br /&gt;
    Failing Scenarios:&lt;br /&gt;
    cucumber features\basic.feature:8 # Scenario: Bye World Scenario&amp;lt;br&amp;gt;&lt;br /&gt;
    2 scenarios (1 failed, 1 passed)&lt;br /&gt;
    6 steps (1 failed, 5 passed)&lt;br /&gt;
    0m0.033s&lt;br /&gt;
&lt;br /&gt;
The Bye Scenario fails since the expected output is “Hello, World” but the received output is “Bye,World”.&lt;br /&gt;
&lt;br /&gt;
=Testing a Sample Rails Application using Cucumber=&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://railscasts.com/episodes/155-beginning-with-cucumber Sample Cucumber Application on Rails]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Cucumber easily integrates with Rails and hence can be used with Ruby applications which run on Rails. But there are a few steps which have to be followed before integrating cucumber with Rails. Let us consider we create a new applications using the following command : &lt;br /&gt;
&lt;br /&gt;
    rails new cucumber_app&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
and the new applications with its folders get created.&lt;br /&gt;
&lt;br /&gt;
we would have to install the gems required for running Cucumber with Rails and there are couple of gems that are required and they can be installed with the following command : &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    gem install cucumber&lt;br /&gt;
    gem install cucumber cucumber-rails &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We would have to then give “bundle install” command to install these new gems. We would then have to update the Gemfile to include these gems in the test group since they will be needed only in the test environment. There are a few other gems which are required for cucumber namely database_cleaner , rspec-rails and webrat.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    group :test do&lt;br /&gt;
       gem 'database_cleaner'&lt;br /&gt;
       gem 'cucumber'&lt;br /&gt;
       gem &amp;quot;rspec-rails&amp;quot;, &amp;quot;~&amp;gt; 2.0&amp;quot;, :require =&amp;gt; false&lt;br /&gt;
       gem &amp;quot;cucumber-rails&amp;quot;, &amp;quot;1.0.6&amp;quot;&lt;br /&gt;
       gem 'webrat'&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Cucumber required files can then be created by giving the following command. The output below the command shows the folders and files that get created for the cucumber test framework.&lt;br /&gt;
&lt;br /&gt;
    rails generate cucumber:install&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
     identical  script/cucumber&lt;br /&gt;
           chmod  script/cucumber&lt;br /&gt;
           exist  features/step_definitions&lt;br /&gt;
     identical  features/step_definitions/web_steps.rb&lt;br /&gt;
           exist  features/support&lt;br /&gt;
     identical  features/support/paths.rb&lt;br /&gt;
     identical  features/support/selectors.rb&lt;br /&gt;
     identical  features/support/env.rb&lt;br /&gt;
           exist  lib/tasks&lt;br /&gt;
     identical  lib/tasks/cucumber.rake&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The Step definitions folder contains ruby files which test the steps which are mentioned in feature file of the cucumber.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The paths.rb file is created in the support folder will contain the rules based on which the step definitions will be compared to the steps and matched. In case we come across any steps whose execution fails while running cucumber we would have to update the paths.rb file to resolve the issue.&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
env.rb is only loaded when the application is first started up and it sets up the environment required by Cucumber to execute.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
    rails generate model article title:string content:text &lt;br /&gt;
    rake db:migrate&lt;br /&gt;
    rake db:test:clone&lt;br /&gt;
    rails generate controller articles index &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
The above four commands generate models with the name article and with two fields title and content&lt;br /&gt;
and the next two commands migrate the db to production and the last commands creates controller for article and index objects.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
We have to go the home directory of our rails application and then run Cucumber with the following command.The result would then be shown to the user on how many and which steps succeeded and failed.  &lt;br /&gt;
&lt;br /&gt;
    =&amp;gt; cucumber&lt;br /&gt;
===Capybara=== &lt;br /&gt;
&amp;lt;ref&amp;gt;[http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-testing-web-apps-with-capybara-and-cucumber Web-app with Cucumber and Capybara]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need a tool that acts out the user stories that are part of the feature that is to be tested with Cucumber. Capybara can be used as that tool. Capybara is an integration testing tool for rack based web based applications. It simulates how a user would interact with a website. It can simulate the actions of a web browser and hence can interact with the application to receive web pages. It can also parse the HTML content and submits forms as the user would. One of the restrictions that Capybara has is that it cannot handle JavaScript. There are other tools like webdriver which can handle JavaScript but it runs a lot slower compared to Capybara.&lt;br /&gt;
&lt;br /&gt;
=Comparison of Unit Test Frameworks : Rspec, Shoulda and Cucumber=&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;2&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Rspec&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Shoulda&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Cucumber&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|RSpec is based on the Behaviour driven development model provided by Ruby. It has its origins in Test Driven Development and Domain Driven Design.&lt;br /&gt;
|Shoulda is based on the Test:Unit which is another Ruby Test Framework. It is based on Test Driven Development.&lt;br /&gt;
|Cucumber is a combination of Test Driven Development and Behaviour driven development. &lt;br /&gt;
|-&lt;br /&gt;
|RSpec provides a Domain Specific Language that can express code functionality and behaviour. &lt;br /&gt;
|Shoulda provides macros, helpers and matchers.&lt;br /&gt;
|Cucumber allows the test cases to be written in native language of the system &lt;br /&gt;
|-&lt;br /&gt;
|RSpec too allows nested describe structure.&lt;br /&gt;
|Shoulda has nested contexts which helps create different environment for different set of tests.&lt;br /&gt;
|Cucumber is similar to RSpec also provides nested structures &lt;br /&gt;
|-&lt;br /&gt;
|RSpec is expressive when it comes to understanding functionalities since they were designed have the human readability in mind. &lt;br /&gt;
|Shoulda has more descriptive names compared to its predecessor Test::Unit but its less expressive. &lt;br /&gt;
|Cucumber test are even more descriptive and allows business stakeholder to write and read them.&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Reference=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pvasude</name></author>
	</entry>
</feed>