Automattic / Automattic/simplenote-ios

Throw instead of failing from code in UITestHelpers

Open
#1,244 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
2.1k
Forks
297
Avg merge
13h 25m
Merged PRs (30d)
6

Description

In this WWDC 2020 session, Apple's Kelly Keenan shares this recommendation:

In my tests, the rule is that I always throw instead of assert from shared code. The reason is because the shared code is being run from many tests, and in some of those tests, I may purposely be testing negative test cases to ensure something hidden isn't shown or to force an error dialog to appear for testing purposes.

I couldn't agree more. I picture UITestHelpers as a set of sharp tools that test authors can easily fit in their scenarios. The less assumptions it makes and the less constrains it puts on the flow, the better.

To make this happen, we'd have to define some kind of custom error for our framework to throw.

I think a neat place to start might be a nested type, like:

extension XCUIElement {

	enum ElementError: Equatable, Error {
	    case doesNotSupportTextManipulation
	}
}

With this type, we could update the following code (taken from this PR) from:

public func clearText() {
    guard let stringValue = self.value as? String else {
        XCTFail("Tried to clear and enter text into a non string value")
        return
    }

to:

public func clearText() throws {
    guard let stringValue = self.value as? String else {
		throw XCUIElement.ElementError.doesNotSupportTextManipulation
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating UITestHelpers and the XCUIElement.clearText implementation referenced in the issue and linked PR. Trace its current XCTFail path and the affected test callers; done means shared helper failures throw a custom XCUIElement error instead of asserting, with the UI test suite still compiling and passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
mobile-dev, testing
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.