Testing 17

How to mock UNNotificationResponse in unit tests

Issue #708

The best way to test is to not have to mock at all. The second best way is to have your own abstraction over the things you would like to test, either it is in form of protocol or some function injection.

But in case you want a quick way …

How to avoid multiple match elements in UITests from iOS 13

Issue #691

Supposed we want to present a ViewController, and there exist both UIToolbar in both the presenting and presented view controllers.

From iOS 13, the model style is not full screen and interactive. From UITests perspective there are 2 …

How to use accessibility container in UITests

Issue #690

Use accessibilityElements to specify containment for contentView and buttons. You can use Accessibility Inspector from Xcode to verify.

class ArticleCell: UICollectionViewCell {
    let authorLabel: UILabel
    let dateLabel: UILabel …

How to test DispatchQueue in Swift

Issue #646

Sync the DispatchQueue

Pass DispatchQueue and call queue.sync to sync all async works before asserting

Use mock

Use DispatchQueueType and in mock, call the work immediately

import Foundation

public protocol DispatchQueueType {
    func …

How to assert asynchronously in XCTest

Issue #644

import XCTest

extension XCTestCase {
    /// Asynchronously assertion
    func XCTAssertWait(
        timeout: TimeInterval = 1,
        _ expression: @escaping () -> Void,
        _: String = "",
        file _: StaticString = …

How to iterate over XCUIElementQuery in UITests

Issue #628

extension XCUIElementQuery: Sequence {
    public typealias Iterator = AnyIterator<XCUIElement>
    public func makeIterator() -> Iterator {
        var index = UInt(0)
        return AnyIterator {
            guard index < …

How to test drag and drop in UITests

Issue #583

In UITests, we can use press from XCUIElement to test drag and drop

let fromCat = app.buttons["cat1"].firstMatch
let toCat = app.buttons["cat2"]
let fromCoordinate = fromCat.coordinate(withNormalizedOffset: CGVector(dx: 0, …

How to generate XCTest test methods

Issue #576

Code

See Spek

Override testInvocations to specify test methods

https://developer.apple.com/documentation/xctest/xctestcase/1496271-testinvocations

Returns an array of invocations representing each test method in the test case.

Because …

How to use passed launch arguments in UITests

Issue #537

Specify launch arguments

In xcodebuild, specify launch arguments.

You can specify this under Launch Arguments in Run action of the app scheme or UITest scheme

Screenshot 2019-12-10 at 23 27 02
-AppleLanguages (jp) -AppleLocale (jp_JP)
(lldb) po ProcessInfo().arguments
▿ …

How to add monkey test to iOS apps

Issue #484

Use SwiftMonkey which adds random UITests gestures

Add to UITests target

target 'MyAppUITests' do
  pod 'R.swift', '~> 5.0'
  pod 'SwiftMonkey', '~> 2.1.0'
end

Troubleshooting

Failed to …

How to configure test target in Xcode

Issue #478

This applies to

  • Main targets
    • App
    • Framework
  • Test targets
    • Unit tests
    • UI tests

Examples

Dependencies used

Examples

  • Cocoapods
  • Carthage

Notes

  • Make sure test target can link to all the …

How to test Date with timezone aware in Swift

Issue #402

I want to test if a date has passed another date

let base =  Date(timeIntervalSince1970: 1567756697)
XCTAssertEqual(validator.hasPassed(event: event, date: base), true)

My hasPassed is using Calendar.current

func minuteSinceMidnight(date: …

How to organise test files

Issue #327

In terms of tests, we usually have files for unit test, UI test, integeration test and mock.

Out of sight, out of mind.

Unit tests are for checking specific functions and classes, it’s more convenient to browse them side by side …

How to test LaunchScreen in iOS

Issue #249

Making splash screen with LaunchScreen.storyboard is now the default way to do in iOS. Testing it with UITests is a bit tricky as this screen is showed the system, and if we test that, we are just testing the system.

What we should test is …

How to deal with animation in UITests in iOS

Issue #143

Today I was writing tests and get this error related to app idle

    t =    23.06s         Assertion Failure: <unknown>:0: Failed to scroll to visible (by AX action) Button, 0x6000003827d0, traits: 8858370049, label: 'cart', …

How to use Given When Then in Swift tests

Issue #73

Spec

Using spec testing framework like Quick is nice, which enables BDD style.

describe("the 'Documentation' directory") {
  it("has everything you need to get started") {
    let sections = Directory( …

How to run UI Test with system alert in iOS

Issue #48

Continue my post https://github.com/onmyway133/blog/issues/45. When you work with features, like map view, you mostly need permissions, and in UITests you need to test for system alerts.

Add interruption monitor

This is the code. Note that …