TestNG unit testing framework - introduction to common annotations and the difference between testng and Junit [Hangzhou multi-tester_Wang sir] [Hangzhou multi-tester]...

1. TestNG unit testing framework - introduction to common annotations

Testng learning URL: https://www.jc2182.com/testng/testng-environment.html

1. The Before category and After category annotations are executed in the following order @BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite
@BeforeSuite : For suite tests, run just once before all tests in this suite execute
@AfterSuite: For suite tests, run after all tests in this suite execute, only once
@BeforeTest: For suite tests, run only once Run @AfterTest before the test methods of all classes in the test> tag are executed
: For suite tests, run after the test methods of all classes belonging to the <test> tag have been executed @BeforeClass: Run @AfterClass
before calling the current class
: run after calling the current class
@BeforeGroups: run before calling the first test method belonging to the group
@AfterGroups: run after calling the last test method belonging to the group executes    
@BeforeMethod: runs before each test method executes Run
@AfterMethod: @Test is run after each test method is executed
: the most basic annotation used to mark the method as part of the test

2. @Test annotation
@Test annotation is the core annotation of TestNG. The method marked with this annotation is represented as a test method. It is a reason to compare JUnit (JUnit also uses this annotation. When using TestNG, be careful not to mislead the package. ).
This annotation has multiple configuration attributes. The usage is: @Test(param1 = ..., param2 = ...)
Common values ​​are explained as follows:
alwaysRun : If =true, it means that even if the pre-test that the test method depends on has In the case of failure, also execute
dataProvider: the constructor of the selected incoming parameters. (The @DataProvider annotation will be introduced in later chapters)
dataProviderClass: Determine the Class class of the parameter constructor. (The parameter constructor will first be searched in the current test class. If the parameter constructor is not defined in the current test class, then this attribute must be used to execute the Class class it is in) dependsOnGroups : Determine the dependent pre-test
group.
dependsOnMethods : Determine the pre-test methods that depend on.
description : Test method description information. (It is recommended to add meaningful description information for each test method, which will be displayed in the final report)
enabled: The default is true, if specified as false, it means that the test method will not be executed.
expectedExceptions : Specifies the exceptions that are expected to be thrown by the test method, and multiple exceptions are separated by commas (,).
groups : Specifies the group to which the test method belongs, multiple groups can be specified, separated by commas. The usage of group tests will be introduced separately in later articles.
invocationCount: Specify the number of times the test method needs to be called.
invocationTimeOut: The timeout of each invocation, if invocationCount is not specified, this parameter will be ignored. The application scenario can obtain a database connection for the test, and it will be considered a failure if it times out. The unit is milliseconds.
priority : Specifies the priority of the test method. The lower the value, the higher the priority, and it will be called with other test methods with higher values. (Note that it is for the priority of a test class)
timeout: Specify the timeout period of the entire test method. The unit is milliseconds.

3. @Parameters annotation
The @Parameters annotation is used to pass parameters for the test method, and the usage is as follows:

@Parameters({ "userName", "password" })
@BeforeMethod
public void beforeTest(String userName, String pwd) {
    System.out.println("参数userName值为:"+userName);
    System.out.println("参数password值为:"+pwd);
}

4. @DataProvider annotation
The above summary mentioned that the @Parameters annotation can pass parameters for the test method, but the parameter value in this way needs to be configured in testng.xml, which is not very flexible. The @DataProvider annotation can also pass parameter values ​​for the test method, and it is a real parameter constructor that can pass in multiple sets of test data to test the test method. For methods annotated by @DataProvider, the method return value must be Object[][] or Iterator<Object[]>. Examples are as follows:

public class Demo {
    @DateProvider(name = "xxx")
    public Object[][] fun() {
        return new Object[][] {
            {
     
     "参数列表1", 0, 1},
            {
     
     "参数列表2", 2, 3}
        };
    }
    
    @Test(dataProvider = "xxx")
    public void funTest(String param1, int param2, int param3) {
        
    }
}

5. @Factory Annotation
Adding @Factory annotation on a method indicates that the method will return a test class that can be tested by TestNG. The factory pattern in the design pattern is used. Examples are as follows:

import org.testng.annotations.Test;
public class SimpleTestFactory {
    @Test
    public void testMethod(){
      System.out.println("Simple Test Method.");
      }
  }


import org.testng.annotations.Test;
import org.testng.annotations.Factory;
public class TestAnnotationFactory {
    @Factory
    public Object[] factoryMethod() {
      return new Object[]{
        new SimpleTestFactory(),
        new SimpleTestFactory()
        };
    }
  }

6. @Listeners annotation
Generally, when we write test classes, this type of annotation will not be involved. This annotation must be defined at the class, interface or enumeration class level. Practical Listeners include ISuiteListener, ITestListener, and IInvokedMethodListener. They can perform some custom operations at the suite level, test level, and test method execution points, such as printing logs.

Two, the difference between testng and Junit

TestNG
TestNG也是一个Java框架,便于在Java中进行软件测试。它是一个在类中运行测试的框架。它为相应的测试制作类,然后处理它们。TestNG是一个先进的框架,克服了JUnit中的限制。它也被认为是一个灵活的工具来执行测试,因为它使用相同的类来运行其所有的测试,并管理线程来运行程序,这使得检查测试的整体功能快速。TestNG测试框架的特点:
TestNG可以把测试方法放在Java组中。
TestNG处理单元测试,将参数传入测试方法。
TestNG利用线程可以更好地提高测试性能,因为执行时间减少。
JUnit的一些限制被克服了,因为TestNG支持并行测试的执行。

JUnit
JUnit框架是一个广泛用于测试的Java框架。它支持通过编写和测试来运行测试。JUnit框架最初是基于用于单元测试的SUnit框架,但后来使用Selenium WebDriver对Java进行了更新。现在,当我们需要在Java中进行测试时,JUnit被作为一个标准。
JUnit测试框架的特点:
JUnit不支持同时运行并行测试。
JUnit框架在Java 8中被更新。
JUnit有助于在编写代码时实现测试驱动的编程。
JUnit现在被许多语言所支持。JUnit和TestNG之间的区别对比表如下:

Guess you like

Origin blog.csdn.net/weixin_39362573/article/details/129096260