Data-driven is an important part of automated testing, the data source of the scheme is also eight of flowers, such as the use of external files, directly in the @dataprovider write dead, and so on, we introduce the use of enum to do the data source, first look at the syntax of the enum:
public enum Testenum {OK ("success"), Fail ("fail");p rivate int retcode;private String msg;private testenum (int RetCode, String msg) {This.retcode = Retcode;this.msg = msg;} public int Getretcode () {return retCode;} public void Setretcode (int retCode) {this.retcode = RetCode;} Public String getmsg () {return msg;} public void Setmsg (String msg) {this.msg = msg;} @Overridepublic String toString () {return retcode+ "" +MSG;} public static void Main (string[] args) {testenum[] vs = Testenum.values (); System.out.println (Vs[0].getmsg ()); System.out.println (TestEnum.OK.getRetCode ()); System.out.println (TESTENUM.OK.GETMSG ()); System.out.println (testenum.valueof ("OK"). getmsg ());}}
After copying the above code, you can run it directly!
The above code can be understood as having two sets of data, each with two data, one retcode and one MSG. Now load the enum into the @dataprovider:
public class Testdatademo { @DataProvider public object[][] Dataprovider () {testenum[] vs = Testenum.values (); O bject[][] obj = new Object[vs.length][];for (int i = 0; i < vs.length; i++) {Obj[i] = new Object[]{vs[i]};} return obj; } @Test (dataprovider= "Dataprovider") public void Testdemo (Testenum te) { System.out.println (Te.getretcode ()) ; System.out.println (Te.getmsg ()); } }
The above code can also be run directly, after running, the results are as follows:
The greatest advantage of this approach is that the data itself is placed in one place with the persisted object, and it is convenient to call.
In fact, the use of external files, you can also persist data objects, but the original example I put in a map, the same can be used to change the map to a Pojo object, the call will be very convenient.
As to which way good, everyone to experience, to achieve it by themselves!
Java uses enum with testng to do data-driven examples