TestNG中的BeforeTest和BeforeMethod有什么区别?

41

两个注解都会在testNG中的@Test之前运行,那么它们之间有什么区别。


可能是在TestNG中BeforeClass和BeforeTest之间的区别的重复问题。 - user7294900
9个回答

39

请检查以下代码和输出

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");
    }
}

输出:

beforeTest

beforeMethod
firstTest

beforeMethod
secondTest

beforeMethod
thirdTest

30

@BeforeTest : 在测试方法之前仅调用一次

@BeforeMethod: 在每个测试方法之前每次都会调用

示例:

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_BeforeTestAndBeforeMethod {

    @BeforeTest
    public void beforeTestDemo()
    {       
        System.out.println("This is before test calling.");   
    }
    
    @BeforeClass
    public void beforeClassDemo()
    {
        System.out.println("This is before class calling.");
    }

    @BeforeMethod
    public void beforeMethodDemo()
    {
        System.out.println("This is before method calling.");
    }
    
    @Test
    public void testADemo()
    {
        System.out.println("This is Test1 calling.");
    }

    @Test
    public void testBDemo()
    {
        System.out.println("This is Test2 calling.");
    }

    @Test
    public void testCDemo()
    {
        System.out.println("This is Test3 calling.");
    }
    
    @AfterMethod
    public void afterMethodDemo()
    {
        System.out.println("This is after method calling.");
    }
    
    @AfterClass
    public void afterClassDemo()
    {
        System.out.println("This is after class calling.");
    }
    
    @AfterTest
    public void afterTestDemo()
    {
        System.out.println("This is after test calling.");
    }
}

参考输出示例


1
testCDemo,“This is Test2 calling” -> “这是Test3的调用”。 - Vitalii Diravka
@IshitaShah - 我所有的测试方法都会实例化一个变量并使用它。我应该将这个变量转换为类成员,并在"@BeforeMethod"中实例化它吗?还有其他的方法吗? - MasterJoe

12

@BeforeTest :在任何测试方法之前只会被调用一次,无论有多少个方法使用了@Test注解,它只会被调用一次。

@BeforeMethod:在每个使用@Test注解的方法之前都会被调用,如果你有10个@Test方法,它将被调用10次。

要了解BeforeClassBeforeTest之间的区别,请参考答案https://dev59.com/UF0a5IYBdhLWcg3wNWXA#57052272


1
那么 @BeforeClass 即使只被调用一次,它也会被调用对吗? - khan

5

我知道已经有几个很好的答案回答了这个问题,我只是想在此基础上将注释视觉化地与testng.xml中的xml元素联系起来,并包括before/after suite。
我尽量让它对新手友好,希望这可以帮助到你。

我的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.");
    }
}



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> 



Output to console

[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
===============================================


2

@BeforeTest - 在testng.xml中声明的每个测试之前运行

@BeforeMethod - 在类内部声明的每个带有@Test注释的测试方法之前运行


2

1
在运行集成测试时,@BeforeTest会在注入任何bean之前执行。与之相反的是,@BeforeMethod在bean注入之后执行。不确定为什么会设计成这样。

0

@BeforeTest 只会在所有测试方法之前执行一次。这些方法将在执行任何标有 @Test 注解的测试方法之前运行,这些测试方法是 testNG.xml 文件中 <test> 标签的一部分。 @BeforeMethod 将在每个标有 @Test 注解的方法之前执行。


0

@BeforeTest 在执行 testng.xml 文件中 < test > 标签中包含的任何测试方法之前执行设置方法。 @BeforeMethod 在执行标记为 @Test 的任何测试方法之前执行设置方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接