TestNG Getting Started Tutorial <java Getting Started >

Source: Internet
Author: User
Tags testng

TestNG Getting Started Tutorial summary: http://www.cnblogs.com/TankXiao/p/3888070.html

National Day 7 days holiday, most of my friends went out to travel, the circle is full of photos of the Sun travel, Southeast Asia tour, European Tour yes, really envy ah. Miserable I only went to the Shanghai Safari Park, rest at home, take advantage of this holiday, to summarize the things I have learned before.

I worked overtime a long time ago, every day busy work, do not have the energy to control their own new skills to learn the plan, blog did not write several, many want to do because the work is too busy and delayed. So the work is too busy and not good, no energy to focus on things other than work.

Recently write automated tests used every day to testng, the use of common testng to summarize.

Read Catalogue

    1. testng Introduction
    2. Installing testng online in eclipse
    3. Installing testng offline in eclipse
    4. TestNG the simplest test
    5. Basic annotations of TestNG
    6. How to perform tests in testng
    7. Use the Testtng.xml file to execute a case
    8. testng executing case sequentially
    9. TestNG Anomaly Testing
    10. TESTNG Group Testing
    11. TestNG Parametric testing
    12. testng Ignore Test
    13. TestNG Dependency Testing
    14. TestNG Test Results Report

testng Introduction

TESTNG is a test framework in Java, similar to JUnit and NUnit, with the same functionality, just more powerful and easier to use

There is already a JUnit test framework in Java.  TestNG is much more powerful than JUnit.  Testers typically use testng to write automated tests. Developers typically write unit tests with JUnit.

Official website: http://testng.org/doc/index.html

Installing testng online in eclipse

Open Eclipse help->install New software, then add "Http://beust.com/eclipse"

Installing testng offline in eclipse

Download testng offline package First: http://pan.baidu.com/s/1hrEaX6k

1. Unzip the file. \eclipse-testng Offline Package \features\org.testng.eclipse_6.9.9.201510270734 folder in eclipse--"features directory

2. Unzip the file. \eclipse-testng Offline Package \plugins\org.testng.eclipse_6.9.8.201510130443 folder in eclipse--"Plugins directory

3. Restart Eclipse

4. Verify that the installation is successful, file-->new-->other-->testng

TestNG the simplest test

Here is one of the simplest examples of testng

Package Tanklearn2.learn;import Org.junit.afterclass;import Org.junit.beforeclass;import Org.testng.annotations.test;public class TestNGLearn1 {    @BeforeClass public    void Beforeclass () {        System.out.println ("This Is before class");    }    @Test public    void Testnglearn () {        System.out.println ("This is TestNG Test case");    @AfterClass public    void Afterclass () {        System.out.println ("This was after Class");}    }

Basic annotations of TestNG

Annotations Description
@BeforeSuite The annotated method will run only once, running all tests in this suite.
@AfterSuite The annotated method will run only once after all the tests in this suite have been run.
@BeforeClass The annotated method will run only one time prior to the method call in the current class.
@AfterClass The annotated method will run only once for all test methods that have been run in the current class.
@BeforeTest The annotated method will be run by any of the internal class's <test> tags before any test methods are run.
@AfterTest The annotated method will run after all the test methods that belong to the inner class of the <test> tag.
@BeforeGroups A list of groups that this configuration method will run before. This method is guaranteed to run the first test method that belongs to any of these groups, and the method is called.
@AfterGroups The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that the method belongs to any of these groups being called.
@BeforeMethod The annotated method runs before each test method.
@AfterMethod The annotated method will be run after each test method.
@DataProvider Marks a method that provides a test method of data. The annotated method must return a object[] [], where each object [] can be assigned in the parameter list of the test method. The @test method, hoping to receive data from this dataprovider, needs to use a Dataprovider name equal to the name of this annotation.
@Factory As a factory, the object of the test class that returns TESTNG will be used to mark the method. The method must return object[].
@Listeners Defines a listener for a test class.
@Parameters Describes how to pass parameters to the @test method.
@Test Mark a class or method as part of the test.

How to perform tests in testng

First Direct execution: Right-click the method to execute, point to run as->testng Test

Use the Testtng.xml file to execute a case

The second type is executed by the Testng.xml file. Put the case that you want to execute into the Testng.xml file. Right click on Testng.xml, point to run as

Testng.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" ><suite name= "Suite1" >    <test name= "test12" >        <classes>            <class name= "TankLearn2.Learn.TestNGLearn1"/>        </classes>    </ Test></suite>

testng executing case sequentially

In Testng.xml, you can control the execution of test cases sequentially. When preserve-order= "true" is, you can guarantee that the following methods of the node are executed sequentially

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" ><suite name= "Suite1" >    <test name= "test12" Preserve-order= "true" >        <classes>            <class name= "TankLearn2.Learn.TestNGLearn1" >                < methods>                    <include name= "TestNgLearn3"/>                    <include name= "TestNgLearn1"/>                    <include Name= "TestNgLearn2"/>                </methods>            </class>        </classes>    </test></ Suite>

TestNG Anomaly Testing

In testing, we sometimes expect some code to throw exceptions.

TestNG through @test (expectedexceptions) to determine the expected exception, and to determine the error Message

Package Tanklearn2.learn;import Org.testng.annotations.test;public class Exceptiontest {        @Test ( Expectedexceptions = Illegalargumentexception.class, expectedexceptionsmessageregexp= "NullPoint") public    Void TestException () {        throw new IllegalArgumentException ("Nullpoint");}    }

TESTNG Group Testing

TestNG can be used to group test cases so that test cases can be executed in groups such as:

Package Tanklearn2.learn;import Org.testng.annotations.test;public class Grouptest {        @Test (groups = {"Systemtest" }) public    void Testlogin () {        System.out.println ("This is Test login");        @Test (groups = {"FunctionTest"}) public    void Testopenpage () {        System.out.println ("The Is Test Open page"); c13/>}}

Then execute the test case by group in Testng.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" ><suite name= "Suite1" >    <test name= "Test1" >        <groups>        <run>        <include name= "FunctionTest"/>        </run>    </ Groups>    </test></suite>

TestNG Parametric testing

In software testing, it is often necessary to test a large number of data sets.  The logic of the test code is exactly the same, except that the parameters of the test are different. So we need a "mechanism for passing test parameters". Avoid writing repetitive test code

TESTNG provides 2 ways to pass parameters.

The first: Testng.xml way to separate code and test data for easy maintenance

The second type: @DataProvider can provide more complex parameters. (also called Data-driven testing)

Method One: Pass parameters to the test code by Testng.xml

Package Tanklearn2.learn;import Org.testng.annotations.parameters;import Org.testng.annotations.test;public class ParameterizedTest1 {        @Test    @Parameters ("test1") public    void Paratest (String test1) {        System.out.println ("This is" + test1);}    }

Testng.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" ><suite name= "Suite1" >        <parameter name= " Test1 "value=" Tank "/>        <parameter name=" test1 "value=" Xiao "/>    <test name=" test12 ">        < classes>            <class name= "TankLearn2.Learn.ParameterizedTest1"/>        </classes>    </test ></suite>

Mode two: passing parameters via Dataprovider

Package Tanklearn2.learn;import Org.testng.annotations.dataprovider;import Org.testng.annotations.test;public Class Dataproviderlearn {        @DataProvider (name= "user") public    object[][] Users () {        return new object[][]{                {"Root", "PASSOWRD"},                {"Cnblogs.com", "Tankxiao"},                {"Tank", "Xiao"}        };    }        @Test (dataprovider= "user") public    void Verifyuser (string userName, string password) {        System.out.println (" Username: "+ Username +" Password: "+ Password);}    }

testng Ignore Test

Sometimes the test case is not ready, and you can disable the test case by adding @test (Enable = false) to the test case

Package Tanklearn2.learn;import Org.testng.annotations.test;public class Tesgngignore {        @Test (enabled = False) Public    void Testignore () {        System.out.println ("This test case would ignore");}    }

TestNG Dependency Testing

Sometimes we need to invoke test cases in order, so there is a dependency between the test cases. TESTNG support for dependencies between test cases

Package Tanklearn2.learn;import Org.testng.annotations.test;public class Dependstest {        @Test public    Void Setupenv () {        System.out.println ("This is setup Env");        @Test (Dependsonmethods = {"Setupenv"}) public    void TestMessage () {        System.out.println ("This is Test message") );    }}

TestNG Test Results Report

Test reports are a very important part of testing.

TestNG By default, two types of test reports are produced for both HTML and XML. The test report is located in the "Test-output" directory.

Of course we can also set the content level of the test report.

Verbose= "2" identifies the log level of the record, with a total of 0-10 levels, where 0 means none, and 10 indicates the most detailed

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" ><suite name= "Suite1" >    <test name= "test12" Verbose= "2" >        <classes>            <class name= "TankLearn2.Learn.TestNGLearn1"/>        </classes >    </test></suite>

TestNG Getting Started Tutorial <java Getting Started >

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.