Wednesday, March 21, 2012

Running Selenium with TestNG

This week I had to rewrite a bunch of functional tests, mostly Selenium stuff. When I think about Java Unit tests the first thing that comes to my mind is the JUnit framework. All tests that I have written until past week were with JUnit, and was using it to run Selenium.

But I got one more requirement, since I was rewriting all tests I would like to change environments (development and pre-production) without dealing in the code where was running the tests. The problem was a simple one, just write configurations with URLs, expected values and so on, for each of my environments. A trivial example is using different URLs in selenium for the same tests. Well, how do this with JUnit?

First approach, using JUnit 3

In JUnit 3 (old one) you have Test Suites. Programatically you have a chance to read a properties file and give the information to your test classes. Well, this could work if I had not the principle: “Do not write test code if isn’t a test.” When I caught myself creating a “little” framework to test my application I realized “I’m in the wrong way”. Not that you should never code a “little” framework to simplify even more the tests, but i hadn’t the time to do it.

Second approach, using JUnit 4

Well, JUnit 4 is more flexible, newer and uses annotations; and I didn’t explored it well. After some digging i found JUnit’s Parameterized Tests, but again I had (bear with me in this one) “to code, code to test code”. And one more thing, JUnit 4 have Test Suites but, AFAIK, you only have a annotation version to define the classes for a suite.

JUnit wasn’t solving the problem

After some thought i had two choices, forget about environments, or spend some time coding new stuff that wasn’t tests or my application. Justice has to be made, JUnit is a excellent test framework but I think my objectives weren’t too “Unitwise”.

TestNG, next generation?

I heard about TestNG about a year ago and didn’t gave it enough attention, I was happy with JUnit. But, everyday is a new day and I had a problem, so I started to read about TestNG. I was skeptic at first, thinking “maybe this can do it, but I don’t have time to learn it all”. I was wrong, happily wrong.

http://marcovaltas.com/?p=49

No comments: