gpt4 book ai didi

java - beforeClass测试方法运行两次Testng

转载 作者:行者123 更新时间:2023-12-01 11:12:49 25 4
gpt4 key购买 nike

在下面的示例中,类方法运行两次之前。这是预期的行为吗?因为 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只会运行一次。

关于java - beforeClass测试方法运行两次Testng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com