工程架构

iOS自动化测试:OC和Swift项目,猴子测试SwiftMon

2019-01-30  本文已影响80人  春田花花幼儿园

iOS自动化测试:OC和Swift项目,猴子测试SwiftMonkey

前话

公司对项目的要求越来越来高,崩溃率,卡顿等等都有明确的指标限制。 所以作为开发人员的自己也需要投入很大的精力去自测。但是即使这样,也很难保证涵盖的所有的测试场景。 前段时间,了解到猴子测试,年前正好有一点点空闲,有时间试验一下。
所谓的猴子测试,就是模拟用户随机操作我们的app,以求暴露问题。

最开始最有名的是ui-auto-monkey,执行代码是js。可惜在XCode7之后,XCode的Profile里,移除了UI Automation, 没有了明确的入口。自己试了几下,没有试出来。 不过不晓得放到'Run Script'里是否可行。

基于ui-auto-monkey,后来的开发者们,也开发了很多monkey,功能也更加丰富,这次我用的是SwiftMonkey

话不多说,写个demo实操起来。

一.OC项目运行SwiftMonkey

创建项目


第一步:创建项目

把SwiftMonkey中的SwiftMonkeyPaws和SwiftMonkey拖入到工程


第二步:把SwiftMonkey中的SwiftMonkeyPaws和SwiftMonkey拖入到工程
创建UI测试
第三步:创建UI测试

记住选择语言是:Swift


第四步:选择语言

这一步很关键,引入库

第五步:很关键,引入库

主工程中设置允许Swift运行


第六步:主工程中设置允许Swift运行

主工程导入SwiftMonkeyPaws,这是为了显示出猴子的爪子


第七步:主Target需要导入SwiftMonkeyPaws

把显示爪子的Swift混编进AppDelegate


第八步:把显示爪子的Swift混编进AppDelegate

最后把UI Test代码替换SMonkeyUITestUITests.swift文件中的代码

```
import XCTest
import SwiftMonkey

class SMonkeyUITestUITests: XCTestCase {

    override func setUp() {
        super.setUp()
        XCUIApplication().launch()
    }
    
    override func tearDown() {
        super.tearDown()
    }
    
    func testMonkey() {
        let application = XCUIApplication()
        
        // Workaround for bug in Xcode 7.3. Snapshots are not properly updated
        // when you initially call app.frame, resulting in a zero-sized rect.
        // Doing a random query seems to update everything properly.
        // TODO: Remove this when the Xcode bug is fixed!
        _ = application.descendants(matching: .any).element(boundBy: 0).frame
        
        // Initialise the monkey tester with the current device
        // frame. Giving an explicit seed will make it generate
        // the same sequence of events on each run, and leaving it
        // out will generate a new sequence on each run.
        let monkey = Monkey(frame: application.frame)
        //let monkey = Monkey(seed: 123, frame: application.frame)
        
        // Add actions for the monkey to perform. We just use a
        // default set of actions for this, which is usually enough.
        // Use either one of these but maybe not both.
        // XCTest private actions seem to work better at the moment.
        // UIAutomation actions seem to work only on the simulator.
        monkey.addDefaultXCTestPrivateActions()
        //monkey.addDefaultUIAutomationActions()
        
        // Occasionally, use the regular XCTest functionality
        // to check if an alert is shown, and click a random
        // button on it.
        monkey.addXCTestTapAlertAction(interval: 100, application: application)
        
        // Run the monkey test indefinitely.
        monkey.monkeyAround()
    }

}

点击这两个地方开始录制运行猴子测试



希望能和大家交流技术
Blog:http://www.lilongcnc.cc


上一篇下一篇

猜你喜欢

热点阅读