?TestNG :JAVA一个主流的测试框架,引入了注解、依赖、分组等便于使用的新功能。
Rest Assured :基于REST服务的测试过程的Java DSL,支持GET、POST等请求,且可以通过断言验证响应的信息。
测试类demo
创建一个测试类:TestDemo()
可以通过下面的demo,体会到注解的执行顺序:
package com.course.demo; ? import org.testng.annotations.*; ? public class TestDemo { ? @Test public void testcase1(){ System.out.println("执行了 >>> testcase1"); } ? @Test public void testcase2(){ System.out.println("执行了 >>> testcase2"); } ? @BeforeTest public void beforeTest(){ System.out.println("执行了 >>> beforeTest"); } ? @AfterTest public void afterTest(){ System.out.println("执行了 >>> afterTest"); } ? @BeforeMethod public void beforeMethod(){ System.out.println("执行了 >>> beforeMethod"); } ? @AfterMethod public void afterMethod(){ System.out.println("执行了 >>> afterMethod"); } ? @BeforeClass public void beforeClass(){ System.out.println("执行了 >>> beforeClass"); } ? @AfterClass public void afterClass(){ System.out.println("执行了 >>> afterClass"); } ? @BeforeSuite public void beforeSuite(){ System.out.println("执行了 >>> beforeSuite"); } ? @AfterSuite public void AfterSuite(){ System.out.println("执行了 >>> AfterSuite"); } }
run之后结果如下:
[TestNG] Running: 执行了 >>> beforeSuite 执行了 >>> beforeTest 执行了 >>> beforeClass 执行了 >>> beforeMethod 执行了 >>> testcase1 执行了 >>> afterMethod 执行了 >>> beforeMethod 执行了 >>> testcase2 执行了 >>> afterMethod 执行了 >>> afterClass 执行了 >>> afterTest 执行了 >>> AfterSuite ? ? =============================================== Default Suite Total tests run: 2, Failures: 0, Skips: 0 ===============================================
生成report
当case量累积到成百上千时,测试报告的作用就凸现出来了。
Python的unittest测试框架,或者UI的Katalon,都可以生成对应的report,TestNG也可以通过配置生成一份美观的测试报告。
pom.xml文件里配置生成测试报告所需的依赖:
<!--report--> <dependency> <groupId>com.relevantcodes</groupId> <artifactId>extentreports</artifactId> <version>2.41.1</version> </dependency> <dependency> <groupId>com.vimalselvam</groupId> <artifactId>testng-extentsreport</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>3.0.6</version> </dependency>
引入一个生成测试报告report的文件:ExtentTestNGIReporterListener.java
通过配置文件resources下的testng.xml配置计划执行哪些case,以及使用的监听:
<?xml version="1.0" encoding="UTF-8" ?> <suite name="test"> <test name="接口测试套件demo"> <classes> <class name="com.course.demo.TestDemo"> <methods> <include name="testcase1" /> <include name="testcase2" /> </methods> </class> </classes> </test> <listeners> <listener class-name="com.course.testng.reporter.ExtentTestNGIReporterListener" /> </listeners> </suite>
为了看到测试报告的效果,修改TestDemo.java里的两个测试case,分别加上断言:
@Test public void testcase1(){ System.out.println("执行了 >>> testcase1"); Assert.assertEquals(1,1); } ? @Test public void testcase2(){ System.out.println("执行了 >>> testcase2"); Assert.assertEquals(1,2); } ?
run一下testng.xml,测试报告生成路径,以及文件名称均可以通过修改ExtentTestNGIReporterListener.java文件自定义
用浏览器打开测试报告,展示效果如下:
引入Rest Assured
引入Rest Assured测试框架
pom.xml引入依赖:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>3.0.3</version> </dependency> ? <dependency> <groupId>io.rest-assured</groupId> <artifactId>json-path</artifactId> <version>3.0.3</version> </dependency> ? <dependency> <groupId>io.rest-assured</groupId> <artifactId>xml-path</artifactId> <version>3.0.3</version> </dependency> ?
修改测试类,用Rest Assured写一个测试case,并忽略之前的两个case:
@Test(enabled = false) public void testcase1(){ System.out.println("执行了 >>> testcase1"); } ? @Test(enabled = false) public void testcase2(){ System.out.println("执行了 >>> testcase2"); } ? @Test(priority = 1) public void testBaidu(){Response response = given(). when(). get("https://www.baidu.com"). then().log().ifError(). statusCode(200). extract().response(); System.out.println("百度首页接口返回信息 >>> "+response.asString()); } ?
run之后结果如下:
[TestNG] Running: /Users/apple/Library/Caches/IdeaIC2019.1/temp-testng-customsuite.xml 执行了 >>> beforeSuite 执行了 >>> beforeTest 执行了 >>> beforeClass 执行了 >>> beforeMethod 百度首页接口返回信息 >>> <!DOCTYPE html> <!--STATUS OK--><html> <head><title>百度一下,你就知道</title></head>...<body></body> </html> ? ? 执行了 >>> afterMethod 执行了 >>> afterClass 执行了 >>> afterTest 执行了 >>> AfterSuite ? ? =============================================== Default Suite Total tests run: 1, Failures: 0, Skips: 0 =============================================== ?
maven编译
testng.xml 配置新增的测试case:testBaidu()
通过配置pom.xml实现maven编译:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.1</version> <configuration> <testFailureIgnore>true</testFailureIgnore> <suiteXmlFiles> <suiteXmlFile> ./src/main/resources/testng.xml </suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build>
命令行执行mvn clean package:
------------------------------------------------------- T E S T S ------------------------------------------------------- Running TestSuite [TestNGContentHandler] 执行了 >>> beforeSuite 执行了 >>> beforeTest 执行了 >>> beforeClass 执行了 >>> beforeMethod 百度首页接口返回信息 >>> <!DOCTYPE html> <!--STATUS OK--><html> <head><title>百度一下,你就知道</title></head>...<body></body> </html> 执行了 >>> afterMethod 执行了 >>> afterClass 执行了 >>> afterTest 执行了 >>> AfterSuite Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.889 sec ? ? Results : ? ? Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ? ? [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Chapter1 --- [INFO] Building jar: /Users/apple/Downloads/TestNG/Chapter1/target/Chapter1-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.015 s [INFO] Finished at: 2019-10-09T11:55:34+08:00 [INFO] ------------------------------------------------------------------------ ?
这样可以集成到jenkins里,通过设置定时任务执行接口测试脚本。
本文暂时没有评论,来添加一个吧(●'◡'●)