开发者

Custom annotations to configure tests

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 som开发者_Go百科e 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜