CSC/ECE 517 Fall 2010/ch1 1f ap: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 44: Line 44:


'''Arguments to it ''' :: it( ) method takes a single String, an optional Hash and an optional block
'''Arguments to it ''' :: it( ) method takes a single String, an optional Hash and an optional block


2. '''describe() ::'''  We use describe() method to define example group aka  test case in Test::Unit.        This acts as an abstraction to many code examples -i,e its a wrapper around  multiple it() functions .
2. '''describe() ::'''  We use describe() method to define example group aka  test case in Test::Unit.        This acts as an abstraction to many code examples -i,e its a wrapper around  multiple it() functions .
Line 52: Line 54:


'''Arguments''':Arguments passed to describe function are string and they describe the subset of behaviour exhibited by the object.
'''Arguments''':Arguments passed to describe function are string and they describe the subset of behaviour exhibited by the object.


3. '''Methods'''
3. '''Methods'''
We can have other methods apart from it() , inside the describe block 
* Before and after methods.
* helper methods.
1. Before and after methods.
* before(:[each or all] ):Some times we need to run some code  or some setup prior to executing the code examples (test methods).We do this by using the before method, this gets executed before any code examples. It takes either :each or :all as parameter . If we use :each then before method gets executed before running each code example. If we use :all  then before gets executed only once at the start  before running any code examples.
eg
* after(:[each or all] ) : This is counterpart for before function. If we need to run some code or some clean up post running the code examples, we use this function. It takes either :each or :all as parameter . If we use :each then after method gets executed after  running each code example. If we use :all  then after method gets executed only once, at the end after running all the code examples.


2.Helper methods:Sometimes we require code that is common across all the code examples.Instead of repeating the same code in each code example.we write helper method and this be utilized by all of the code examples present in the code example group.This method helps us to overcome duplication in the code.Even these helper methods can be used across the example groups.


<u>Need to paste code over here with formaatting</u>


===shoulda===
===shoulda===

Revision as of 11:09, 7 September 2010

Introduction


Benefits of unit test frameworks

  1. Test Driven Development

Unit testing frameworks for Ruby

Test::Unit

Rspec

Rspec is one more unit test framework available for ruby programmers and this framework adopts Behaviour Driven development paradigm .In this we write test cases that resembles spoken English, even people with little idea about the test cases can comprehend what the test case is doing.

Some keyword that you need to know before jumping into technical aspects about Rspec.

  • Expectations - These are assertion statments used inside the test case.
  • Example - This is a test method and collection of expectations(assertion statements).
  • Example Group - Collection of examples is called Example group and also called test case.

Lets take an example and explain on how to write a unit test case using Rspec framework.

    describe "Checking Account" do 
        it  "should have a balance greater than 0"  do 
             CheckingAccount = Account.new
             CheckingAccount account.balance.should > Money.new(0, :USD)
        end 
        it  “...”  do 
             //some expectations
        end 
    end 


Lets go through elements of this test case aka code example group.

1. it ()  :: This is called code example and it is similar to test method in Test::unit .This method takes string as an argument and the string describes the functionality/ behaviour we are going to test about the system . it() method has a code block , which is collection of expectations also called assertions.Expectations are enclosed between the do ... end keywords.

General structure of it() method:.

will paste the code later over here , need some formatting.

Arguments to it  :: it( ) method takes a single String, an optional Hash and an optional block


2. describe() :: We use describe() method to define example group aka test case in Test::Unit. This acts as an abstraction to many code examples -i,e its a wrapper around multiple it() functions .

General structure of describe() construct looks like this.

need to paste code over here and it requires formatting

Arguments:Arguments passed to describe function are string and they describe the subset of behaviour exhibited by the object.


3. Methods We can have other methods apart from it() , inside the describe block

  • Before and after methods.
  • helper methods.

1. Before and after methods.

  • before(:[each or all] ):Some times we need to run some code or some setup prior to executing the code examples (test methods).We do this by using the before method, this gets executed before any code examples. It takes either :each or :all as parameter . If we use :each then before method gets executed before running each code example. If we use :all then before gets executed only once at the start before running any code examples.

eg

  • after(:[each or all] ) : This is counterpart for before function. If we need to run some code or some clean up post running the code examples, we use this function. It takes either :each or :all as parameter . If we use :each then after method gets executed after running each code example. If we use :all then after method gets executed only once, at the end after running all the code examples.

2.Helper methods:Sometimes we require code that is common across all the code examples.Instead of repeating the same code in each code example.we write helper method and this be utilized by all of the code examples present in the code example group.This method helps us to overcome duplication in the code.Even these helper methods can be used across the example groups.

Need to paste code over here with formaatting

shoulda


Comparison of unit test frameworks


References and conclusion