java testing framework

Unit testing framework used in the project development process has Junit, TestNG and Mockito, Junit and TestNG use more, Mockito only recently started.

TestNG JUnit the same point
1. annotation, and most of the same annotation.
2. The unit can be tested (Unit test).
3. Java is a tool for testing.

Different points of TestNG and JUnit:
1. Only JUnit unit test can be TestNG unit testing, functional testing, end to end testing, integration testing.
2. TestNG require an additional xml configuration file, test configuration class, method or even package.
3. TestNG operation more flexible: the command line, ant and IDE, JUnit can only use the IDE.
4. TestNG annotation of more abundant, such as @ ExpectedExceptions, @ DataProvider and so on.
The test suite fails, JUnit 4 will re-run the entire test suite. When TestNG fails, it will create an XML file description failed tests, use this file to perform the program will not run repeated tests have been successful.
Https://blog.csdn.net/SystemZYF/article/details/77746632 ---------------------
TestNG, i.e. Testing, Next Generation, next generation testing technique , it is set based on JUnit and NUnit thinking and build using annotations to strengthen a test framework test function. TestNG designed to encompass all types of tests: unit function, end to end, integration. Before learning TestNG need to learn the programming language Java, JDK configuration of the local environment (JDK1.5 or later versions) and install java development tools eclipse.
1) mounted in eclipse testNG
open Eclipse Help -> MarketPlace, TestNG input in the search box inside, and then install TestNG plug.

2) After successful installation, right on the package of the project can be seen TestNG -> Create TestNG class.

Here TestNG annotation method can be checked.
@BeforeSuite:  is this method annotated method will run before all the test runs.
@AfterSuite:  is this annotated method will run the process after all the test runs.
@BeforeTest:  is this annotated method will be run before the test run.
@AfterTest:  is this annotated method will be run after the test run.
@BeforeClass:  is this annotated method will be run before the first test method of the current class to call
@AfterClass:  is this annotated method will be run after all the test methods of the current class to call
@BeforeMethod:  is this method annotated It will run before each test method call
@AfterMethod:  is this annotated method will be run after each test method call.
@DataProvider:  marks a method provides a test method data. The method must return an annotated Object [] [], the parameter list for each object where [] of the test method can be allocated. The @Test, and I hope to receive data from a DataProvider this, you need to use a dataProvider name equal to the name of the annotation.
Let's check what @ BeforeTest, @ AfterTest, @ DataProvider , auto-generated class file as follows:


 

Here incorrect report, because we have not TestNG will add to our library in the past, click on the error, there will be import Library option and click there will be some, as


 

So far environment to build complete, you can start writing a test case.

3) Starting from the start a simple test case, the code is as follows:

package com.pingan.ff.zijin;

import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest; public class NewTest { @Test(dataProvider = "dp") public void f(Integer n, String s) { System.out.println("第一个参数是"+n+",第二个参数是"+s); } @DataProvider public Object[][] dp() { return new Object[][] { new Object[] { 1, "a" }, new Object[] { 2, "b" }, }; } @BeforeTest public void beforeTest() { System.out.println("------------开始测试------------"); } @AfterTest public void afterTest() { System.out.println("------------结束测试------------"); } }

右键Run As --> TestNG Test,运行后结果如下:

------------开始测试------------
第一个参数是1,第二个参数是a
第一个参数是2,第二个参数是b
------------结束测试------------

从测试的结果可以看到执行的顺序是beforeTest()-->Test()-->afterTest(),同时Test()方法从dataProvider dp里面接收参数。
4)TestNG默认情况下,会生成两种类型的测试报告HTML和XML,测试报告位于 "test-output" 目录下。右键项目刷新一下项目就可以看到:

Guess you like

Origin www.cnblogs.com/klb561/p/10963564.html