【TestNG快板说一】TestNG、Maven、testng.xml构建测试工程

目录

创建一个maven工程

pom.xml中的内容

 编写测试用例

 testng.xml的内容

 通过IDEA运行testng.xml

命令行运行testng.xml


创建一个maven工程

使用Idea创建maven工程

建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是用来进行自动化测试的,所以实际这里src/main/java是用不到的,只是IDEA统一规则建立了而已。这里个人还建立了一个bin目录,用来存放chromedriver等浏览器执行文件,用来匹配不同的操作系统及不同的浏览器版本。

pom.xml中的内容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wilmar.test</groupId>
    <artifactId>woodpecker</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--<packaging>jar</packaging>-->

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <excludeScope>provided</excludeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>Team nexus Release Repository</name>
            <url>http://10.118.888.10:8888/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>Team nexus Snapshot Repository</name>
            <url>http://10.118.888.10:8888/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>
</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();
    }
}

测试用例比较简单,三个逻辑:

  1. 测试类实例化开始的时候,执行init,设置下当前系统应该使用哪个chromedriver,因为我是mac,且Chrome浏览器的版本是72.0.3626.81,所以去selenium官网下载了对应版本的driver放在bin下面使用。
  2. 使用testng.xml去运行类中有@Test注释的测试方法。
  3. 测试类实例销毁的时候,退出ChromeDriver。

 testng.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
    <test name="Regression1">
        <classes>
            <class name="com.wilmar.loms.testcase.ManualOrderTest"/>
        </classes>
    </test>
</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配置文件的路径。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值