1. What is testng?
is a test framework that makes it easier to execute test cases.
2, how to install?
Eclipse--help--install New Software
Work with input: Http://beust.com/eclipse
Wait a few seconds, check testng, all the way next
Window--preferences can see testng, is the installation success.
3, the Project right button--build Path--add libraries--testng--finish
4, on the package com.lx, right---new--other--testng class all the way next, created by default Newtest
5, in the F method, add content, run (right-click--run as--testng Test)
Package COM.LX; Import org.testng.annotations.Test; Public class newtest { @Test publicvoid f () { System.out.println ("Test1") ); }}
A use-case test named F is passed
6. Increase the test case, assertion (judging if the use case is passed)
PackageCOM.LX;Import Staticorg.testng.Assert.assertTrue;Importorg.testng.annotations.Test; Public classnewtest {@Test Public voidf () {System.out.println ("Test1"); Asserttrue (true); } @Test Public voidg () {System.out.println ("Test2"); Asserttrue (false); } @Test Public voidh () {System.out.println ("Test3"); Asserttrue (true); } @Test Public voidJ () {System.out.println ("Test4"); Asserttrue (false); }}
Operation Result: Tests Run:4, Failures:2, skips:0
Run 4 test cases, 2 failed
Asserttrue () is used to assert whether the current test case passed
7, take Demo1 as an example
If the interface is normal, then the interface returns the data, containing the text "and";
S.indexof ("and") indicates the location of the search and the word in the returned data
S.indexof ("and")!=-1 is True
If you can't find it, it's false.
@Test Public void f () { System.out.println ("test1"); = Httprequest.sendget ("Https://translate.google.cn/translate_a/single", "CLIENT=T&SL=EN&TL=ZH-CN &hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss& Dt=t&ie=utf-8&oe=utf-8&otf=1&ssel=0&tsel=0&kc=1&tk=640089.1025185&q=and ") ; System.out.println (s); Asserttrue (S.indexof ("and")!=-1); }
Replace this code with Newtest's F method, remove other methods, run
If the language is garbled, you need to set the encoding format and try again. Window--preferences--general--workspace--other:utf8
8. Use Testng.xml to run the use case
Project Right-click--new--file--file name:testng.xml--finish
Testng.xml content:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd"><Suitename= "Suite1"> <Testname= "Test12"> <Classes> <classname= "Com.lx.NewTest" /> </Classes> </Test></Suite>
Right-click Run as, with the same effect as running Newtest
Interface Test (java+testng+ant+jenkins) second article testng