目前跑的接口自动化是通过Testng + Jenkins来做的, 但是项目可能跑在不同的平台上, 不同的平台有不同的ip.
目前是使用了Jenkins的参数化构建功能, 每次跑的时候,手动输入一个ip,然后对该ip的服务进行自动化测试.
Testng框架的配置文件是application.properties, 我们将服务的ip记录在这个文件中.
根据从Jenkins传入的参数, 动态修改application.properties中的ip变量,然后进行后续测试工作.
application.properties 示例
server_ip=http://192.168.100.100
port=9000
path=/home/data
目前想到的方法有2个:
从最简化考虑,优先考虑shell的方法.
Linux有3剑客: grep ,sed, awk.其中grep主要做过滤, sed主要做文本的相关处理(如修改替换等),awk主要做数据处理,报告输出等。所以应该考虑使用sed命令来解决此问题.
本次用到的sed命令截图
sed "/server_ip=/c server_ip=http://192.168.200.200" application.properties
sed -i "/server_ip=/c server_ip=http://192.168.200.200" application.properties
注: Jenkins使用传入变量的格式为:${变量名}
#!/bin/bash
echo "传入的ip是:${server_ip}"
# 根据传入参数修改项目的ip
cd /home/data/jenkins/workspace/maven_testng/maven_testng/src/main/resources
sed -i "/server_ip=/c server_ip=http://${server_ip}" application.properties
echo "项目已更改为传入的ip: ${server_ip}"
踩坑点: sed 命令后面要使用双引号, 不能使用单引号, 否则会将变量识别成普通字符串!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有