Java two test methods comparison of JUnit and testng

Source: Internet
Author: User
Tags testng

Development process, often use Java testing, front-end Javas cript debugging relatively easy, firebug,console.log (), but Java is more tangled point, each change to compile and run, the process is relatively slow, plus if the project is relatively large, Very DT, have to listen to friends say their company's project is EJB, development time every time to make a small change will take a few minutes to redeploy on the server. In order to reduce the debugging process, improve the development efficiency, some specifically for the testing framework has emerged, typical of JUnit and testng, the following Changsha Obertek Software College with you to share a simple how to use the two tools and their differences, is purely introductory level introduction.

Junit

This estimate is familiar to everyone, the current development to JUNIT4 version, through simple annotations can be code testing, commonly used @Test, @Before, @After; @Test annotation is the method we want to test, @ Before is the implementation of the @test method before the operation, generally used to do the initialization work, @After is @test method run after the operation, looks a bit like an AOP annotation, hehe.

Ready to work: Import the JUnit 4 jar package, when running, right-click, run as, select "JUnit Test".

Limitations: No action is provided with parameter execution (the new version has wood), of course, you can use @before to do some initialization work, where we can initialize the data we want to test.

Patch Test Demo Code:

Testng

The test framework is similar to JUnit, but one thing that is different from JUnit is that the built-in support for data-driven testing is that the @test method can be a parameter, which is also the most shiny feature. It also has a similar note: @Test, @BeforeMethod, and a @dataprovider, is the data provider, do not understand to see the code is clear.

Preparation work: TestNG's preparation work in fact, a lot of talk on the internet, tossing a period of time, and finally succeeded. Specific process: MyEclipse is click Help->eclipse MarketPlace ...-->search: Enter TestNG, find TestNG, click Install, the process of installation gray often tangled, Slow speed plus seemingly download plug-in site is wall (you know, FQ will not say). After installation, restart, import testng jar package: Testng-jdk15.jar, do not have to download, their own directly in the MyEclipse import can, find a @test class, right-click Run as, you can see the "testng Test" option, Congratulations, you've succeeded.

The difference from JUnit is primarily the built-in support for test data, which allows us to unit test the method with parameters. In addition, the construction of the test process to configure the relevant XML file, the declaration is for that class which method to test, see the code.

To test the demo code:

Configuration file:

Temp-testng-customsuite.xml

Java two test methods comparison of JUnit and testng

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.