框架預(yù)覽

IntelliJ IDEA
IntelliJ IDEA是公認(rèn)為最好的java開發(fā)工具之一,已內(nèi)置Gradle插件。免費(fèi)版(Community)已足夠滿足需求,下載安裝,Next...
Gradle
Gradle是一個(gè)基于Apache Ant和Apache Maven概念的項(xiàng)目自動(dòng)化建構(gòu)工具。拋棄了基于XML的各種繁瑣配置--易用顏值很重要!
前提:JDK環(huán)境。
配置Gradle環(huán)境:官網(wǎng)上有多種安裝配置方法,不怕折騰的可以嘗試Gradle Wrapper、SDKMAN等方法。這里采用常規(guī)的Install manually方法即可,與JDK的配置方法相似:
下載zip > 解壓 > 配置環(huán)境變量Gradle_HOME、PATH在IntelliJ IDEA中創(chuàng)建Gradle工程:
File > New > Project > Gradle > Next...



- Gradle配置文件
將工程所需的依賴包都寫到build.gradle文件中,IntelliJ IDEA會(huì)自動(dòng)加載。
dependencies {
compile 'org.jsoup:jsoup:1.10.2'
compile 'com.jayway.jsonpath:json-path:2.2.0'
compile 'commons-codec:commons-codec:1.10'
compile 'commons-dbutils:commons-dbutils:1.6'
compile 'mysql:mysql-connector-java:6.0.6'
compile 'com.google.inject:guice:4.1.0'
compile 'velocity:velocity:1.4'
testCompile 'org.uncommons:reportng:1.1.4'
testCompile 'org.testng:testng:6.0.1'
}
test{
useTestNG(){
suites(file('src/test/resources/testng.xml'))
}
options {
listeners << 'org.uncommons.reportng.HTMLReporter'
listeners << 'org.uncommons.reportng.JUnitXMLReporter'
}
}
ps:gradle test執(zhí)行測(cè)試用例時(shí),報(bào)錯(cuò),執(zhí)行g(shù)radle test --info或在...\IdeaProjects\APITest\build\reports\tests\test\index.html文件查看
Caused by: java.lang.ClassNotFoundException: com.google.inject.Injector
Caused by: java.lang.ClassNotFoundException: org.apache.velocity.context.Context
原因是guice、velocity都是ReportNG相關(guān)依賴,如果不添加會(huì)報(bào)錯(cuò),上面的build.gradle中已添加。
jsoup
jsoup是一款Java 的HTML解析器。重點(diǎn)是它提供了一套非常省力的API,構(gòu)造接口請(qǐng)求非常便捷。
Connection connection = Jsoup.connect(String url).header(String name, String value).userAgent(String userAgent).method(Method method).data(String name, String value)...
Request req = connection.request();
Response resp = connection.execute();
DbUtils
Commons DbUtils是Apache組織提供的一個(gè)對(duì)JDBC進(jìn)行簡(jiǎn)單封裝的開源工具類庫,使用它能夠簡(jiǎn)化應(yīng)用程序的開發(fā),同時(shí)也不會(huì)影響程序的性能。
JsonPath
JsonPath對(duì)于 JSON 來說相當(dāng)于 XPATH 對(duì)于 XML。這是一個(gè)簡(jiǎn)單的從文檔中抽取指定信息的工具,提供多種語言實(shí)現(xiàn)版本,包括:Javascript, Java, Python 和 PHP。
- 不使用Gson、fastjson等解析,是因?yàn)閷?duì)于測(cè)試來說,大多數(shù)情況下只是要獲得json中的特定值進(jìn)行斷言等其他操作,JsonPath語法簡(jiǎn)單,可以快速獲得。
TestNG+ReportNG
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use.目前流行的java測(cè)試框架。
ReportNG:An HTML/XML Reporting Plug-in for TestNG。TestNG插件,美化測(cè)試報(bào)告。
- ReportNG中文亂碼,已有解決方案,轉(zhuǎn)載參考。
測(cè)試執(zhí)行
- 在工程目錄下在執(zhí)行
gradle test

- 有兩份測(cè)試報(bào)告:一份是Gradle本身產(chǎn)生的(...\IdeaProjects\APITest\build\reports\tests\test\index.html),一份是ReportNG產(chǎn)生的(D:\IdeaProjects\APITest\build\reports\tests\test\html\index.html)。


