Thursday, October 3, 2013

Method Stub

Stub :

Stubs are dummy modules which are known as "called programs" which is used in integration testing (top down approach), used when sub programs are under construction. A stub may simulate the behavior of existing code (such as a procedure on a remote machine) or be a temporary substitute for yet-to-be-developed code.
Example 1:
  
   BEGIN
       Temperature = ThermometerRead(Outside)
       IF Temperature > 40 THEN
            PRINT "It's HOT!"
       END IF
   END

BEGIN ThermometerRead(Source insideOrOutside)
        RETURN 28
   END ThermometerRead


Example 2: A drive program to calculate_income_taxfunction may consist line like
Cout << “income tax on 50000 is:”;
Cout << calculate_income_tax(5000)<<“\n”;

Example 3:
void function_under_test(int& x, int& y) {
  ...
  p = price(x);
  ...
}
double price(int x) {return 10.00;}
The value returned by function price is good enough for testing. The real price() function may not yet have been tested, or even written.


Driver:
Drivers are kind of dummy modules which are known as "calling programs", which is used in bottom up integration testing, used when main programs are under construction.
Example 1: to move a fighter on the game, the driver code would be 
moveFighter(Fighter, LocationX, LocationY);
This driver code would likely be called from the main method. A white-box test case would execute this driver line of code and check 
fighter.getPosition() to make sure the player is now on the expected cell on the board.

Example 2:           Edouble calculate_salary (double hours, double rate)
{      cout<< “salary is : ”;
                                   return (hours*rate);
                                 }
The main program can be tested using this code
Example 3:
#include <iostream.h>
void get_input(x& cost, int& y);
int main( )
{
    double a;
    int b;
    char ans;
    do
    {
        get_input(a, b);
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
        cout << "a is " << a << endl;
        cout << "b is " << b << endl;
      
        cout << "Test again?"
             << " (Type y for yes or n for no): ";
        cin >> ans;
        cout << endl;
    } while (ans == 'y' || ans == 'Y');
    return 0;
}
Difference between stub and driver:

Stub
Driver

A piece of code that simulates the activity of missing component

A piece of code that passes test case to another piece of code

Stubs are created integration testing like Top-down approach 

Drivers are created integration testing like bottom-up approach

Stubs are simulations of the sub-code that otherwise is very gives full control to the test code

Driver takes care that the entry point of the application is masked by that of the test code and difficult to execute in the test code

A stub is a brief section of compliable code that serves as a class

A driver is a brief test program that demonstrates the functionality of a class or a portion of placeholder for future work

It is a temporary called program. It functions similarly like sub modules

It is a temporary Calling program. It functions similarly like main module for calling the sub when called by the main module

Stub is a simple routine that takes the place of the real routine Stubs let you check the interfaces and higher levels of the program

The driver approach is to write a program that passes input data to the unit under test and compares the output to the truth

Stub is a piece of special code that which is used to simulate the set up environment missing or not yet constructed

Driver is a code that which invokes the code to be tested

Stub is a skeleton of function having function header. This function can have actual statements or simple statements which can be      replaced with the actual code

Driver is a small program used to test a function

A driver creates necessary ‘Inputs’ required for      the Unit and then invokes the Unit

A driver creates necessary ‘Inputs’ required for the Unit and then invokes the Unit
Stub
Driver

Stubs can be "filled in" to form the actual method

Drivers can become automated test cases

Example: In module A and module B, module A      ready and module B is not. For integration testing of module A, a dummy module is created which stimulate like module B. This dummy module is Stub.

Example: module B cannot send or receive data from module A automatically so, in such case we have to transfer data from one module to another module by some external features. This      external feature used is called Driver.




References:
  1. Pressman, Roger S., Software Engineering: A Practitioner's Approach (P 459-462), New Delhi, 2011
  2. http://www.qualitytesting.info/forum/topics/difference-between-stubs-and
  3. http://www.cs.gmu.edu/~mcjunkin/cs112lectures/Stubs.htm
  4. http://forums.sureshkumar.net/testing-interview-technical-questions/13586-what-difference-between-stub-driver.html
  5. https://en.wikipedia.org/wiki/Method_stub

No comments:

Post a Comment