2013-06-25 214 views
2

我在使用TestNG和maven時遇到了一些奇怪的問題。我有太多的代碼發佈在這裏,但我會發佈一個相關的例子。TestNG測試不會運行

我有這個在我的pom.xml TestNG進行:

....other pom stuff.... 

<dependency> 
    <groupId>org.testng</groupId> 
    <artifactId>testng</artifactId> 
    <version>6.2</version> 
    <type>jar</type> 
</dependency> 

....other pom stuff.... 

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.14.1</version> 
    <configuration> 
     <suiteXmlFiles> 
      <suiteXmlFile>testng.xml</suiteXmlFile> 
     </suiteXmlFiles> 
    </configuration> 
</plugin> 

....other pom stuff.... 

我的testng.xml文件看起來像這樣:

​​3210

而且SOTest.java看起來是這樣的:

import org.testng.annotations.BeforeSuite; 
import org.testng.annotations.Test; 
import org.testng.annotations.BeforeTest; 

public class SOTest { 
    @BeforeSuite 
    public void setup() { 
     ...DO SOME STUFF... 
     System.out.println("foo"); 
    } 

    @BeforeTest 
    public void setupTest() { 
     ...DO SOME STUFF... 
     System.out.println("bar"); 
    } 


    @Test 
    public void test_good_subkey_pass() { 
     System.out.println("baz"); 
     ...DO SOME STUFF... 
    } 
} 

當運行mvn test時,會打印「foo」和「bar」,但是它會掛起而「baz」從不打印?有誰知道什麼會阻止@Test註釋的方法運行?

UPDATE

test_good_subkey_pass()之後另一個測試,裏面有一個無限循環。爲什麼會阻止第一次測試運行?請注意,preserve-order屬性未設置爲false

+0

做u有什麼版本的TestNG的在烏爾依賴呢? –

+0

您的進口是否有junit'@ test'導入而不是testng'@ test'導入,這也可能導致此問題 –

+0

@niharika_neo我更新了問題 – fvrghl

回答

2

您知道XML文件定義了要運行的測試,但TestNG不會按照它們在類代碼中存在的順序運行測試。看起來您的XML文件指定了運行的順序,但不是執行方法的順序。 (另外,我知道你可以指定包含/排除的方法,但我不確定那甚至定義了它們將運行的順序。根據我的經驗,測試總是按字母順序運行。)

如果另一個測試一個無限循環,這可以解釋爲什麼test_good_subkey_pass()沒有運行。嘗試移除其他測試用例以查看是否可以解決問題(或使用@AfterSuite或類似的註釋通知您所有測試完成)。

你也可能只是想指定的testng.xml

方法名這有可能是你最好的資源:http://testng.org/doc/documentation-main.html#testng-xml