TestNG中BeforeTest和BeforeMethod之间有什么区别

Sam*_*Dev 12 testng

两个注释都在testNG中的@test之前运行,然后两个注释之间有什么区别.

irf*_*fan 11

检查以下代码并输出

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_BeforeTestAndBeforeMethod {

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

    @BeforeMethod
    public void beforeMethod()
    {
        System.out.println("\nbeforeMethod");
    }


    @Test
    public void firstTest()
    {
        System.out.println("firstTest");
    }

    @Test
    public void secondTest()
    {
        System.out.println("secondTest");
    }

    @Test
    public void thirdTest()
    {
        System.out.println("thirdTest");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

beforeTest

beforeMethod
firstTest

beforeMethod
secondTest

beforeMethod
thirdTest
Run Code Online (Sandbox Code Playgroud)


Ish*_*hah 8

@BeforeTest:它将在Test方法之前调用一次.

@BeforeMethod它将在测试方法之前每次调用.

参考O/P示例


小智 6

我知道这个问题已经有几个很好的答案,我只是想在它们的基础上以可视化方式将注释与 testng.xml 中的 xml 元素联系起来,并包括之前/之后的套件。
我尽力让它对新手尽可能友好,我希望它能有所帮助。

我的 java 示例基本上只是 Ishita Shah 代码的重新格式化版本。


BeforeAfterAnnotations.java(假设此文件位于名为“test”的包中)

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;
import org.testng.annotations.Test;



public class BeforeAfterAnnotations
{
    @BeforeSuite
    public void beforeSuiteDemo()
    {
        System.out.println("\nThis is before a <suite> start tag.");
    }


    @BeforeTest
    public void beforeTestDemo()
    {
        System.out.println("\tThis is before a <test> start tag.");
    }


    @BeforeClass
    public void beforeClassDemo()
    {
        System.out.println("\t\tThis is before a <class> start tag.\n");
    }


    @BeforeMethod
    public void beforeMethodDemo()
    {
        System.out.println("\t\t\tThis is before a method that is annotated by @Test.");
    }


    @Test
    public void testADemo()
    {
        System.out.println("\t\t\t\tThis is the testADemo() method.");
    }


    @Test
    public void testBDemo()
    {
        System.out.println("\t\t\t\tThis is the testBDemo() method.");
    }


    @Test
    public void testCDemo()
    {
        System.out.println("\t\t\t\tThis is the testCDemo() method.");
    }


    @AfterMethod
    public void afterMethodDemo()
    {
        System.out.println("\t\t\tThis is after a method that is annotated by @Test.\n");
    }


    @AfterClass
    public void afterClassDemo()
    {
        System.out.println("\t\tThis is after a </class> end tag.");
    }


    @AfterTest
    public void afterTestDemo()
    {
        System.out.println("\tThis is after a </test> end tag.");
    }


    @AfterSuite
    public void afterSuiteDemo()
    {
        System.out.println("This is after a </suite> end tag.");
    }
}
Run Code Online (Sandbox Code Playgroud)



testng.xml(testng.xml --> 运行方式 --> TestNG Suite)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Before/After Annotations Suite">
    <test name="Before/After Annotations Test">
        <classes>
            <class name="test.BeforeAfterAnnotations" />
        </classes>
    </test>
</suite> 
Run Code Online (Sandbox Code Playgroud)



输出到控制台

[RemoteTestNG] detected TestNG version 7.0.0

This is before a <suite> start tag.
    This is before a <test> start tag.
        This is before a <class> start tag.

            This is before a method that is annotated by @Test.
                This is the testADemo() method.
            This is after a method that is annotated by @Test.

            This is before a method that is annotated by @Test.
                This is the testBDemo() method.
            This is after a method that is annotated by @Test.

            This is before a method that is annotated by @Test.
                This is the testCDemo() method.
            This is after a method that is annotated by @Test.

        This is after a </class> end tag.
    This is after a </test> end tag.
This is after a </suite> end tag.

===============================================
Before/After Annotations Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
Run Code Online (Sandbox Code Playgroud)


Mel*_*ius 5

@BeforeTest:对于任何测试方法,只会调用一次,无论注释了多少种方法@Test,都只会调用一次

@BeforeMethod在每个带有注解的方法之前,它将被称为@Test,如果您有10个@Test方法,它将被调用10次。

要了解BeforeClass和之间的区别BeforeTest,请参阅答案/sf/answers/3993659071/


小智 2

在测试NG中

@BeforeMethod - BeforeMethod 在每个测试方法之前执行。所有使用@Test注解的方法。@BeforeMethod 适用于 Java 类中定义的测试。

@测试前 - BeforeTest 仅在 testng.xml 文件中给定的标记之前执行。@BeforeTest 适用于 testng.xml 中定义的测试

参考:- https://examples.javacodegeeks.com/enterprise-java/testng/testng-beforetest-example/http://howtesting.blogspot.com/2012/12/difference- Between-beforetest-and.html