【TestNG测试】TestNG、Maven、testng.xml构建测试工程
创建一个maven工程
使用Idea创建maven工程

建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是用来进行自动化测试的,所以实际这里src/main/java是用不到的,只是IDEA统一规则建立了而已。这里个人还建立了一个bin目录,用来存放chromedriver等浏览器执行文件,用来匹配不同的操作系统及不同的浏览器版本。
pom.xml中的内容
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.wilmar.test</groupId>
8 <artifactId>woodpecker</artifactId>
9 <version>1.0-SNAPSHOT</version>
10 <!--<packaging>jar</packaging>-->
11
12 <dependencies>
13 <dependency>
14 <groupId>org.testng</groupId>
15 <artifactId>testng</artifactId>
16 <version>6.14.3</version>
17 <scope>test</scope>
18 </dependency>
19 <dependency>
20 <groupId>org.seleniumhq.selenium</groupId>
21 <artifactId>selenium-java</artifactId>
22 <version>3.141.59</version>
23 </dependency>
24 <dependency>
25 <groupId>com.alibaba</groupId>
26 <artifactId>dubbo</artifactId>
27 <version>2.5.3</version>
28 <exclusions>
29 <exclusion>
30 <groupId>org.springframework</groupId>
31 <artifactId>spring</artifactId>
32 </exclusion>
33 </exclusions>
34 </dependency>
35
36 </dependencies>
37 <build>
38 <plugins>
39 <plugin>
40 <groupId>org.apache.maven.plugins</groupId>
41 <artifactId>maven-dependency-plugin</artifactId>
42 <executions>
43 <execution>
44 <id>copy-dependencies</id>
45 <phase>prepare-package</phase>
46 <goals>
47 <goal>copy-dependencies</goal>
48 </goals>
49 <configuration>
50 <outputDirectory>${project.build.directory}/lib</outputDirectory>
51 <overWriteReleases>false</overWriteReleases>
52 <overWriteSnapshots>false</overWriteSnapshots>
53 <overWriteIfNewer>true</overWriteIfNewer>
54 <excludeScope>provided</excludeScope>
55 </configuration>
56 </execution>
57 </executions>
58 </plugin>
59 <plugin>
60 <groupId>org.apache.maven.plugins</groupId>
61 <artifactId>maven-compiler-plugin</artifactId>
62 <version>3.0</version>
63 <configuration>
64 <source>1.7</source>
65 <target>1.7</target>
66 <encoding>UTF-8</encoding>
67 </configuration>
68 </plugin>
69 </plugins>
70 </build>
71 <distributionManagement>
72 <repository>
73 <id>releases</id>
74 <name>Team nexus Release Repository</name>
75 <url>http://10.118.888.10:8888/nexus/content/repositories/releases</url>
76 </repository>
77 <snapshotRepository>
78 <id>snapshots</id>
79 <name>Team nexus Snapshot Repository</name>
80 <url>http://10.118.888.10:8888/nexus/content/repositories/snapshots</url>
81 </snapshotRepository>
82 </distributionManagement>
83 </project>
这里计划使用TestNG和Selenium 作为自动化测试的框架,所以添加了这两个依赖包(根据需要添加)。另外repository镜像源地址最好添加为公司私有的资源镜像地址或者国内一些开源的镜像地址,比如阿里云、网易等。
编写测试用例
在src/test/java下建立一个测试类,包名com.nitb.demo(根据自己意愿随意取),类名Demo1。
代码如下:
package com.nitb.demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.File;
public class Demo1 {
private static WebDriver driver;
private static String executePath;
@BeforeClass
public void init() {
executePath = System.getProperty("user.dir") + File.separator + "bin" + File.separator + "chromedriver_mac";
System.setProperty("webdriver.chrome.driver", executePath);
driver = new ChromeDriver();
}
@Test
public void testDemoLogin() throws InterruptedException{
System.out.print("testDemoLogin");
}
@AfterClass
public void quit() {
driver.quit();
}
}
测试用例比较简单,三个逻辑:
- 测试类实例化开始的时候,执行init,设置下当前系统应该使用哪个chromedriver,因为我是mac,且Chrome浏览器的版本是72.0.3626.81,所以去selenium官网下载了对应版本的driver放在bin下面使用。
- 使用testng.xml去运行类中有@Test注释的测试方法。
- 测试类实例销毁的时候,退出ChromeDriver。
testng.xml的内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 3 <suite name="Suite1" verbose="1" > 4 <test name="Regression1"> 5 <classes> 6 <class name="com.wilmar.loms.testcase.ManualOrderTest"/> 7 </classes> 8 </test> 9 </suite>
这里的demo属于较简单的:
- suite:代表一个测试集,里面可以包含多个测试。
- test:代表一个测试事务,里面可以运行多个测试类及多个测试方法。
- classes:包含测试事务要运行的所有测试类。
- classs:代表具体运行哪个类。
通过IDEA运行testng.xml
右击testng.xml文件,然后点击run xxx/testngxml即可。
命令行运行testng.xml
java -ea -cp $CLASSPATH org.testng.TestNG testng.xml
这里需要注意的是$CLASSPATH指的是所有的依赖包,jar包较多,这里不举例,另外最后一个参数可以是testng.xml单个文件,也可以是包含多个testng xml配置文件的路径。
【TestNG测试】TestNG、Maven、testng.xml构建测试工程的更多相关文章
- maven - pom.xml 聚合(父)工程 基本内容演示
企业开发中所用到的基本jar包以及插件都已在此 可以自己根据实际情况酌情增减 <project xmlns="http://maven.apache.org/POM/4.0.0&quo ...
- Maven创建Web工程并执行构建/测试/打包/部署
创建工程基本参考上一篇Java Application工程,不同的是命令参数变了,创建Web工程的命令如下: mvn archetype:generate -DgroupId=com.jsoft.te ...
- TestNG(十五)xml文件实现多线程测试
package com.course.testng.thread; import org.testng.annotations.Test; public class ThreadOnXml { @Te ...
- testng入门教程12 TestNG执行多线程测试
testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...
- testng入门教程11 TestNG运行JUnit测试
现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...
- testng入门教程10 TestNG参数化测试
在TestNG的另一个有趣的功能是参数测试.在大多数情况下,你会遇到这样一个场景,业务逻辑需要一个巨大的不同数量的测试.参数测试,允许开发人员运行同样的测试,一遍又一遍使用不同的值. TestNG让你 ...
- testng入门教程9 TestNG依赖测试
有时候,你可能需要在一个特定的顺序调用方法在测试案例,或你想分享一些数据和方法之间的状态.TestNG支持这种依赖测试方法之间的显式依赖它支持声明. TestNG允许指定依赖,无论与否: 使用属性de ...
- testng入门教程8 TestNG异常测试
TestNG跟踪异常处理代码提供了一个选项.可以测试是否需要代码抛出异常或不抛出. @Test注释expectedExceptions 参数一起使用.现在,让我们来看看@Test(expectedEx ...
- testng入门教程7 TestNG组测试
在TestNG中组测试是一个新的创新功能,它不存在于JUnit框架,它允许调度到适当的部分方法和瓶坯复杂的测试方法分组.您不仅可以声明属于群体的那些方法,但你也可以指定一组包含其他组.然后,TestN ...
随机推荐
- October 2nd 2017 Week 40th Monday
Grown-ups work for things. Grown-ups pay. Grown-ups suffer consequences. 真正的成年人会奋斗.会付出.会承担后果. How to ...
- September 04th 2017 Week 36th Monday
Try not to become a man of success but rather try to become a man of value. 不要努力去做一个成功的人,而要努力去做一个有价值 ...
- FTP工具FileZilla&WinSCP与FTP类库FluentFTP
FileZilla Filezilla分为client和server.其中FileZilla Server是Windows平台下一个小巧的第三方FTP服务器软件,系统资源也占用非常小,可以让你快速简单 ...
- 终端复用工具 tmux 基本操作教程
简介 在 Linux 操作环境下,终端操作是发挥 Linux 强大命令功能的重要途径,但在本地主机操作中,针对不同任务开启不同的终端,在使用时进行频繁的终端切换在某些场合下是一种使人分心和疲惫的操作, ...
- tomcat6 集群配置
1. 概要 web容器在做集群配置时,有3点需要注意: 1.1. 负载均衡配置: 1.2. session共享: 1.3. 若做的是单机集群(多个tomcat安装在同一台机器上),需要注意端口冲突问题 ...
- 前端统计利器:Sentry & Matomo
今天主要说下两款前端统计工具的使用,Sentry & Matomo.以下主要是统计代码接入方式,因此使用前提是你已经在自己的服务器上搭建好了Sentry和Matomo的服务器 Sentry统计 ...
- VM下,装centos7系统,配置nginx的问题
一.流程 1.先安装nginx依赖的包 (1)yum install gcc-c++ (2)yum install -y pcre pcre-devel (3)yum install -y zlib ...
- Centos7安装anaconda3
Centos7安装anaconda3 1. 安装bunzip2 yum install bunzip2 2. 下载anaconda3 wget https://repo.anaconda.com/ar ...
- React 入门学习笔记1
摘自阮一峰:React入门实例教程,转载请注明出处. 一. 使用React的html模板 使用React, 我们需要加载3个库,react.js, react-dom.js, 和browser.js. ...
- ajax和原生ajax、文件的上传
ajax理解: ajax发送的请求是异步处理的.也就是说如下形式: function f1(){ $.ajax( { ....... success:function(){ a= return a } ...