美文网首页
TestNG的使用

TestNG的使用

作者: 御都 | 来源:发表于2019-04-09 20:40 被阅读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

相关文章

  • 测试框架TestNG使用介绍

    今天分享TestNG测试框架的基础知识,使用TestNG的优点,TestNG的基本注解如何使用,套件、忽略、异常、...

  • TestNG注解使用与测试技巧

    TestNG注解的使用 TestNG执行结果顺序 其中的BeforeMethod/AfterMethod�会在每个...

  • TestNG单元测试框架|java方向第一个测试用例脚本

    介绍 TestNG 官方网址:http://testng.org/doc/ 我们先感官上对TestNG使用有一个初...

  • 批量执行测试用例

    想要批量执行测试用例,可以使用TestNG这个插件,配合maven使用。 1. 创建testng.xml文件 1)...

  • TestNG官方文档记录+例子运行

    testng.xml 使用maven工程时,我们可以在工程中添加testng.xml来运行。运行xml,可以使用I...

  • TestNG的使用

    【背景】 1 TestNG框架用于用例的管理,分模块分测试集。 【如何使用】 在Maven的pom.xml中设置依...

  • TestNG使用

    TestNG介绍 TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit, 功能都差不多, 只...

  • testNg使用

    testNg使用 测试是检查应用程序的功能的过程是否按要求工作,在开发人员层面进行单元测试,在采取适当措施来测试每...

  • 2022-01-04

    今天主要是学习为主: 了解了testng的基础使用,常见的注解@test、@Factory等 了解了testng的...

  • 【Java进阶营】Java技术专题「TestNG专题」单元测试框

    承接上文 [☕【Java技术指南】「TestNG专题」单元测试框架之TestNG使用教程指南(上)],继续开展我们...

网友评论

      本文标题:TestNG的使用

      本文链接:https://www.haomeiwen.com/subject/xhtwiqtx.html