Testing:

Testing is nothing but checking the expected results with actual results.

Test Case:

Test Case is a Test Plan where expected results will be compared with actual results with specific input values. To test one functionality it is recommended to write more test cases with both wrong data and write data.

Unit Testing

The testing performed by the programmer on his own piece of code is called as Unit Testing.

Peer Testing

The unit testing performed by the programmer on other programmers code is called Peer Testing.

Ø  Unit Testing , Peer Testing are the responsibility of the programmer. The remaining testing’s like Integration Testing, Performance Testing, Navigation Testing and etc are the responsibility of the QA department.

Test Suite

Test Suite provides the environment to run all the Test Cases.

What is Junit?

v  Junit is a program used to perform unit testing of virtually any software. Junit testing is accomplished by writing test cases using java, compiling these test cases and running the resultant classes with a JUnit Test Runner.

v  I will explain a little bit about software and unit testing in general. What and how much to test depends on how important it is to know that the tested software works right, in relation to how much time you are willing to dedicate to testing. since you are reading this, then for some reason you have decided to dedicate at least some time to unit testing.

Example

ü  I’m Currently developing an SMTP server. An SMTP server needs to handle email address of the format specified in RFC documents. To be confident that my SMTP server compiles with the RFC, I need to write a test case for each allowable email address type specified in the RFC. In the RFC says that the character “#” is prohibited, then I should write a test case that sends an address with “#” and verifies that the SMTP server rejects it. You don’t need to have a formal specification document to work from. If I wasm’t aiming for compliance, I could just decide that it’s good enough if my server accepts email address consisting only of letters, numbers and “@”, without caring whether other addresses succeed or fail.
ü  Another important point to understand is that everything is better if you make your tests before you implement the features. For my SMTP server, I did some prototyping first to make sure that my desing will work, but I did not attempt to satisfy my requirements with that prototype. I am now writing up my test cases and running them against existing SMTP server implementations(like Send mail). When I get to the implementation stage my work will be extremely well defined.Once my implementation satisfies all of the unit tests, just like send mail does then my implementation will be completed.

Note: Every thing I ‘ve said so far has to do with unit testing, not JUnit specifically. Now on to JUnit
.
               JUnit was Originally written by Erich Gamma and Kent Beck.

Installing Junit

ü  Downloading
You can download Junit 4.x from http://www.junit.org/index.html in the zipped format.

ü  Installation

Below are the installation steps for installing JUnit

1.      UnZip the JUnit 4.x zip file
2.      Add junit-4.x.jar to the CLASSPATH or we can create a bat file “setup.bat” in which we can write “set CLASSPATH=.;%CLASSPATH%;junit-4.x.jar;”. So whenever we need to use Junit in our project then we can run this setup.bat file to set the classpath.

Test Coverage-Quantity of test cases

ü  With respect to test coverage, an ideal test would have test cases for every conceivable variation type of input, not every possible instance of input. You should have test cases for combination or permutations of all variation types of the implemented operations. You use your knowledge of the method implementation(including possible implementation changes with you want to allow for) tomorrow the quantity test cases way down.

Test Expressions
The most important thing is to write tests like
  (expectedResult == obtainedResult)
    Or
expectedResult.equals(obtainedResults)
ü  You can do that without JUnit , of course. For the purpose of this document, I will call the smallest unit of testing, like the expression above, a test expression. All that JUnit does is give you flexibility in grouping your test expressions and convenient ways to capture and or summarize test failures.
Class junit.framework.Assert

ü  The Assert class has a bunch of static convenience methods to help you with individual test expressions.For example, without JUnit I might code
If(obtainedResult ==null || !expectedResult.equals(obtainedResult))
Throw new MyTestException(“Bad output for # attempt”);

Interface junit.framework.Test

ü  An object that you can run with the JUnit infrastructure is a Test. But you can’t just implement Test and run that object. You can only run specially created instances of Test Case.
Abstract class junit.framework.TestCase
A test case defines that fixture to run multiple tests. To define a test case

1.      Implement a subclass of TestCase
2.      Define instance variables that store the state of the fixture
3.      Initialize the fixture state by overriding setup()
4.      Clean-up after a test by overriding teardown()

ü  setup() method is executed for every test case method execution. In this method we can perform initialization activity.
ü  Teardown() method is executed at the end of each Test Case method. In this method we can perform cleanup activity or un initialization.
ü  Every Test case class must and should extend the TestCase class
ü  Every Test Case should follow the following conventions



Coding Conventions
1.      Name of the test class must end with “Test”.
2.      Name of the method must begin with “test”
3.      Return type of a test method much be void
4.      Test method must not throw any exception
5.      Test method must not have any prarmeter.

Class.junit.framework.TestSuite

ü  A TestSuite is just an object that contains an ordered list of runnable Test objects. TestSuite also implement Test() and are run nable. To run a TestSuite is just to run all of the elemental Tests in the specified order, where by elemental tests, I mean tests for single method like we  used in the Abstract class junit.framewok.TestCase.TestSuites ca be nested. Remember that for every elemental Test run there methods are actually invoked, setup + test method + teardown.
This is how TestSuites are used.
TestSuite s = new TestSuite()
s.addTest(new TC(“tcm”));
a.addTest(new TD(“tdm”));
s.addTestSuite(AnotherTestSuiteImpleentation.class);
s.run(new TestResult());

Class.Junit.textuit.TestRunner.
A command line based tool to run tests.
Java junit.textui.TestRunner [-wait] TestCaseClass.
Test Runner expects the name of a TestCase as argument. If this class defines a static suite method it will be invoked and the returned test is run. Otherwise all the methods starting with “text” having no argument are run.
When the wait command line argument is given TestRunner waits until the users type RETURN.
TestRunner prints a trace as the test are executed followed by a summary at the end.
Examples
If you want to work with Junit you need to write three classes
1.      Main Class(ie our Actual Logic)
2.      TestCase class
3.      Use TestRunner class to run all TestCase classes
Calc.java
 Class Calc
{
  Public int add(int a , int b)
{
  Return a+b;
       }
     Public int sub(int a, int b)
   {
     Return a-b;
  }
Public int div(int a , int b)
{
    Return a/b;
}
}

Write TestCase Class to Test the Functionality
Import  junit.framework.TestCase;
Class CalcTest extends TestCase
{
  Calc c;
//Initialization code
Public void setup()
{
  C= new Calc();
}
//cleanup code
Public void tearDown()
{
  C= null;
}
//actual testCase methods
Public void testAdd()
{
  assertEquals(2,c.add(2,1));
}
}




Next
This is the most recent post.
Previous
Older Post

Post a Comment

 
Top