Introduction to TestNG Data Driven
TestNG数据驱动
testng的功能很强大,利用@DataProvider可以做数据驱动,数据源文件可以是EXCEL,XML,YAML,甚至可以是TXT文本。
@DataProvider注解简介:
@DataProvider标记专门为测试方法提供参数的方法。这类方法必须返回Object[ ][ ]类型的二维数组或者Iterator
@DataProvider的小例子:
import java.lang.reflect.Method; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class test { @DataProvider(name = "user") public Object[][] createUser(Method m) { System.out.println(m.getName()); return new Object[][] { { "root", "root" }, { "test", "root" }, { "test", "test" } }; } @Test(dataProvider = "user") public void verifyUser(String username, String password) { System.out.println("Verify User : " + username + ":" + password); assert username.equals(password); } }
如上所示@DataProvider注解了createUser方法,返回的二位数组里有三行数据,每行两列。
所以@Test(dataProvider = "user")注解的verifyUser方法有两个参数,用来接收每一行的两个数据,如果createUser返回的数据数组的列数和verifyUser的参数个数不同就会报错的。
因为返回的有三行,所以verifyUser会被执行三次。结果如下:
PASSED: verifyUser("root", "root") FAILED: verifyUser("test", "root") PASSED: verifyUser("test", "test")
CSV文件数据读取和@DataProvider
我自己做了一个以csv为例的测试架子,部分代码可通用。
CSV文件读取类(可通用,目录自己可以修改,也可改变成读取EXCEL、TXT等文件):
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.regex.Matcher; public class CSVData implements Iterator<Object[]> { private BufferedReader br = null; //行数 private int rowNum = 0; //获取次数 private int curRowNo = 0; //列数 private int columnNum = 0; //key名 private String[] columnName; //csv中所有行数据 private List<String> csvList; //实际想要的行数据 private List<String> csvListNeed; /* * 在TestNG中由@DataProvider(dataProvider = "name")修饰的方法 * 取csv时,调用此类构造方法(此方法会得到列名并将当前行移到下以后)执行后,转发哦 * TestNG自己的方法中去,然后由它们调用此类实现的hasNext()、next()方法 * 得到一行数据,然后返回给由@Test(dataProvider = "name")修饰的方法,如此 * 反复到数据读完为止 * * * @param filepath CSV文件名 * @param casename 用例名 */ public CSVData(String fileName, String caseId) { try { File directory = new File("."); String ss = "resources."; File csv = new File(directory.getCanonicalFile() + "\\src\\test\\" + ss.replaceAll("\\.", Matcher.quoteReplacement("\\")) + fileName + ".csv"); br = new BufferedReader(new FileReader(csv)); csvList = new ArrayList<String>(); while (br.ready()) { csvList.add(br.readLine()); this.rowNum++; } String stringValue[] = csvList.get(0).split(","); this.columnNum = stringValue.length; columnName = new String[stringValue.length]; for (int i = 0; i < stringValue.length; i++) { columnName[i] = stringValue[i].toString(); } this.curRowNo++; csvListNeed = new ArrayList<String>(); for (int i = 1; i < rowNum; i++) { String values[] = csvList.get(i).split(","); if (caseId.equals(values[0])) { csvListNeed.add(csvList.get(i)); } } this.rowNum = 2;//就取一行 } catch (Exception e) { e.printStackTrace(); } } @Override public boolean hasNext() { if (this.rowNum == 0 || this.curRowNo >= this.rowNum) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } return false; } else { return true; } } @Override public Object[] next() { /* * 将数据放入map */ Map<String, String> s = new TreeMap<String, String>(); String csvCell[] = csvListNeed.get(0).split(","); for (int i = 0; i < this.columnNum; i++) { String temp = ""; try { temp = csvCell[i].toString(); } catch (ArrayIndexOutOfBoundsException ex) { temp = ""; } s.put(this.columnName[i], temp); } Object r[] = new Object[1]; r[0] = s; this.curRowNo++; return r; } @Override public void remove() { throw new UnsupportedOperationException("remove unsupported"); } }
这个类实现了Iterator
数据驱动类:
import java.lang.reflect.Method; import java.util.Iterator; import org.testng.annotations.DataProvider; public class DataProviderTest { /** * @DataProvider的返回值类型只能是Object[][]与Iterator<Object>[] * * @param method * @return */ @DataProvider public Iterator<Object[]> dataSource(Method method) { return (Iterator<Object[]>) new CSVData(method.getDeclaringClass().getSimpleName(), method.getName()); } }
Method方法是通过反射获取的,总之哪个方法调用我Method就是那个方法。
method.getDeclaringClass().getSimpleName()可以获取方法所属的类的类名。
我这里规定了csv的文件名就是测试类的类名,用例名就是方法名。
return (Iterator
测试类:
import java.util.Map; import org.testng.annotations.Test; public class DataTest extends DataProviderTest { @Test(dataProvider = "dataSource") public void id2(Map<String, String> data) { System.out.println(data); } @Test(dataProvider = "dataSource") public void id1(Map<String, String> data) { System.out.println(data); } }
输出结果如下:
PASSED: id1({caseId=id1, flag=Y, property=flowModel, type=com.mybank.bkloanapply.core.model.BaseModel, value=BaseModel.csv@1}) PASSED: id2({caseId=id2, flag=M, property=context, type=java.util.Map, value=a:Object.csv@1})
总结
通过以上例子可以看到,无论@DataProvider注解的方法返回的是Object[ ][ ]还是Iterator
The above is the detailed content of Introduction to TestNG Data Driven. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

AdeadlockinJavaoccurswhentwoormorethreadsareblockedforever,eachwaitingforaresourceheldbytheother,typicallyduetocircularwaitcausedbyinconsistentlockordering;thiscanbepreventedbybreakingoneofthefournecessaryconditions—mutualexclusion,holdandwait,nopree

UseOptional.empty(),Optional.of(),andOptional.ofNullable()tocreateOptionalinstancesdependingonwhetherthevalueisabsent,non-null,orpossiblynull.2.CheckforvaluessafelyusingisPresent()orpreferablyifPresent()toavoiddirectnullchecks.3.Providedefaultswithor

The core of SpringDataJPA and Hibernate working together is: 1. JPA is the specification and Hibernate is the implementation, SpringDataJPA encapsulation simplifies DAO development; 2. Entity classes map database structures through @Entity, @Id, @Column, etc.; 3. Repository interface inherits JpaRepository to automatically implement CRUD and named query methods; 4. Complex queries use @Query annotation to support JPQL or native SQL; 5. In SpringBoot, integration is completed by adding starter dependencies and configuring data sources and JPA attributes; 6. Transactions are made by @Transactiona

Understand JCA core components such as MessageDigest, Cipher, KeyGenerator, SecureRandom, Signature, KeyStore, etc., which implement algorithms through the provider mechanism; 2. Use strong algorithms and parameters such as SHA-256/SHA-512, AES (256-bit key, GCM mode), RSA (2048-bit or above) and SecureRandom; 3. Avoid hard-coded keys, use KeyStore to manage keys, and generate keys through securely derived passwords such as PBKDF2; 4. Disable ECB mode, adopt authentication encryption modes such as GCM, use unique random IVs for each encryption, and clear sensitive ones in time
![LOL Game Settings Not Saving After Closing [FIXED]](https://img.php.cn/upload/article/001/431/639/175597664176545.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
IfLeagueofLegendssettingsaren’tsaving,trythesesteps:1.Runthegameasadministrator.2.GrantfullfolderpermissionstotheLeagueofLegendsdirectory.3.Editandensuregame.cfgisn’tread-only.4.Disablecloudsyncforthegamefolder.5.RepairthegameviatheRiotClient.

The Pattern class is used to compile regular expressions, and the Matcher class is used to perform matching operations on strings. The combination of the two can realize text search, matching and replacement; first create a pattern object through Pattern.compile(), and then call its matcher() method to generate a Matcher instance. Then use matches() to judge the full string matching, find() to find subsequences, replaceAll() or replaceFirst() for replacement. If the regular contains a capture group, the nth group content can be obtained through group(n). In actual applications, you should avoid repeated compilation patterns, pay attention to special character escapes, and use the matching pattern flag as needed, and ultimately achieve efficient

Chrome bookmark editing is simple and practical. Users can enter the bookmark manager through the shortcut keys Ctrl Shift O (Windows) or Cmd Shift O (Mac), or enter through the browser menu; 1. When editing a single bookmark, right-click to select "Edit", modify the title or URL and click "Finish" to save; 2. When organizing bookmarks in batches, you can hold Ctrl (or Cmd) to multiple-choice bookmarks in the bookmark manager, right-click to select "Move to" or "Copy to" the target folder; 3. When exporting and importing bookmarks, click the "Solve" button to select "Export Bookmark" to save as HTML file, and then restore it through the "Import Bookmark" function if necessary.
!['Java is not recognized' Error in CMD [3 Simple Steps]](https://img.php.cn/upload/article/001/431/639/175588500160220.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
IfJavaisnotrecognizedinCMD,ensureJavaisinstalled,settheJAVA_HOMEvariabletotheJDKpath,andaddtheJDK'sbinfoldertothesystemPATH.RestartCMDandrunjava-versiontoconfirm.
