Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TestNg';在基类上的s@BeforeTest每个fixture只发生一次_Java_Unit Testing_Testng - Fatal编程技术网

Java TestNg';在基类上的s@BeforeTest每个fixture只发生一次

Java TestNg';在基类上的s@BeforeTest每个fixture只发生一次,java,unit-testing,testng,Java,Unit Testing,Testng,我正在尝试使用@BeforeTest获取代码以。。。每次测试前运行一次 这是我的代码: public class TestBase { @BeforeTest public void before() { System.out.println("BeforeTest"); } } public class TestClass extends TestBase{ @Test public void test1(){} @Test

我正在尝试使用@BeforeTest获取代码以。。。每次测试前运行一次

这是我的代码:

public class TestBase {
    @BeforeTest
    public void before() {
        System.out.println("BeforeTest");
    }
}

public class TestClass extends TestBase{
    @Test
    public void test1(){}

    @Test
    public void test2(){}
}

“测试前”只打印一次,而不是两次。我做错了什么?

使用@beforethod,而不是@BeforeTest

中解释了@BeforeTest的含义

“测试前”只打印一次,而不是两次。我做错了什么

***对不起。我没有注意到您编写的是@BeforeTest,但在您的示例中@BeforeTest几乎等于@BeforeClass,当您不再使用测试类时,最好使用@BeforeClass

@BeforeClass“应该在测试方法的同一个类中声明,而不是不同

//Example

package test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Tests {
private String bClass;
private String bMethod1;
private String bMethod2;

@BeforeClass
public void beforeClass() {
    bClass = "BeforeClass was executed once for this class";
}

@BeforeMethod
public void beforeMetodTest1() {
    bMethod1 = "It's before method for test1";
}

@Test
public void test1() {
    System.out.println(bClass);
    System.out.println(bMethod1);
}

@BeforeMethod
public void beforeMethodTest2() {
    bMethod2 = "It's before method for test2";
}

@Test
public void test2() {
    System.out.println(bClass);
    System.out.println(bMethod2);
}
}
@BeforeClass将在此类中的所有测试方法之前执行一次。@BeforeMethod将在编写它之前的测试方法之前执行

@BeforeClass可能是测试类中唯一的一个,不同的是@BeforeMethod!(如果是某个@BeforeClass,则它们是轮流执行的,但这不是测试的正确组合)

另外,我的英语很抱歉:)

根据,在运行属于标记内类的任何@Test方法之前,将运行带有@BeforeTest注释的方法

根据我的经验:

  • 每个@BeforeTest方法只运行一次
  • 如果有几个@BeforeTest方法,它们的执行顺序取决于包含这些@BeforeTest方法的类的顺序

您可以通过设置一个简单的示例来测试这一点。

如果您使用@beforeTest,则该方法将在每个
的开头运行一次(我们在测试套件xml文件中指定)如果该测试包含该类

一个
中所有类中的所有@befortests都将在该测试开始时执行

文档中说“@BeforeTest:在运行属于标记中类的任何测试方法之前,将运行带注释的方法”。但是他有两个测试。所以它应该运行两次,对吗?