TestNG用例執行順序

大家好,我是剛哥。

TestNG用例的執行順序有兩種方式來指定:註解和XML。

註解

使用@Test的dependsOnMethods屬性:

@Test
public void serverStartedOk() {}
 
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}

或者dependsOnGroups屬性:

@Test(groups = { "init" })
public void serverStartedOk() {}
 
@Test(groups = { "init" })
public void initEnvironment() {}
 
@Test(dependsOnGroups = { "init.*" })
public void method1() {}

@Before/After也能實現初始化,但是它們的結果不會出現在測試報告中。

默認TestNG會強制校驗,依賴的用例必須成功纔會執行當前用例,否則當前用例會被標記爲SKIP,這叫做強依賴。通過設置alwaysRun=true可以變成弱依賴,無論依賴用例執行成功與否,都會執行當前用例。

需要特別注意的是,依賴測試方法是按照測試類來進行執行的(group by class),比如b()方法依賴的a()方法有多個實例,那麼會按照以下順序執行:

a(1)
a(2)
b(2)
b(2)

舉個實際的例子,登入和登出,如果想達到以下效果:

signIn("us")
signOut("us")
signIn("uk")
signOut("uk")

那麼需要在XML中進行配置:

  <suite name="Factory" group-by-instances="true">
or
  <test name="Factory" group-by-instances="true">

XML

在testng.xml中使用<dependencies>depends-on來指定用例順序:

<test name="My suite">
  <groups>
    <dependencies>
      <group name="c" depends-on="a  b" />
      <group name="z" depends-on="c" />
    </dependencies>
  </groups>
</test>

參考資料:

https://testng.org/doc/documentation-main.html 5.7 - Dependencies

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章