目錄
(一)TestNG學(xué)習(xí)之路—HelloWorld入門
(二)TestNG學(xué)習(xí)之路—注解及屬性概覽
(三)TestNG學(xué)習(xí)之路—TestNG.xml/YAML
(四)TestNG學(xué)習(xí)之路—注解詳述之@Test
(五)TestNG學(xué)習(xí)之路—注解詳述之參數(shù)化
(六)TestNG學(xué)習(xí)之路—注解詳述之@Factory
(七)TestNG學(xué)習(xí)之路—注解詳述之忽略測(cè)試
(八)TestNG學(xué)習(xí)之路—注解詳述之并發(fā)
(九)TestNG學(xué)習(xí)之路—失敗測(cè)試重跑
(十)TestNG學(xué)習(xí)之路—編碼執(zhí)行TestNG
(十一)TestNG學(xué)習(xí)之路—BeanShell高級(jí)用法
(十二)TestNG學(xué)習(xí)之路—注解轉(zhuǎn)換器
(十三)TestNG學(xué)習(xí)之路—方法攔截器
(十四)TestNG學(xué)習(xí)之路—TestNG監(jiān)聽器
(十五)TestNG學(xué)習(xí)之路—依賴注入
(十六)TestNG學(xué)習(xí)之路—測(cè)試報(bào)告
(十七)基于TestNG+Rest Assured+Allure的接口自動(dòng)化測(cè)試框架
前言
用過Jmeter的童鞋肯定都聽說過Beanshell象缀,BeanShell是一種松散類型的腳本語言(和JS類似),一種完全符合java語法的java腳本語言消玄,但其也擁有自己的語法和方法叠骑,足以可見其功能的強(qiáng)大只磷。更讓你吃驚的是舅锄,TestNG居然可以同Beanshell結(jié)合薄翅,構(gòu)建強(qiáng)大的testng.xml配置饥瓷。
環(huán)境配置
登錄beanshell官網(wǎng)下載bsh-2.0b4.jar伙狐,放到$JAVA_HOME/jre/lib/ext目錄下涮毫。更詳細(xì)的說明可以參考beanshell手冊(cè)。
To install as an extension place the bsh.jar file in your
$JAVA_HOME/jre/lib/ext folder. (OSX users: place the bsh.jar in
/Library/Java/Extensions or ~/Library/Java/Extensions for individual users.)
Or add BeanShell to your classpath like this:
windows: set classpath %classpath%;bsh-xx.jar
示例
當(dāng)在<script>標(biāo)簽出現(xiàn)在testng.xml時(shí)贷屎,TestNG將忽略當(dāng)前test標(biāo)簽下的組和方法的<include>窒百,<exclude>標(biāo)簽,您的BeanShell表達(dá)式將是決定是否執(zhí)行測(cè)試方法的唯一因素豫尽。
編寫測(cè)試類如下:
import org.testng.Assert;
import org.testng.annotations.*;
@Test(groups = "test1")
public class TestNGHelloWorld1 {
@BeforeTest
public void bfTest() {
System.out.println("TestNGHelloWorld1 beforTest!");
}
@Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = ".*zero")
public void helloWorldTest1() {
System.out.println("TestNGHelloWorld1 Test1!");
int c = 1 / 0;
Assert.assertEquals("1", "1");
}
@Test()
@Parameters(value = "para")
public void helloWorldTest2(@Optional("Tom")String str) {
Assert.assertEquals("1", "1");
System.out.println("TestNGHelloWorld1 Test2! "+ str);
}
@AfterTest
public void AfTest() {
System.out.println("TestNGHelloWorld1 AfterTest!");
}
}
testng.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" group-by-instances="true">
<test verbose="2" preserve-order="true" name="Test">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
groups.containsKey("test1")
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="TestNGHelloWorld1"/>
</classes>
</test>
</suite>
執(zhí)行結(jié)果如下:
TestNGHelloWorld1 beforTest!
TestNGHelloWorld1 Test1!
TestNGHelloWorld1 Test2! Tom
TestNGHelloWorld1 AfterTest!
===============================================
All Test Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
由此可見篙梢,beanshell可讓測(cè)試/開發(fā)人員更靈活地對(duì)testng.xml進(jìn)行配置。但需要關(guān)注以下幾點(diǎn):
- 它必須返回一個(gè)布爾值美旧。除了這個(gè)約束之外渤滞,還允許任何有效的BeanShell代碼(例如,您可能想在工作日期間返回true榴嗅,在周末返回false妄呕,這將允許您根據(jù)日期以不同的方式運(yùn)行測(cè)試)。
- 為了方便起見嗽测,TestNG定義了以下變量:
java.lang.reflect.Method method: 當(dāng)前的測(cè)試方法
org.testng.ITestNGMethod testngMethod: 當(dāng)前測(cè)試方法的描述
java.util.Map<String, String> groups: 當(dāng)前測(cè)試方法所屬組的映射
上述testng.xml的groups.containsKey返回的正是布爾值绪励。
擴(kuò)展學(xué)習(xí)資料
關(guān)于beanshell的學(xué)習(xí),可參考beanshell手冊(cè)唠粥。