【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 ...
随机推荐
- nodejs API(一)
不要注重版本 URL 官网所在位置:https://nodejs.org/dist/latest-v8.x/docs/api/url.html URL网址解析的好帮手: url有三个可调用的方法:ur ...
- [2018HN省队集训D1T1] Tree
[2018HN省队集训D1T1] Tree 题意 给定一棵带点权树, 要求支持下面三种操作: 1 root 将 root 设为根. 2 u v d 将以 \(\operatorname{LCA} (u ...
- angularjs-$location
$location服务分析浏览器地址栏中的URL(基于window.location),让我们可以在应用中较为方便地使用URL里面的内容.在地址栏中更改URL,会响应到$location服务中,而在$ ...
- React & TypeScript
之前看了一下 TypeScript 的知识,但是一直没有上手,最近开始结合 React 和 TypeScript 一起尝试了一下,感受还是很好的,所以写一下笔记. 环境配置没有参考其他东西,就是看了下 ...
- isa class superclass metaclass
http://www.cnblogs.com/feng9exe/p/7232915.html Note: 其实这里的难点就在于对 和 的区分. .class 当 target 是 Instance 则 ...
- linux 用户和组及文件权限的相关内容
1.添加用户(只有管理员用户或具有管理员权限的用户可以添加删除用户) useradd 用户名 (laoda) 新创建用户后home下会多一个laoda的目录 ,此目录中存放的是laoda用户相关的 ...
- 虚机的SQL Server空间占满之后进行释放的一些操作
用了好几年的一个虚机,数据库是SQL Server 2008,配额500M. 今天忽然发现无法录入数据,登录后台一看,原来是数据库容量满了. 很久没用SQL Server了,找到一段之前用过的收缩数据 ...
- 【转】合格PHP工程师的知识结构
PHP的运行环境 连环境都搞不起来,就是你有多么喜欢PHP,那也是白搭,开始我们大多会使用集成环境软件例如xampp,wamp.随着知识的增加慢慢要学会自己搭建运行环境,例如 Linux(Ubuntu ...
- 【原创】大叔经验分享(53)kudu报错unable to find SASL plugin: PLAIN
kudu安装后运行不正常,master中找不到任何tserver,查看tserver日志发现有很多报错: Failed to heartbeat to master:7051: Invalid arg ...
- $Mayan$游戏
\(Mayan\)游戏 好啊,一年(半年)来的梦魇,终于结束了. 其实我从来没料到整体竟然会如此暴力--做的时候机房里冷得很,感觉晕晕乎乎地做完了,晕晕乎乎地调了好久,晕晕乎乎地听(看了题解的)\(q ...