如何按顺序使用功能

时间:2017-03-30 11:41:19

标签: ios swift3 xcode8 xcode-ui-testing

import XCTest

class TestClass: XCTestCase
{

    override func setUp() {
        super.setUp() 
        // Put setup code here. This method is called before the invocation of each test method in the class.

        // In UI tests it is usually best to stop immediately when a failure occurs.
        continueAfterFailure = true
        // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
        XCUIApplication().launch()

        // UIApplication.shared.keyWindow?.layer.speed = 100
        UIApplication.sharedApplication().keyWindow?.layer.speed = 100

        // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample()
    {
    }

    func testA1_TC8InvalidEmailAndPasswordTC8_A1()
    {
        let app = XCUIApplication()
        let emailIdTextField = app.textFields["Email ID"]
        emailIdTextField.tap()

        if emailIdTextField.value as! String != ""
        {
            emailIdTextField.clearAndEnterText(emailIdTextField.value as! String)
        }

        emailIdTextField.typeText("test@gmail.com")

        let passwordSecureTextField = app.secureTextFields["Password"]
        passwordSecureTextField.tap()
        passwordSecureTextField.typeText("dgbnnnbb")
        app.buttons["Login"].tap()
        app.buttons["OK"].tap()
    }

    func testA2_TC9KeepAllFieldBlankTC9_A2()
    {
        let app = XCUIApplication()
        let emailIdTextField = app.textFields["Email ID"]
        emailIdTextField.tap()

        if emailIdTextField.value as! String != ""
        {
            emailIdTextField.clearAndEnterText(emailIdTextField.value as! String)
        }

        app.textFields["Email ID"].tap()
        let passwordSecureTextField = app.secureTextFields["Password"]
        app.secureTextFields["Password"].tap()

        if passwordSecureTextField.value as! String != ""
        {    
            passwordSecureTextField.clearAndEnterText(passwordSecureTextField.value as! String)
        }

        app.secureTextFields["Password"].tap()
        app.buttons["Login"].tap()
        app.buttons["OK"].tap()
    }

    func testTC10ValidUsernameAndInvalidPasswordTC10C()
    {
        let app = XCUIApplication()
        let emailIdTextField = app.textFields["Email ID"]
        emailIdTextField.tap()

        if emailIdTextField.value as! String != ""
        {
            emailIdTextField.clearAndEnterText(emailIdTextField.value as! String)
        }

        emailIdTextField.typeText("gfdedkff@gmail.com")

        let passwordSecureTextField = app.secureTextFields["Password"]
        passwordSecureTextField.tap()
        passwordSecureTextField.typeText("grtegrgrtst")

        let moreNumbersKey = app.keys["more, numbers"]
        moreNumbersKey.tap()
        passwordSecureTextField.typeText("@123")
        app.buttons["Login"].tap()
        app.buttons["OK"].tap()
    }

我在测试类中有多个函数。我想依次使用这些功能(同步),因为功能一结果是功能二输出或参数。你能帮我找出解决方案吗?

3 个答案:

答案 0 :(得分:3)

在我们的项目中,我们遇到了类似的问题。您无法在XCTest上强制执行测试执行顺序。我们通过描述每个测试的所有步骤的方法来解决我们的问题,然后我们可以每次执行整个路径并且仅检查每个测试中的特定点。

要检查它是否适用于您提供的示例代码,我创建了测试应用>>here<<。存储库位于GitHub上,需要一些工作,所以如果你能看一下我将不胜感激。我需要一个基本的测试表单和尽可能少的代码来测试我的解决方案。最重要的部分是UI测试,你可以看到两个版本。一个在香草XCTest和一个重构使用我与同事一起开发的图书馆 - AutoMate。它让我清理代码。

当我在你的情况下测试这个解决方案时,我发现你可能会在启动之间持续存在状态。这就是我引入可以传递给应用程序的选项的原因。

var app: XCUIApplication!
override func setUp()
{
    super.setUp()
    // Put setup code here. This method is called before the invocation of each test method in the class.

    // In UI tests it is usually best to stop immediately when a failure occurs.
    continueAfterFailure = true
    // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
    app = XCUIApplication()
    app.launchEnvironment["NUMBER_OF_ATTEMPTS"] = "0"
    app.launch()

    // UIApplication.shared.keyWindow?.layer.speed = 100
    //        UIApplication.sharedApplication().keyWindow?.layer.speed = 100
}

func testA1_TC8InvalidEmailAndPasswordTC8_A1()
{
    setUp(forTest: 1)
    // Do Your asserts here
}

func testA2_TC9KeepAllFieldBlankTC9_A2()
{
    setUp(forTest: 2)
    // Do Your asserts here
}

func testTC10ValidUsernameAndInvalidPasswordTC10C()
{
    setUp(forTest: 3)
    // Do Your asserts here
}

func setUp(forTest testsCount: Int) {
    let credentials = [
        ("test@gmail.com", "dgbnnnbb"),
        ("", ""),
        ("gfdedkff@gmail.com", "grtegrgrtst")
    ]
    let emailIdTextField = app.textFields["Email ID"]
    let passwordSecureTextField = app.secureTextFields["Password"]

    for (no, (email, password)) in credentials.prefix(upTo: testsCount).enumerated() {
        fill(emailIdTextField, with: email)
        switch no {
        case 0:
            fill(passwordSecureTextField, with: password, clear: false)
        case 1:
            fill(passwordSecureTextField, with: password)
            passwordSecureTextField.tap()
        case 2:
            fill(passwordSecureTextField, with: password, clear: false)
            let moreNumbersKey = app.keys["more, numbers"]
            moreNumbersKey.tap()
            passwordSecureTextField.typeText("@123")
        default:
            break
        }
        app.buttons["Login"].tap()
        app.buttons["OK"].tap()
    }
}

func fill(_ field: XCUIElement, with text: String, clear: Bool = true) {
    field.tap()

    if clear, let stringValue = field.value as? String, stringValue != ""
    {
        field.clearAndEnterText(field.value as! String)
    }

    field.typeText(text)
}

答案 1 :(得分:0)

您正在使用的此测试框架(Apple提供的XCTest)旨在使测试彼此独立。这非常(很多)很好的理由超出了这个答案的范围。

你可以做到这一点,但你将全面对抗框架,或者失去Xcode和XCTest框架为你提供的关键功能。

快速简单的方法就是只有一个test() func。然后将所有单独的测试编写为辅助函数,并在func test()内按顺序调用它们。

因此...

func testSequentially() {
    assertThisFirst()
    assertThisSecond()
}

func assertThisFirst() {
    // Some test
}

func assertThisSecond() {
    // Some other test
}

现在让我重申一点,这不是运行测试的好方法。你错过了运行单个测试的能力。你在Xcode中错过了许多很好的用于检查测试的GUI。当不可避免地发生故障时,您的测试将很多难以调试。一次又一次......我祝你好运!

答案 2 :(得分:0)

您可以尝试以下代码:

let randomFunction = [test1(),test2(),test3()]
testMethodA()
testMethodB()
testMethodC()