XCTHealthKit

0.1.0

An XCTest Framework to test the creation of HealthKit samples using the Health App
StanfordBDHG/XCTHealthKit

What's New

0.1.0

2023-01-15T06:33:54Z

What's Changed

Full Changelog: https://github.com/StanfordBDHG/XCTHealthKit/commits/0.1.0

XCTHealthKit

Build and Test codecov

XCTHealthKit is an XCTest-based framework to test the creation of HealthKit samples using the Apple Health App on the iPhone simulator.

How To Use XCTHealthKit

You can use XCTHealthKit in your UI tests. The framework has the following functionalities:

Add Mock Data Using the Apple Health App

Use the XCTestCase's exitAppAndOpenHealth(_: HealthAppDataType) throws function passing in an HealthAppDataType instance to add mock data using the Apple Health app:

import XCTest
import XCTHealthKit


class HealthKitUITests: XCTestCase {
    func testAddMockDataUsingTheAppleHealthApp() throws {
        try exitAppAndOpenHealth(.electrocardiograms)
        try exitAppAndOpenHealth(.steps)
        try exitAppAndOpenHealth(.pushes)
        try exitAppAndOpenHealth(.restingHeartRate)
        try exitAppAndOpenHealth(.activeEnergy)
    }
}

Handle the HealthKit Authorization Sheet

You can use the XCUIApplication's handleHealthKitAuthorization() throws function to handle the HealthKit authorization sheet:

import XCTest
import XCTHealthKit


class HealthKitUITests: XCTestCase {
    func testHandleTheHealthKitAuthorizationSheet() throws {
        let app = XCUIApplication()
        app.launch()
        
        app.buttons["Request HealthKit Authorization"].tap()
        try app.handleHealthKitAuthorization()
    }
}

Inspect the System Under Test if it Contains HKTypeIdentifier Static Text Elements

You can use the XCUIApplication's numberOfHKTypeIdentifiers() throws function to inspect the system under test if it contains HKTypeIdentifier static text elements:

import XCTest
import XCTHealthKit


class HealthKitUITests: XCTestCase {
    func testInspectTheSystemUnderTestIfItContainsHKTypeIdentifierStaticTextElements() throws {
        let app = XCUIApplication()
        app.launch()
        
        XCTAssertEqual(
            HealthAppDataType.numberOfHKTypeIdentifiers(in: app),
            [
                "HKQuantityTypeIdentifierActiveEnergyBurned": 2,
                "HKQuantityTypeIdentifierRestingHeartRate": 1,
                "HKDataTypeIdentifierElectrocardiogram": 3,
                "HKQuantityTypeIdentifierStepCount": 1
            ]
        )
    }
}

Installation

The project can be added to your Xcode project or Swift Package using the Swift Package Manager.

Xcode: For an Xcode project, follow the instructions on Adding package dependencies to your app.

Swift Package: You can follow the Swift Package Manager documentation about defining dependencies to add this project as a dependency to your Swift Package.

License

This project is licensed under the MIT License. See Licenses for more information.

Contributors

This project is developed as part of the Stanford Byers Center for Biodesign at Stanford University. See CONTRIBUTORS.md for a full list of all XCTHealthKit contributors.

Stanford Byers Center for Biodesign Logo Stanford Byers Center for Biodesign Logo

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Feb 05 2023 01:56:11 GMT-0500 (GMT-05:00)