TestNG的使用

2019-04-09  本文已影响0人  御都

【背景】

1 TestNG框架用于用例的管理,分模块分测试集。

【如何使用】

在Maven的pom.xml中设置依赖列表,如果使用6.10版本下载jar包失败,很奇怪。难道是6.10不支撑32位的


<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8.8</version>
  <scope>test</scope>
</dependency>

【使用】
1 @Test标签,不用main方法也能执行测试用例
【例子get接口访问百度网页】
代码

package com.cn;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;


public class Demo2 {
    
    @Test
    public void test() throws ClientProtocolException, IOException{
        String url = "https://www.baidu.com";
        HttpGet get = new HttpGet(url);
//      HttpClient client = new DefaultHttpClient();
        CloseableHttpClient client = HttpClients.createDefault();
        HttpResponse response = client.execute(get);
        int code = response.getStatusLine().getStatusCode();
        String result = EntityUtils.toString(response.getEntity(),"UTF-8");
        System.out.println(result);
        System.out.println("状态码为:"+code);
    }
}

【结果】
由于没有mian方法,无法执行该用例。感觉没有使用到testng的test标签。
【原因分析】
1、在eclipse中使用时,是不是没有testng.xml的设置?
是的,没有配置testng.xml文件,就没有框架对应的映射,在代码中写了@Test也不能执行。
2、testng.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!--定义测试套-->
<suite name="suitename">
    <!--定义一个测试集,相当于一个独立的模块-->
    <test name="2222">
        <!--定义一个测试类,模块下包含的测试类-->
        <classes>
            <!--类的全路径信息,供框架反射调用,如果没有在代码里写了@Test也不会有什么结果-->
            <classe name="com.cn.Demo2">
            </classe>
        </classes>
    </test>
</suite>

3 新的问题
选中<classe name="com.cn.Demo2">中的com.cn.Demo,右键没有按测试套执行的选项。
【原因分析】通过搜索TestNG按测试套执行,发现没有安装testNG插件
4 安装testNG插件
4.1 安装方式
4.1.1 通过Eclipse Marketplace安装-----不行
打开Eclipse,点击help->Eclipse Marketplace,搜索testng,进行安装
现象:打开Eclipse Marketplace报错。放弃

Cannot open Eclipse Marketplace
Cannot install remote marketplace locations: Cannot complete request to http://marketplace.eclipse.org/catalogs/api/p: Pipe closed
Cannot complete request to http://marketplace.eclipse.org/catalogs/api/p: Pipe closed
Pipe closed
Cannot complete request to http://marketplace.eclipse.org/catalogs/api/p: Pipe closed
Pipe closed

4.1.2 在线安装---一出现错误,安装失败
Eclipse安装testNG插件:打开Eclipse—help—install new software—add,location输入(http://beust.com/eclipse),--》选中testNG-->Next..
也不行。
4.1.3 怀疑eclipse版本过低
TestNG安装的前提是JDK1.7以上版本、Eclipse4.2以上版本。
查询本地eclipse版本为4.4

<title>Eclipse Project Release Notes 4.4</title>

查询JDK版本为1.6不行


image.png
上一篇 下一篇

猜你喜欢

热点阅读