Java TestNg 在基类上的 @BeforeTest 每个夹具只发生一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4310964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:44:29  来源:igfitidea点击:

TestNg's @BeforeTest on base class only happening once per fixture

javaunit-testingtestng

提问by ripper234

I'm trying to use @BeforeTest to get code to ... run once before every test.

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

This is my code:

这是我的代码:

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

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

    @Test
    public void test2(){}
}

"BeforeTest" is only printed once, not twice. What am I doing wrong?

“BeforeTest”只打印一次,而不是两次。我究竟做错了什么?

采纳答案by Cedric Beust

Use @BeforeMethod, not @BeforeTest.

使用@BeforeMethod,而不是@BeforeTest。

The meaning of @BeforeTest is explained in the documentation.

@BeforeTest 的含义在文档中有解释。

回答by Raman

"BeforeTest" is only printed once, not twice. What am I doing wrong?

“BeforeTest”只打印一次,而不是两次。我究竟做错了什么?

***Sorry. I haven't noticed that you is written @BeforeTest , but in your example @BeforeTest almost equals @BeforeClass , and better to use @BeforeClass , when you haven't anymore test classes.

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

@BeforeClass" should be declared in same class that your tests methods, not differently!

@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 will executed once, before your all tests methods in this class. @BeforeMethod will executed before test method, before which it is written.

@BeforeClass 将在该类中的所有测试方法之前执行一次。@BeforeMethod 将在测试方法之前执行,在测试方法之前执行。

@BeforeClass may be only one in test class, in difference @BeforeMethod!(If it is some @BeforeClass, they are carried out by turns, but it not a correct composition of the test)

@BeforeClass在测试类中可能只有一个,不同的是@BeforeMethod!(如果是一些@BeforeClass,它们是轮流进行的,但它不是测试的正确组合)

P.S. Sorry for my English :)

PS对不起我的英语:)

回答by CuongHuyTo

According to documentation, a method annotated with @BeforeTest is run before any @Test method belonging to the classes inside the tag is run.

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

From my experience:

根据我的经验:

  • Each @BeforeTest method is run only once
  • If you have several @BeforeTest methods, the order of their execution depends on the order of the class containing those @BeforeTest method.
  • 每个@BeforeTest 方法只运行一次
  • 如果您有多个 @BeforeTest 方法,则它们的执行顺序取决于包含这些 @BeforeTest 方法的类的顺序。

You could test this by setting up a simple example.

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

回答by automaticSoldier

If you use @beforeTest, that method will be run once in the beginning of every <test>(we specify in the test suit xml file) if that test contains that class

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

All the @befortests within all the classeswithin a <test>will be executed at the beggining of that test

a中所有类中的所有@befortests<test>将在该测试开始时执行