TestNg 的 @BeforeTest 在基类上每个固定装置仅发生一次

发布于 2024-10-05 06:42:03 字数 363 浏览 0 评论 0原文

我正在尝试使用 @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(){}
}

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对不⑦ 2024-10-12 06:42:03

使用@BeforeMethod,而不是@BeforeTest。

文档中解释了 @BeforeTest 的含义。

Use @BeforeMethod, not @BeforeTest.

The meaning of @BeforeTest is explained in the documentation.

ゝ杯具 2024-10-12 06:42:03

“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,它们是轮流进行的,但这不是测试的正确组成)

PS对不起我的英语:)

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

***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.

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

//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 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)

P.S. Sorry for my English :)

跨年 2024-10-12 06:42:03

根据文档,用@BeforeTest注释的方法在属于任何@Test方法之前运行标签内的类被运行。

根据我的经验:

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

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

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

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.

You could test this by setting up a simple example.

深爱不及久伴 2024-10-12 06:42:03

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

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

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

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

被翻牌 2024-10-12 06:42:03

官方文件非常不清楚,而且大多含糊不清。这就是为什么很多人不喜欢读它的原因。更不用说没有显示现实生活中的例子。

如果您试图让代码在每次测试之前运行一次,那么您正在寻找@BeforeMethod,而不是@BeforeTest。因为您的每个测试都独立考虑了方法。

@BeforeMethod 在每个方法之前运行。我看到您有两个名为 test1() 和 test2() 的方法。期望 BeforeMethod 在每个方法之前运行。

@BeforeTest 在所有测试(测试方法)之前仅运行一次。在 Selenium 中,WebDriver 被调用一次,这是更好的测试实践。
@BeforeMethod 将在每次测试之前调用 WebDriver,这不是一个好的做法,特别是当您要运行数百个回归测试时。

例如,如果您运行此代码(这些是单独的类,不是彼此内部的,仅出于演示目的而在此处显示):

public class TestClass extends Base {

    @Test
    public void test1() {
        System.out.println("Running Test 1");
    }

    @Test
    public void test2() {
        System.out.println("Running Test 2");
    }

}

public class Base {

    @BeforeTest
    public void beforeTest() {
        System.out.println("Before Test");
    }

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

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("Before Method");
    }

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

}

您将得到以下输出:

Before Test
Before Method
Running Test 1
After Method
Before Method
Running Test 2
After Method
After Test

如您所见,BeforeMethods 在每次测试之前运行,而 BeforeTest 和 AfterTest在完成所有测试之前和之后仅运行一次。

希望这能为您澄清其中的差异。

Official Documentations are so unclear and mostly ambiguous. This is why many don't like to read it. Not to mention no real-life examples shown.

If you are trying to get code to run once before every test, then you are looking for @BeforeMethod, not @BeforeTest. Because each of your tests independently considered methods.

@BeforeMethod runs before each method. I see you have two methods with the names test1() and test2(). Expect BeforeMethod to run before each of them.

@BeforeTest runs only once before ALL of your tests (test-methods). In Selenium, WebDriver is called once, this is better practice for testing.
@BeforeMethod will invoke WebDriver before each test, this is not good practice, especially when you have regression tests with hundreds of them to run.

For example, if you run this code (these are separate classes, not inner to each other, only showing for demonstration purposes here):

public class TestClass extends Base {

    @Test
    public void test1() {
        System.out.println("Running Test 1");
    }

    @Test
    public void test2() {
        System.out.println("Running Test 2");
    }

}

public class Base {

    @BeforeTest
    public void beforeTest() {
        System.out.println("Before Test");
    }

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

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("Before Method");
    }

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

}

And you will get this as an output:

Before Test
Before Method
Running Test 1
After Method
Before Method
Running Test 2
After Method
After Test

As you can see BeforeMethods run before each test while BeforeTest and AfterTest run only once before and after completing ALL tests.

Hope this clarified the difference for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文