简体   繁体   中英

How to share a variable between two test classes?

public class Reg {
   @BeforeClass
   public void setUp() throws Exception
   {    
     // Generate a unique email every time 
     String email= UniqueEmail.now()+"@test.com";
     .....
    }
   @Test()
   public void Reg1() throws Exception 
   {
      // Registration Test
   }
}

public class Login{
   @BeforeClass
   public void setUp() throws Exception
   {    
     //How to parse in the email variable from Class Reg?
    }
   @Test()
   public void Login() throws Exception 
   {
      // Use the email generated from class A
      selenium.type("EmailID", email)
   }
}

Class Reg will always be executed before Class Login . How can I store the email variable after executing Class Reg so that it can be used in Class Login ?

创建一个包含所有全局变量的类,并使用该类扩展所有类(包含全局变量)

Here is the best solution I have found for sharing variables between two or more classes, which is also the simplest one.

Create a class on the top of everything and declare your variable in there:

class global {
  static String email;
}

You can then share 'email' -- using it as 'global.email' -- between the main method, other public static methods within the public class and independent classes -- in fact between everything.

as I understand the example, each class represent a test case. Test cases should not have any dependencies among themselves as, for once, the execution order is not guaranteed by the test framework. You should not be doing this.

Unfortunately, you question is vague enough so I do not understand what you are trying to accomplish and thus cannot give you pointers on a better way of achieving your goals.

how about putting it in a static member of Reg class and then accessing it in Login?

Alternatively you should think of your tests as independent as possible which means in the second test you should register a new user and then test login process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM