Archive

Posts Tagged ‘Quality’

TestNG or JUnit

7 August, 2011 7 comments

For many years now, I have always found myself going back to TestNG whenever it comes to doing Unit Testing with Java Code. Everytime, I picked up TestNG, people have asked me why do I go over to TestNG especially with JUnit is provided by the default development environment like Eclipse or Maven. Continuing the same battle, yesterday I started to look into Spring’s testing support. It is also built on top of JUnit. However, in a few minutes of using the same, I was searching for a feature in JUnit that I have always found missing. TestNG provides Parameterized Testing using DataProviders. Given that I was once again asking myself a familiar question – TestNG or JUnit, I decided to document this so that next time I am sure which one and why.

Essentially the same

If you are just going to do some basic Unit Testing, both the frameworks are basically the same. Both the frameworks allow you to test the code in a quick and effective manner. They have had tool support in Eclipse and other IDE. They have also had support in the build frameworks like Ant and Maven. For starters JUnit has always been the choice because it was the first framework for Unit Testing and has always been available. Many people I talk about have not heard about TestNG till we talk about it.

Flexibility

Let us look at a very simple test case for each of the two.

package com.kapil.itrader;
import java.util.Arrays;
import java.util.List;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class FibonacciTest
{
    private Integer input;
    private Integer expected;

    @BeforeClass
    public static void beforeClass()
    {
        // do some initialization
    }

    @Test
    public void FibonacciTest()
    {
        System.out.println("Input: " + input + ". Expected: " + expected);
        Assert.assertEquals(expected, Fibonacci.compute(input));
        assertEquals(expected, Fibonacci.compute(input));
    }
}

Well, this is example showcases I am using a version 4.x+ and am making use of annotations. Priori to release 4.0; JUnit did not support annotations and that was a major advantage that TestNG had over its competitor; but JUnit had quickly adapted. You can notice that JUnit also supports static imports and we can do away with more cumbersome code as in previous versions.

package com.kapil.framework.core;
import junit.framework.Assert;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class BaseTestCase
{
    protected static final ClassPathXmlApplicationContext context;

    static
    {
        context = new ClassPathXmlApplicationContext("rootTestContext.xml");
        context.registerShutdownHook();
    }

    @BeforeSuite
    private void beforeSetup()
    {
       // Do initialization
    }

    @Test
    public void testTrue()
    {
        Assert.assertTrue(false);
    }
}

A first look at the two code, would infer that both are pretty much the same. However, for those who have done enough unit testing, will agree with me that TestNG allows for more flexibility. JUnit requires me to declare my initialization method as static; and consequently anything that I will write in that method has to be static too. JUnit also requires me to have my initialization method as public; but TestNG does not. I can use best practices from OOP in my testing classes as well. TestNG also allows me to declare Test Suite, Groups, Methods and use annotations like @BeforeSuite, @BeforeMethod, @BeforeGroups in addition to @BeforeClass. This is very helpful when it comes to writing any level of integration testing or unit test cases that need to access common data sets.

Test Isolations and Dependency Testing

Junit is very effective when it comes to testing in isolation. It essentially means that there is you can not control the order of execution of tests. And, hence if you have two tests that you want to run in a specific order because of any kind of dependency, you can not do that using JUnit. However, TestNG allows you to do this very effectively. In Junit you can make workaround this problem, but it is not neat and that easy.

Parameter based Testing

A very powerful feature that TestNG offers is “Parameterized Testing”. JUnit has added some support for this in 4.5+ versions, but it is not as effective as TestNG. You may have worked with FIT you would know what I am talking about. However, the support added in JUnit is very basic and not that effective. I have modified my previous test case to include parameterized testing.

package com.kapil.itrader;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FibonacciTest
{
    private Integer input;
    private Integer expected;

    @Parameters
    public static List data()
    {
        return Arrays.asList(new Integer[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
    }

    @BeforeClass
    public static void beforeClass()
    {
        System.out.println("Before");
    }

    public FibonacciTest(Integer input, Integer expected)
    {
        this.input = input;
        this.expected = expected;
    }

    @Test
    public void FibonacciTest()
    {
        System.out.println("Input: " + input + ". Expected: " + expected);
        Assert.assertEquals(expected, Fibonacci.compute(input));
        assertEquals(expected, Fibonacci.compute(input));
    }

}

You will notice that I have used @RunWith annotation to allow my test case to be parameterized. In this case, the inline method – data() which has been annotated with @Parameters will be used to provide data to the class. However, the biggest issue is that the data is passed to class constructor. This allows me to code only logically bound test cases in this class. And, I will end up having multiple test cases for one service because all the various methods in the Service wil require different data sets. The good thing is that there are various open source frameworks which have extended this approach and added their own “RunWith” implementations to allow integration with external entities like CSV, HTML or Excel files.

TestNG provides this support out of the box. Not support for reading from CSV or external files, but from Data Providers.

package com.kapil.itrader.core.managers.admin;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.uhc.simple.common.BaseTestCase;
import com.uhc.simple.core.admin.manager.ILookupManager;
import com.uhc.simple.core.admin.service.ILookupService;
import com.uhc.simple.dataprovider.admin.LookupValueDataProvider;
import com.uhc.simple.dto.admin.LookupValueRequest;
import com.uhc.simple.dto.admin.LookupValueResponse;

/**
 * Test cases to test {@link ILookupService}.
 */
public class LookupServiceTests extends BaseTestCase
{

    @Test(dataProvider = "LookupValueProvider", dataProviderClass = LookupValueDataProvider.class)
    public void testGetAllLookupValues(String row, LookupValueRequest request, LookupValueResponse expectedResponse)
    {
        ILookupManager manager = super.getLookupManager();
        LookupValueResponse actualResponse = manager.getLookupValues(request);
        Assert.assertEquals(actualResponse.getStatus(), expectedResponse.getStatus());
    }
}

The code snippet above showcases that I have used dataProvider as a value to the annotations and then I have provided a class which is responsible for creating the data that is supplied to the method at the time of invocation. Using this mechanism, I can easily write test cases and its data providers in a de-coupled fashion and use it very effectively.

Why I choose TestNG

For me the Parameterized Testing is the biggest reason why I choose TestNG over Junit. However, everything that I have listed above is the reason why I always want to spend a few minutes in setting up TestNG in a new Eclipse setup or maven project. TestNG is very useful when it comes to running big test suites. For a small project or a training exercise JUnit is fine; because anyone can start with it very quickly; but not for projects where we need 1000s of test cases and in most of those test cases you will have various scenarios to cover.

http://kapilvirenahuja.com/tech/2011/08/07/testng-or-junit/

Quality has new meaning – it is shit

I do not find that title implied to me; but see it almost everyday being implied to others.

This is in a continuation to a post a while back. In that post, I talked about a project team and their view point on quality. Here is an addition to the same set of people. The conversation goes like

Project Manager: I was just checking on the defect count and noticed we have 24 open P1 and P2 in system. How are we are going to go live tomorrow. What just happened?

QA Manager 1: Nothing, it is just that my team currently does not have any work for Release 2. They had some time on their hands and hence pro-actively they started to test in R1 and logged these defects.

Project Manager 1: so what are you going to do?

QA Manager 1: Nothing, these are not R1 defects. Testing is closed. We wrapped that last week and all these have tobe logged in R2. These are not R1 defects.

Everyone else in the room which included Analyst, and Project Managers laughed about it and joked that this is funny and defects have to go away.

This was the last point of discussion in the meeting and I was shell shocked to say anything. Not that it would have mattered to these guys. I am responsible for the delivery of a different project and not the modules they were talking about. So, amongst all politics and shock I did not say anything. But, I was thinking what happened to quality here.

One of them was the QA Manager, who is responsible for quality of the project and she said “Testing is closed”. How would we feel if Toyota or Honda or any car manufacturer would say that decide to fix problems in the next batch or version of the car.

And what amuses me is that these discussions reach people who are Director IT and Vice President IT and they are in agreement of the fact that “Testing is closed”

 

Categories: Quality Tags: , , ,

The Architect’s Eye – Communicating Errors

In many of my projects, I have found architects guilty of preparing a design that leaves the error messages out of the question. And now, I come across an article that shows us 35 creative designs of showing a 404 page (http://www.onextrapixel.com/2011/03/09/the-secret-of-a-successful-error-page-with-35-amazing-404-page-designs/). As I was browsing some of these designs, I recall a designer I worked with – Beth. I learnt so much from her about design and especially Information Architecture. I have always found her looking at things differently be it work or a status update she did.

Coming back to error messages, in most applications I have noticed that error messages are cryptic like “An error has occurred, please check again and get back to System Administrator”. The user is needed to log a report with the call center and report what they were doing. Many a times a user would just ignore to do all that because it takes their time to do such stuff and they say we will come back and try another time – of course if it is not urgent. I see two issues here:

1. A user has been asked to do something that an application designer – An Architect could have done by designing the system right

2. The Application team has lost an opportunity of knowing where their application failed because a user chooses not to report it. They lost an opportunity to fix something pro-actively.

 

Categories: Design, Expert, Quality Tags: , ,

Managing Code

Do you write code locally for personal use/reference? If the answer is yes, then this post is for you. For others, it may be worth still to read it.

I have been working on a set of projects for last few weeks. Today, I wanted to make some major and possibly destructive changes to the code base. Before I started to change code, I took a backup of the local folder into another folder so that I have a revert point. At this point it hit me – I am missing version control, that I enjoy in my corporate world.

I dropped everything and started to setup my laptop with the version control. I did the following:

1. Downloaded SmartSVN or TortoiseSVN as subversion client and install on your machine.’

2. Download Subversion client for windows from http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=8100

3. Downoad Apache HTTP server to front-end the subversion server from http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91

4. Install svn client and apache. Steps are simple and can be found with the downloads.

5. Configure Apache wotk with SVN. Read the post http://svnbook.red-bean.com/en/1.0/ch06s04.html

6. Start Apache and hit http://localhost:80/svn/. Provide a different port number in case you changed the apache configurations.

7. Create a local repository on your hard disk by using the command line “svnadmin create foo”. This will create a repository in the same folder from where you ran the command.

8. Open a browser and type http://localhost:80/svn/foo. This will show a repository view to you.

Whola, you have your code base in repository. Enjoy it!!

Is Defect a defect when not defined in a Test Script?

18 April, 2008 3 comments

This question came up during an email thread that I had with some testers. Most of the testers had the view that a defect can be a defect even if it has not been defined in a test script, while I said that this is not true. The conversation went like this:

Tester: We can not put every scenario in a test script

ME: Yes you can

Tester: Can not because we can not think of scenarios while writing test scripts

ME: Then you should. Testers also think when they are testing, so what stops them from thinking when they are creating test scripts

Tester: It is not possible and also it is not feasible

ME: Not possible, I will disagree. Let us focus on the Not Feasible part

Tester: It becomes very difficult to document everything as then the scripts will run into multiple pages and maintaining them would be an overhead

ME That is not reason enough not to capture all that can be in a test script. You have to accept the fact that a test script can be incorrect. For a defect to be a defect, it has to be mapped against a missing requirement that should come from a document that can a client would have provided. If you can not provide a document/reference for the missing functionality, then there is not reason you can call it a defect. It is fine to log it as a TAR and discuss as a change request, but a defect i do not agree to.

Beyond this point, I started finding testers fumbling for words. No one out there could give me a solid answer to convince me. What are your views on the same? I blogged about why do i do unit testing and why do i feel we can cover all the scope in a test script. We developers do that for Unit testing, why can testers not?

Categories: Quality Tags:

Why do I Test?

16 April, 2008 2 comments

Most of you who would read this would have done some level of testing in their programming career. Many of you would appreciate the fact that unit testing exists in the world.

Analogy that works for me is that in real life, I test all the time. I test so much that it has become a habit so much so, that I now do not even realize that I am testing. If I think about it, I can site many examples. The reason I test so often is because I want to be sure that things will work when I need them to.

When I became a programmer, I found myself doing that same just out of habit of testing everything. Since I started to code, I have come across may programmers who have reasons not to test and they resist testing al their life. Once they find themselves in a the arena of no testing, they enjoy and as per them are most productive. As for me, there have been times, when I have not tested myself, and I have been productive as well, but I have always found that the applications that I design/code with writing tests end up being prototypes.

I see many benefits of testing, and thats the reason when I start a project, I make sure that the first thing that is done is to setup an integrated unit testing tool for me to write my tests. In other words, without unit tests, I do not trust my own code and I would not even go into changing it – I can never know after a few changes if I broke something. Unit tests act as an umbrella that saves you from a downpour for defects in the application.

Passing my code along to a new developer has been a challenge. Ever tried giving a new programmer a code walk through. I tried test walk through and it has been a wonderful experience. They understood the code once and were able to start coding faster. I like to call my tests as API documentation for my code.

I have been unit testing in projects for last 3.5 years now and always I have found in a later stage how valuable those tests were. Coming to Flex, I started to find similar options and Flex Cover was the answer. Simple to get started and now very soon I would have my testing suite for Flex as well.

Eager to hear what your experience have been.

Categories: Beginner, Quality Tags: ,

Testing Flex applications

Borland has announced the latest version of its Silk testing tool.

Categories: Beginner, Flex, Quality Tags: ,
Follow

Get every new post delivered to your Inbox.