美文网首页tom程序员iOS程序猿
三十张图了解如何简单搭配Jenkins+KIF实现自动化测试

三十张图了解如何简单搭配Jenkins+KIF实现自动化测试

作者: 五分钟学算法 | 来源:发表于2017-05-26 09:57 被阅读127次

    本文主要讲解如何使用开源社区的一个CI工具Jenkins来搭建iOS的的CI环境

    1.下载并运行Jenkins

    在Mac环境下,我们需要先安装JDK,然后在Jenkins的官网.下载最新的war包。
    下载完成后,打开终端,进入到war包所在目录,执行以下命令:

    java -jar jenkins.war --httpPort=8080
    

    进行安装

    图一.png
    图二.png 图三.png

    httpPort指的就是Jenkins所使用的http端口,这里指定8080,可根据具体情况来修改。待Jenkins启动后,在浏览器页面输入以下地址:

    http://localhost: 8080
    
    

    安装完成之后重新打开[http://localhost: 8080](http://localhost: 8080) 此时需要用户输入密码

    图四.png

    该密码文件在路径 /Users/Shared/Jenkins/Home/secrets 下无法直接打开 可以终端输入

    sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword 
    

    获取内容

    2.Jenkins自动配置安装

    图五.png
    图六.png

    设置用户名密码

    图七.png

    3.安装插件

    设置完毕后进入[http://localhost: 8080](http://localhost: 8080) 再配置一些我们所需要的插件

    图八.png

    通过过滤搜索,我们安装几个后续需要使用的插件(可根据自己实际需要安装其他的辅助插件)

    • GitLab Plugin
    • Gitlab Hook Plugin
    • Xcode integration
    • Keychains and Provisioning Profiles Management
    • Credentials Plugin
    图九.png

    4.配置SSH与KeyChain

    添加SSH.

    在Jenkins管理页面,选择“Credentials”,然后选择“Global credentials”,点击“Add Credentials”,进行添加SSH.

    图十.png

    Private Key的获取是在路径 /Users/管理员用户名/.ssh 里面,可以通过终端输入 open ~/.ssh 打开文件夹,找到id_rsa (注意不是id_rsa.pub
    ) 将里面内容全部复制 填写时
    开头是-----BEGIN RSA PRIVATE KEY-----

    结尾是-----END RSA PRIVATE KEY-----

    iOS打包需要签名文件和证书

    在系统管理页面,选择刚刚安装好的插件“Keychains and Provisioning Profiles Management”。

    图十一.png

    上传keychain

    先将钥匙串登录里面的相关开发者证书复制到系统目录下


    图十二.png

    然后

    图十三.png
    点击选取文件 选择路径/Users/管理员用户名/Library/keychains/login.keychain 然后点击Upload

    Code Signing Identity 填写复制的内容

    图十四.png

    然后再点击选取文件 选择路径/Users/管理员用户名/Library/MobileDevice/Provisioning Profiles下自己项目所需要的配置文件

    成功后如下所示

    图十六.png

    理论上Jenkins会把这个Keychain拷贝到/Users/Shared/Jenkins/Library/keychains这里,(Library是隐藏文件)。Provisioning Profiles文件也直接拷贝到/Users/Shared/Jenkins/Library/MobileDevice文件目录下。

    执行上述操作后查看是否有路径Users/Shared/Jenkins/Library/keychainsUsers/Shared/Jenkins/Library/MobileDevice 如果不存在这两个文件夹,自己先创建两条路径 然后将login.keychain与login.keychain-db和配置文件夹复制到相应的文件夹内

    否则容易出现问题:

    No profile matching 'getgetsetset5CommonTest' found: Xcode couldn't find a profile matching 'getgetsetset5CommonTest'. Install the profile (by dragging and dropping it onto Xcode's dock item) or select a different one in the General tab of the target editor

    即 Jenkins 在指定的目录下没有找到对应的profile文件

    解决方案:
    /Users/Shared/Jenkins/Library/MobileDevice/Provisioning Profiles 看看这个目录下面,有没有对应的profile文件,如果没有,可以手动拷贝相应的profile文件,如果中间某些目录(例如 :MobileDevice)缺失,也可以自己手动创建

    或是下面问题

    FATAL: Unable to unlock the keychain. Will not set default: file /Users/Shared/Jenkins/Library/Keychains/login.keychain

    10.12系统后 keychain变成keychain-db

    所以最好将login.keychain与login.keychain-db 都复制过去

    5.构建

    插件全部安装完毕后,下面就可以正式开始自动化构建了!!!

    1)点击新建进行构建新的项目

    图十七.png

    2)设置参数

    图十八.png

    为了方便举例子,这里使用GitHub上的项目,具体情况可以使用自己公司的项目地址

    图十九.png

    如果出现上述的红色警告,请检查SSH是否配置正确,配置成功如下图


    图二十.png

    Poll SCM (poll source code management) 轮询源码管理
    需要设置源码的路径才能起到轮询的效果,因为这个项目主要是用来测试,因此时间设置为60分钟才查询一下

    图二十一.png

    3)添加脚本

    Xcode integration也是可以进行打包测试的,但xcode8.0之后老是有一下问题,因此直接使用脚本构建

    图二十二.png

    点击 增加构建步骤 选择 Execute shell 填写
    xcodebuild clean build test -workspace JenkinsWithKIFDemo.xcworkspace -scheme JenkinsWithKIFDemo -destination 'platform=iOS,name=coderOnePolo'

    本来是在模拟器进行测试的 但是老是报错
    iPhoneSimulator: Could not launch simulator: -10810,即使按照SETTING UP JENKINS CI ON A MAC的设置依旧有问题,所以现在先用真机进行测试

    图二十三.png

    到目前为止 基本的构建已经完成 后面可以根据需要再添加远程测试或者添加报表等需求。

    3.使用KIF进行测试

    使用Cocoapod管理,在项目的Podfile file中添加KIF

    # Uncomment the next line to define a global platform for your project
     platform :ios, '9.0'
    
    target 'JenkinsWithKIFDemo' do
      # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
      # use_frameworks!
       pod 'AFNetworking','~>2.5.4'
    end
    target 'JenkinsWithKIFDemoTests' do
        #inherit! :search_paths
         pod 'KIF', :configurations => ['Debug']
    
        # Pods for testing
    end
    
    图二十四.png

    如果是在旧的项目引入Test,则需要添加以下几个配置


    图二十五.png
    图二十六
    图二十七.png
    图二十八
    图二十九.png

    之后就是添加测试代码了,简单的测试效果如下

    测试.gif

    使用Jenkins实现自动化测试

    接下来就是见证成果的时候了,可以自己马上点击立即构建进行测试


    图三十.png
    图三十一.png

    配置好后当自己的代码修改提交到远程仓库后,Jenkins就能马上自动化测试代码。(__)

    补充 配置时候的一些坑

    qustion:1.A build only device cannot be used to run this target

    I get this error when I trying to test an app.
    A build only device cannot be used to run this target.
    No supported iOS devices are available. Connect a device to run your application or choose a simulated device as the destination.

    add -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.3' to your xcodebuild

    qustion:2.Could not launch simulator: -10810.

    I had exact same issue. It happens because Mac OS X doesn't allow an iOS simulator to run in the BACKGROUND.

    If you are constructing a Jenkins CI environment, and trying to unit test via the simulator, you can't run the simulator, because Jenkins is basically run as a DAEMON. This also happens if you're running the tests behind environments like tmux or screen.

    Here is a great tutorial to read that can help you fix this.

    Cheers!

    qustion:3 The target … overrides the OTHER_LDFLAGS build setting defined in `Pods/Pods.xcconfig

    在那个target下就去设置哪个target的setting

    This definitely works most of the time:

    Go to your target Build Settings -> Other linker flags -> double click . Add $(inherited) to a new line.

    If you have problem with "...target overrides the GCC_PREPROCESSOR_DEFINITIONS build setting defined in..." then you must add $(inherited) to your target Build Settings -> Preprocessor Macros

    qustion:4 出现错误 Failed to connect to repository : Command "git ls-remote -h

    选择SSH Username with private key
    直接输入私钥,要完整复制 ~/.ssh/id_isa里面的内容。

    相关文章

      网友评论

        本文标题:三十张图了解如何简单搭配Jenkins+KIF实现自动化测试

        本文链接:https://www.haomeiwen.com/subject/wainxxtx.html