TestNG Hello World

Source: Internet
Author: User
Tags assert xmlns testng

"Other tutorials in this series are being translated, click on Category: TestNG for viewing. 】

"Translation by clearly like the month QQ 605283073"

Original address: http://websystique.com/java/testing/testng-hello-world-example/

Next post: TestNG Annotations Example

In this article we will learn TestNG's Hello World example.

We will learn how to set up an environment for using testng, how to write and execute unit tests and validate results.

The project is based on Maven.

-------------------------------------------

The environment used

TestNG 6.9.4 Maven 3 JDK 1.7 Eclipse JUNO Service Release 2 project directory structure:



The details of the above structure are described in detail below.

1th Step: Add dependencies in the Pom.xml file

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance
    " xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
    < modelversion>4.0.0</modelversion>
 
    <groupId>com.websystique.testng</groupId>
    < artifactid>testnghelloworldexample</artifactid>
    <version>1.0.0</version>
    < packaging>jar</packaging>
 
    <name>TestNGHelloWorldExample</name>
 
    <dependencies >
        <dependency>
            <groupId>org.testng</groupId>
            <artifactid>testng</ artifactid>
            <version>6.9.4</version>
            <scope>test</scope>
        </ dependency>
    </dependencies>
</project>

Note: Because we only use testng to test, we set the scope of the element to test. 2nd Step: Create a simple Java classHere is a simple Java class with only one method in it. This class of methods will be used for unit testing.
Package com.websystique.testng;
 
public class Vatcalculator {
     /* * Returns 21% VAT on given amount
     */public
    double Getvatonamount (Double A Mount) {
        return amount * 0.21;
    }
 

3rd Step: Create a Test class
The following test class is used to test the Getvatonamount method in the above class
Package com.websystique.testng;
 
Import Org.testng.annotations.Test;
Import Org.testng.Assert;
 
public class Testvatcalculator {
 
    @Test public
    void Testgetvatonamount () {
        Vatcalculator calc = new Vatcalculator ();
        Double expected = +;
        Assert.assertequals (Calc.getvatonamount (+), expected);
        Assert.assertnotequals (Calc.getvatonamount (), expected);
    }

@Test annotations are added to the method so that the method is called a test method.

We invoke the Getvatonamount method of the Vatcalculator class instance, and then use the Estng assert API to assert

The return value. If any one of the assertions fails, the entire test will fail. The Assert API provides a number of more desirable and true-to-results methods.


Key points:

The source files are created in the Src/main/java folder, however the test class is created in the Src/test/java folder.

The test class and the original class class name should be the same just before (or later) added test.


Name of the test class: If you want to perform this test through MAVEN, you must have test as either a prefix or a suffix.

The name of the test method: This is arbitrary, not necessarily with test, but preferably similar to the method name in the original class.

4th step: There are many ways to perform the test 1 TestNG Eclipse plugin 2 using maven
Use the TestNG Eclipse plugin-----------------------------to search for "TestNG" from the Eclipse marketplace for installation. After successful installation, right-click the test class, run as "TestNG test" will produce the following output:

Another folder called "Test-output" that contains test results-related files (especially testng-results.xml) is added to your project folder.

Using Maven

--------------------------

Enter the project directory via cmd

Executes the MVN clean Test command line.

MAVEN will perform the test through Maven-surefire-plugin.

E:\WORKSPACE7\TESTNGHELLOWORLDEXAMPLE&GT;MVN clean test [INFO] scanning for projects ...
[INFO] [INFO]------------------------------------------------------------------------[INFO] Building Testnghelloworldexample 1.0.0 [INFO]------------------------------------------------------------------------[ INFO] [INFO]---maven-clean-plugin:2.5:clean (default-clean) @ testnghelloworldexample---[info] Deleting e:\ Workspace7\testnghelloworldexample\target [INFO] [INFO]---maven-resources-plugin:2.6:resources (default-resources  ) @ testnghelloworldexample---[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build
is platform D ependent! [INFO] Skip non existing resourcedirectory E:\workspace7\TestNGHelloWorldExample\src\main\resources [INFO] [INFO]--- Maven-compiler-plugin:2.5.1:compile (default-compile) @ testnghelloworldexample---[WARNING] File encoding have not been
Set, using platform encoding Cp1252, i.e. build is platform Depende nt! [INFO] CompiliNg 1 source file to E:\workspace7\TestNGHelloWorldExample\target\classes [info] [INFO]---maven-resources-plugin:2.6: Testresources (default-testresources) @ testnghelloworldexample---[WARNING] Using platform encoding (Cp1252 actually) t
o Copy filtered resources, i.e. build is platform D ependent! [INFO] Skip non existing resourcedirectory E:\workspace7\TestNGHelloWorldExample\src\test\resources [INFO] [INFO]--- Maven-compiler-plugin:2.5.1:testcompile (default-testcompile) @ testnghelloworldexample---[WARNING] File encoding
Have not been set, using platform encoding Cp1252, i.e. build is platform Depende nt! [INFO] Compiling 1 source file to E:\workspace7\TestNGHelloWorldExample\target\test-classes [info] [INFO]--- Maven-surefire-plugin:2.12.4:test (default-test) @ testnghelloworldexample---[INFO] Surefire report Directory:e:\ Workspace7\testnghelloworldexample\target\surefire-reports---------------------------------------------------- ---t E s t s-------------------------------------------------------Running com.websystique.testng.TestVatCalculator Configuring testng with: ORG.APACHE.MAVEN.SUREFIRE.TESTNG.CONF.TESTNG652CONFIGURATOR@3A4346CD Tests run:1, failures:0, errors:0, skipped:0, Time elapsed:0.976 sec results:tests run:1, failures:0, errors:0, skipped:0 [INFO]-------------------------- ----------------------------------------------[INFO] BUILD SUCCESS [INFO]---------------------------------------- --------------------------------[INFO] Total time:5.150s [info] finished At:fri Jul 20:26:37 CEST [INFO] Final memory:12m/226m [INFO]------------------------------------------------------------------------E:\workspace7\ Testnghelloworldexample>

We can see from the above test results. A total of one test method was run. No failures, no errors, no ignoring.


At the end of this article, the next article will explain how to use testng annotations and how you can set them up.



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.