Jenkins如何执行本地终端命令

因为需要执行本地的newman命令文件,在Jenkins中的创建一个Item,  点击配置:configure  再点击Build, 添加一个选项 execute shell 

添加shell执行选项

如果是windows ,执行放在一个*.bat文件

文件里面写上命令:

@echo off

call newman run D:\newman\autoTest\scripts\测试接口.postman_collection.json -e D:\newman\autoTest\env\测试环境.postman_environment.json -g D:\newman\autoTest\global\workspace.postman_globals.json 

在命令行中写上以下命令 先找到文件存储目录,再执行:


        cd  /d "D:/newman/autoTest"

        call someTest.bat


如果是mac电脑,执行放在*.sh文件中,在.sh文件中写上需要执行的命令,我存在testNewman.sh文件中的。

现在需要Jenkins帮忙执行, 再在jenkins中的execute shell 命令行中写上以下内容,先找到文件存储目录,再执行:


    cd /Users/zhen/Documents/AutomatedTesting/newman

    rm -r -f *.html

    cd /Users/zhen/Documents/AutomatedTesting

    sh testNewman.sh

     cd /Users/zhen/Documents/AutomatedTesting/newman

     python3 testHTMLReadFile.py



上面的意思就是,找到存储html的目录,先清除上一次的结果,然后在进入newman的命令文件,执行所有的测试案例,再对测试结果(生成的html文件)进行分析

其中 “sh testNewman.sh  ” 也是保存的终端命令组合,里面也是一堆newman执行的命令行,因为需要经常修改添加内容,所以用文件保存起来会方便很多,这也好分类一点,以下就是文件内容,用newman命令执行代码并生成html文件,最后testHTMLReadFile.py文件是为了解析html文件,把结果分析出来。

newman run /Users/zhen/Documents/AutomatedTesting/FirstTestDemo/001Test.postman_collection.json -e /Users/zhen/Documents/AutomatedTesting/FirstTestDemo/测试环境.postman_environment.json -g /Users/zhen/Documents/AutomatedTesting/FirstTestDemo/workspace.postman_globals.json -r htmlextra

newman run /Users/zhen/Documents/AutomatedTesting/FirstTestDemo/002Test.postman_collection.json -e /Users/zhen/Documents/AutomatedTesting/FirstTestDemo/测试环境.postman_environment.json -g /Users/zhen/Documents/AutomatedTesting/FirstTestDemo/workspace.postman_globals.json -r htmlextra

你可能感兴趣的:(Jenkins如何执行本地终端命令)