Maven+testNG 测试集成测试

本文详细介绍了如何在Maven项目中配置TestNG进行自动化测试。通过在pom.xml中添加maven-surefire-plugin插件,解决中文乱码问题,并指定testng.xml作为测试套件。同时,配置了resources资源,确保测试用例正常运行。文章还提到了在不同操作系统下正确处理文件路径的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目构建需要添加如下配置:

1.pom.xml  添加 <build></build>配置

 <build>
        <plugins>
          
           <!--添加插件 关联testng-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <!-- 解决maven test命令时console出现中文乱码乱码 -->
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <file>testng.xml</file>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>

        <!--配置resources 资源-->
      <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.xls</include>
            </includes>
            <filtering>false</filtering>
           </resource>
      </resources>
  </build>

1.build 配置中添加<file>testng.xml</file> 也就是要执行的testng 文件。

创建testng.xml,文件放在项目根目录下,和pom.xml 同级。testng.xml 如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="httptest">

    <test name="httptest" preserve-order="true" verbose="3">
        <packages>
            <package name="com.test.cases.*" />
        </packages>
    </test>
</suite>

扫描要执行测试的包cases

配置完成后,本地terminal中执行:mvn test ,如下显示我么们bulid 做的事情。

1.读取resources,2.执行test

最后测试结果:

成功了!!

另外需要注意的是,linux 系统读取配置文件中文件路径 只识别‘/’ 不识别,”//“或者’/‘,不然在jenkins 构建过程中会出现这样的错误信息。

 在我们的Java代码总需要做这样的操作:在获取的地址对象后面加.replace("\\", File.separator);或者.replace("/", File.separator);

根据你的配置文件中地址符号改变。个人windows 电脑习惯写“\\src\\main\\resources\\APIcase.xls ”这样的路径。因此选择.replace("\\", File.separator);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值