Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2020 SwiftDeveloper Tools

WWDC20 · 18 min · Swift / Developer Tools

Write tests to fail

Plan for failure: Design great tests to help you find and diagnose even the toughest bugs. Learn how to improve your automated tests with XCTest to find hidden issues in even the best code. We’ll explain how to prepare your tests for failure to make triaging issues easier, letting you solve interface issues and deliver fixes quickly. To get the most out of this session, you should already be familiar with writing UI tests within the XCTest framework. For more on testing tools, head over to “The suite life of testing”.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 14 snippets

Use setUpWithError() swift · at 1:58 ↗
class RecipesTests: XCTestCase {
    let app = FrutaApp()

    override func setUpWithError() throws {
        continueAfterFailure = false
        app.launchArguments.append("-recipes-tests")
        app.launch()
    }
}
Use launch arguments swift · at 3:09 ↗
class RecipesTests: XCTestCase {
    let app = FrutaApp()

    override func setUpWithError() throws {
        continueAfterFailure = false
        app.launchArguments.append("-recipes-tests")
        app.launch()
    }
}

@State private var selection: Tab = 
       CommandLine.arguments.contains("-recipes-tests") 
       ? .recipes : .menu
Design tests for a specific goal swift · at 4:12 ↗
func testIngredientsListAccuracy() throws {
    // Select Berry Blue recipe
    let recipe = try   
        app.smoothieList().selectRecipe
                           (smoothie: .berryBlue)

    // Verify ingredients list
    try recipe.verify(ingredients: 
        SmoothieType.berryBlue.ingredients)
}
Use enums for string values swift · at 4:56 ↗
public enum SmoothieType : String {
    case berryBlue = "Berry Blue"
    case carrotChops = "Carrot Chops"
    case berryBananas = "That's Berry Bananas!"
    
    var ingredients : [String] {
        switch self {
        case .berryBlue:
            return ["Orange", "Blueberry", "Avocado"]
        case .carrotChops:
            return ["Orange", "Carrot", "Mango"]
        case .berryBananas:
            return ["Almond Milk", "Banana", "Strawberry"]
        }
    }
}
Factor common code swift · at 5:25 ↗
let recipe = try app.smoothieList().selectRecipe(smoothie: .berryBlue)

public class FrutaApp : XCUIApplication {
   public func smoothieList() throws -> SmoothieList {
        let element = tables["Smoothie List"]
        if !element.waitForExistence(timeout: 5) {
            throw FrutaError.elementDoesNotExist("Smoothie List table")
        }
        return SmoothieList(app: self, element: element)
    }
}  

public class SmoothieList : FrutaUIElement {
    public func selectRecipe(smoothie: SmoothieType) throws -> Recipe {
       element.buttons[smoothie.rawValue].tap()
       return try app.recipe()
   }
}
Model UI hierarchy in testing code swift · at 5:49 ↗
public class FrutaApp : XCUIApplication {
   public func smoothieList() throws -> SmoothieList {  }
} 

public class SmoothieList : FrutaUIElement {
    public func selectRecipe(smoothie: SmoothieType) throws -> Recipe {  }
}

open class FrutaUIElement {
    let app: FrutaApp
    let element: XCUIElement
    init(app: FrutaApp, element: XCUIElement) {
        self.app = app
        self.element = element
    }
}
Use assertion messages swift · at 8:17 ↗
XCTAssertEqual(count, expectedCount, "\(SmoothieType.berryBlue.rawValue) smoothie is expected to have \(expectedCount) ingredients: \(expectedIngredients), however, there were 
\(count) found.")
Asynchronous events swift · at 9:21 ↗
public func selectRecipe(smoothie: SmoothieType) throws -> Recipe {
    element.buttons[smoothie.rawValue].tap()
    return try app.recipe()
}

public func recipe() throws -> Recipe {
    let element = scrollViews["Ingredients View"]
    if !element.waitForExistence(timeout: 5) {
        throw FrutaError.elementDoesNotExist(
                        "Ingredients View scroll view")
    }
    return Recipe(app: self, element: element)
}
Unwrapping optionals swift · at 10:19 ↗
func countFavorites(favorites: [String]?) -> Int{
     let favs = favorites!
     return favs.count
}
Unwrapping optionals continued swift · at 10:56 ↗
if let favs = favorites {  }
guard let favs = favorites else { /* throw an error */ }
let favs = favorites ?? []
let favs = try XCTUnwrap(favorites, "favorites is nil, so there is nothing to count”)
Throw errors from shared code swift · at 12:19 ↗
public func verify(ingredients: [String]) throws {
    try XCTContext.runActivity(named: "Verifying \(ingredients) exists in the Recipe screen.")
    { verifyingRecipe in
        for ingredient in ingredients {
            if !element.switches[ingredient].waitForExistence(timeout: 5) {
                throw RecipeError.ingredientDoesNotExist(ingredient)
            }
        }
    }
}

public enum RecipeError : Error, CustomStringConvertible {
    case ingredientDoesNotExist(String)

    public var description : String {
        switch self {
        case .ingredientDoesNotExist(let ingredient):
            return "\(ingredient) does not exist in the Ingredients View.)"
        }
    }
}
Use XCTContext.runActivity() swift · at 13:41 ↗
public func verify(ingredients: [String]) throws {
    try XCTContext.runActivity(named: "Verifying \(ingredients) exists in the Recipe screen.")
    { verifyingRecipe in
        for ingredient in ingredients {
            if !element.switches[ingredient].waitForExistence(timeout: 5) {
                throw RecipeError.ingredientDoesNotExist(ingredient)
            }
        }
    }
Add attachments to the result bundle swift · at 14:02 ↗
public func verify(ingredients: [String]) throws {
    try XCTContext.runActivity(named: "Verifying \(ingredients) exists in the Recipe screen.")
    { verifyingRecipe in
        for ingredient in ingredients {
            if !element.switches[ingredient].waitForExistence(timeout: 5) {
                let attachment = XCTAttachment(string: element.debugDescription)
                verifyingRecipe.add(attachment)
                 throw RecipeError.ingredientDoesNotExist(ingredient)
            }
        }
    }
Use XCTSkip swift · at 14:50 ↗
let debuggingTests = false

func testSelectSmoothie() throws {
    try XCTSkipUnless(debuggingTests == true, "This test is not yet implemented.")
}