views:

64

answers:

2

First of al let me start off by saying I think custom annotations can be used for this but i'm not totally sure.

I would like to have a set of annotations that I can decorate some test classes with. The annotations would allow me to configure the test for different environments. Example:

public class Atest extends BaseTest{ 
     private String env;

     @Login(environment=env)
     public void testLogin(){
     //do something
     }

     @SignUp(environment=env)
     public void testSignUp(){
     //do something
     }
}

The idea here would be that the login annotation would then be used to lookup the username and password to be used in the testLogin method for testing a login process for a particular environment.

So my question(s) is this possible to do with annotations? If so I have not been able to find a decent howto online to do something like this. Everything out there seems to be your basic here's how to do your custom annotations and a basic processor but I haven't found anything for a situation like this. Ideas?

A: 

Nice idea. Maybe you could write an aspect, that intercepts methods annotated like this, looking up the username and password and set these to ThreadLocal members in the class (ThreadLocal if you want to use it in multithreaded environments). The test method gets then the username and password from the members.

Dominik
Exactly. The major issue is that when moving from one environment to another is that the test data used in environment A is not available in environment B.In addition the data within each environment is transient; it may be purged after a couple of months. So what I essentially would like to be doing is have the tests describe the type of data it needs and have some other component find data to match and provide it. For example I need an account with a login to the website with branding for partner abc. I’m only describing what I need and not hard-coding data. I think aop addresses this nicely
ace
A: 

Look at the "group" feature in TestNG. You can do something like this:

  public class LoginTest {
     private UserData userData;

     @BeforeMethod(groups="activedirectory")
     public void setup() {
         userData = ...
     }

     @BeforeMethod(groups="ldap")
     public void setup() {
         userData = ...
     }

     @Test(groups={"activedirectory","ldap"})
     public void testLogin() {
         // ...
     }
 }

Then you can choose whether to run the "ldap" tests or the "activedirectory" tests depending on the test server. I use the groups feature to separate "unit" tests from "functional" tests, for example, where the latter require a fully configured environment and the former don't. I also put some tests in the "manual" group that only run on developer machines.

Chris Dolan