TestNG的測(cè)試方法、測(cè)試組,異常

測(cè)試方法

測(cè)試方法被注解為@Test。注釋的方法與@Test碰巧返回值將被忽略,可以設(shè)置允許回值設(shè)置為true在你的testng.xml:

<suite allow-return-values="true">
or
<test allow-return-values="true">

測(cè)試組

TestNG 允許將測(cè)試方法歸類(lèi)為不同的組。
不僅僅是可以聲明某個(gè)方法屬于某個(gè)組,而且還可以讓組包含其他的組。
這樣TestNG可以調(diào)用或者請(qǐng)求包含一組特定的組(或者正則表達(dá)式)而排除其他不需要組的集合。
這樣,如果打算將測(cè)試分成兩份的時(shí)候,就無(wú)需重新編譯。
這個(gè)特點(diǎn),會(huì)給你在劃分組的時(shí)候帶來(lái)很大的靈活性。

組在的testng.xml文件中指定,可以發(fā)現(xiàn)無(wú)論是<test><suite>標(biāo)簽。指定組<suite>標(biāo)簽適用于所有的<test>下方標(biāo)簽。需要注意的是群體標(biāo)簽:如果指定組的“a”在<suite>標(biāo)簽中和b在<test>標(biāo)簽中,那么這兩個(gè)“a”和“b”將被包括在內(nèi)

將測(cè)試劃分為兩種類(lèi)別:

  • 檢查性測(cè)試(Check-in test):這些測(cè)試在你提交新代碼之前就會(huì)運(yùn)行。它們一般都是很快進(jìn)行的,并且保證沒(méi)有哪個(gè)基本的功能是不好使的

  • 功能性測(cè)試(Functional test):這些測(cè)試涵蓋你的軟件中所有的功能,并且至少每天運(yùn)行一次,不過(guò)也可能希望他們持續(xù)的運(yùn)行

典型的來(lái)說(shuō),檢測(cè)性測(cè)試通常是功能性測(cè)試的一個(gè)子集。TestNG允許你根據(jù)個(gè)人感覺(jué)來(lái)進(jìn)行組劃分。例如,你可能希望把你所有的測(cè)試類(lèi)都劃歸為"functest"組,并且額外的有幾個(gè)方法輸入"checkintest"組。

public class Test1 {
  @Test(groups = { "functest", "checkintest" })
  public void testMethod1() {
  }

  @Test(groups = {"functest", "checkintest"} )
  public void testMethod2() {
  }

  @Test(groups = { "functest" })
  public void testMethod3() {
  }
}

通過(guò)下面的內(nèi)容調(diào)用TestNG

<test name="Test1">
  <groups>
    <run>
      <include name="functest"/>
    </run>
  </groups>
  <classes>
    <class name="example1.Test1"/>
  </classes>
</test>

以上會(huì)運(yùn)行上面那個(gè)類(lèi)中所有的測(cè)試,當(dāng)y要使用checkintest進(jìn)行調(diào)用的時(shí)候,就僅僅運(yùn)行testMethod1()和testMethod2()

使用正則表達(dá)式

@Test
public class Test1 {
  @Test(groups = { "windows.checkintest" })
  public void testWindowsOnly() {
  }

  @Test(groups = {"linux.checkintest"} )
  public void testLinuxOnly() {
  }

  @Test(groups = { "windows.functest" )
  public void testWindowsToo() {
  }
}

可以使用下面這個(gè) testng.xml 來(lái)只運(yùn)行在Windows下的方法:

<test name="Test1">
  <groups>
    <run>
      <include name="windows.*"/>
    </run>
  </groups>

  <classes>
    <class name="example1.Test1"/>
  </classes>
</test>

注意:TestNG使用的是正則表達(dá)式,而不是通配符。注意這二者的區(qū)別(例如,"anything" 是匹配于 "."代表點(diǎn)和星號(hào),而不是星號(hào) "")

組中組

測(cè)試組也可以包含其他組。這樣的組叫做“元組"(MetaGroups)"

例如,可能要定義一個(gè)組"all"來(lái)包含其他的組,"chekcintest""functest"。"functest"本身只包含了組windowslinux,而"checkintest"僅僅包含windows。你就可以在屬性文件中這樣定義:

<test name="Regression1">
  <groups>
    <define name="functest">
      <include name="windows"/>
      <include name="linux"/>
    </define>

    <define name="all">
      <include name="functest"/>
      <include name="checkintest"/>
    </define>

    <run>
      <include name="all"/>
    </run>
  </groups>

  <classes>
    <class name="test.sample.Test1"/>
  </classes>
</test>

排除組

stNG 允許你包含組,當(dāng)然也可以排除

創(chuàng)建一個(gè)叫做"broken"組, 然后使得這些測(cè)試方法從屬于那個(gè)組。
例如上面的例子,假設(shè)我知道testMethod2()會(huì)中斷,所以我希望使其失效:

@Test(groups = {"checkintest", "broken"} )
public void testMethod2() {
}

從運(yùn)行中排除這個(gè)組:

<test name="Simple example">
  <groups>
    <run>
      <include name="checkintest"/>
      <exclude name="broken"/>
    </run>
  </groups>

  <classes>
    <class name="example1.Test1"/>
  </classes>
</test>

通過(guò)這種辦法,可以得到整潔的測(cè)試運(yùn)行,同時(shí)也能夠跟蹤那些需要稍后修正的中斷的測(cè)試。

注意:你可以可以通過(guò)使用"enabled"屬性來(lái)完成,這個(gè)屬性適用于@Test@Before/After annotation

局部組

可以在類(lèi)級(jí)別定義組,之后還可以在方法級(jí)定義組:

@Test(groups = { "checkin-test" })
public class All {

  @Test(groups = { "func-test" )
  public void method1() { ... }

  public void method2() { ... }
}

在這個(gè)類(lèi)中,method2()類(lèi)級(jí)組"checkin-test"的一部分,而method1()即屬于"checkin-test"也屬于"func-test"

異常

測(cè)試中,有時(shí)候我們期望某些代碼拋出異常

TestNG通過(guò)@Test(expectedExceptions) 來(lái)判斷期待的異常, 并且判斷Error Message

import org.testng.Assert;
import org.testng.annotations.Test;

public class ExpectedExceptionTest {
    String message = "Manisha"; 
    MessageUtil messageUtil = new MessageUtil(message);
       
    @Test(expectedExceptions = ArithmeticException.class)
    public void testPrintMessage() {    
        System.out.println("Inside testPrintMessage()");     
        messageUtil.printMessage();     
   }
   @Test
   public void testSalutationMessage() {
      System.out.println("Inside testSalutationMessage()");
      message = "Hi!" + "Manisha";
      Assert.assertEquals(message,messageUtil.salutationMessage());
   }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容