Testng basic annotation (annotation) []

Source: Internet
Author: User
Tags testng

The test method in JUnit 3 is to test its own name prefix. Marking some methods in a class has special significance. This is a very effective method, but the name is not well extended (if we want to add more labels for different frameworks ?), Rather than lack of flexibility (if we want to pass the additional parameter testing framework ).

The annotation is officially added to the Java language and testng in JDK 5 to make a choice using the annotation test class.

Here is the annotation in the Support List of testng:

Annotation Description
@ Beforesuite Annotation method will only run once, before running all tests in this suite.
@ Aftersuite The annotation method will only run once after all tests in this suite are run.
@ Beforeclass The annotation method will only run once first try to call the method in the current class.
@ Afterclass The annotation method will only run once and then all the testing methods that have been run in the current class.
@ Beforetest The annotation method is run by the <Test> label of the internal class of any test method before running.
@ Aftertest After the annotation method is run, all the test methods belong to the running of the <Test> label of the internal class.
@ Beforegroups List of groups. This configuration method runs before. This method is used to ensure that the first test method in any of these groups is run and the method is called.
@ Aftergroups Group list. This configuration method will run. This method is the final test method shortly after running, which is called by any of these groups.
@ Beforemethod The annotation method runs before each test method.
@ Aftermethod After the annotated method is run, each test method.
@ Dataprovider It indicates a method that provides a test method for data. The annotation method must return an object [] [], which can be allocated to the parameter list of the test method for each object. In this @ test method, if you want to receive data from this dataprovider, you need to use a dataprovider name that is equal to the name of this annotation.
@ Factory As a factory, the objects of the testng test class returned will be used for marking methods. This method must return object [].
@ Listeners Define a listener for a test class.
@ Parameters This section describes how to pass parameters to the @ test method.
@ Test Mark a class or method as part of the test.
Benefits of using annotations

The following are some of the benefits of using Annotations:

  • The testng identification method is concerned with searching for annotations. Therefore, the method name is not limited to any mode or format.
  • We can use additional parameter annotations.
  • Annotations are strongly typed, so the compiler will mark any errors.
  • The test class no longer needs anything (such as test cases, in junit3) Extension

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------

This tutorial describes how to execute a program in testng, which means that this method is called first and next. The following is an example of the testng test API method for executing the program.

Create a Java class file named testngannotation. Java to test the annotation in c: \> testng_workspace.

import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeClass;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeSuite;import org.testng.annotations.AfterSuite;public class TestngAnnotation {// test case 1@Testpublic void testCase1() {System.out.println("in test case 1");}// test case 2@Testpublic void testCase2() {System.out.println("in test case 2");}@BeforeMethodpublic void beforeMethod() {System.out.println("in beforeMethod");}@AfterMethodpublic void afterMethod() {System.out.println("in afterMethod");}@BeforeClasspublic void beforeClass() {System.out.println("in beforeClass");}@AfterClasspublic void afterClass() {System.out.println("in afterClass");}@BeforeTestpublic void beforeTest() {System.out.println("in beforeTest");}@AfterTestpublic void afterTest() {System.out.println("in afterTest");}@BeforeSuitepublic void beforeSuite() {System.out.println("in beforeSuite");}@AfterSuitepublic void afterSuite() {System.out.println("in afterSuite");}}

Next, let's create the fileTestng. xmlInC: \> testng_workspaceExecute annotation.

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Suite1">  <test name="test1">    <classes>       <class name="TestngAnnotation"/>    </classes>  </test></suite>

Compile and use the javac test case class.

C:\TestNG_WORKSPACE>javac TestngAnnotation.java

Run testng. XML to run the test cases defined in the provided test case class.

C:\TestNG_WORKSPACE>java org.testng.TestNG testng.xml

Verify the output.

in beforeSuitein beforeTestin beforeClassin beforeMethodin test case 1in afterMethodin beforeMethodin test case 2in afterMethodin afterClassin afterTestin afterSuite===============================================SuiteTotal tests run: 2, Failures: 0, Skips: 0===============================================

As shown in the preceding output, testng is the execution process as follows:

  • First, all beforesuite () methods are executed only once.

  • Finally, the () method of aftersuite is executed only once.

  • Even if the beforetest (), beforeclass (), afterclass (), and aftertest () methods are executed only once.

  • The beforemethod () method executes each test case, but before this test case.

  • Aftermethod () method executes each test case, but after the test case is executed.

  • In between beforemethod () and aftermethod () each test case executes.

Testng basic annotation (annotation) []

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.