簡體   English   中英

運行兩次Testng的beforeClass測試方法

[英]beforeClass test method running twice Testng

在下面的示例中,class方法運行兩次。 這是預期的行為嗎? 因為beforeClass應該只在類中的第一個方法運行之前運行一次。 但是在我的課上它運行兩次。

package test;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

public class Sample1
{ 

 @BeforeSuite
 public void setUp()
 {
     System.out.println("This is before suite");
 }

 @BeforeTest
 public void init()
 {
     System.out.println("This is before Test");
 }

 @BeforeClass
 public void t1()
 {
     System.out.println("This is before class");
 }

 @BeforeMethod
 public void t3()
 {
     System.out.println("This is before Method");
 }


  @AfterMethod
  public void t4()
  {
     System.out.println("This is After Method");
  }

  @AfterClass
  public void t2()
  {
     System.out.println("This is After class");
  }

  @AfterTest
  public void cleanUp()
  {
     System.out.println("This is After Test");
  }

  @AfterSuite
  public void tearDown()
  {
     System.out.println("This is after suite");
  }

}

package test;

import org.testng.annotations.Test;

public class Sample2 extends Sample1 {

  @Test
  public void f() 
  {
      System.out.println("This is the testmethod1 in Sample2");
  }

  @Test
  public void tets1 () 
  {
      System.out.println("This is the testmethod2 in Sample2");
  }

}

<suite name="Suite1" verbose="1" >

  <test name="Regression1"   >
  <classes>
  <class name="test.Sample2">
  <methods>
  <include name = "f" />
  </methods>
  </class>
  </classes>
  </test>

  <test name="Regression2"   >
  <classes>
  <class name="test.Sample2">
  <methods>
  <include name = "tets1" />
  </methods>
  </class>
  </classes>
  </test>

</suite>

/* Output is below -- after and before class running twice

This is before suite
This is before Test
This is before class
This is before Method
This is the testmethod1 in Sample2
This is After Method
This is After class
This is After Test
This is before Test
This is before class
This is before Method
This is the testmethod2 in Sample2
This is After Method
This is After class
This is After Test
This is after suite

*/

之所以會出現這種情況,是因為您的<suite> XML將這兩個測試方法作為獨立測試運行。 如果允許一個測試運行兩個方法,則它們將在同一類實例上運行,而@BeforeClass將僅運行一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM