Skip to content

StanfordBDHG/XCTHealthKit

Repository files navigation

XCTHealthKit

Build and Test codecov DOI

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 API documentation provides a detailed overview of the public interface of XCTHealthKit.

The framework has the following functionalities:

Add Mock Data Using the Apple Health App

Use the XCTestCase.launchAndAddSample(healthApp:_:) throws function passing in an NewHealthSampleInput instance to add mock data using the Apple Health app:

import XCTest
import XCTHealthKit

class HealthKitUITests: XCTestCase {
    func testAddMockData() throws {
        let healthApp = XCUIApplication.healthApp
        try launchAndAddSample(healthApp: healthApp, .steps(value: 71))
        try launchAndAddSample(healthApp: healthApp, .electrocardiogram())
    }
}

Alternatively, the XCTestCase.launchAndAddSamples(healthApp:_:) throws function can be used to add multiple samples in a single call:

import XCTest
import XCTHealthKit

class HealthKitUITests: XCTestCase {
    func testAddMockData() throws {
        let healthApp = XCUIApplication.healthApp
        try launchAndAddSamples(healthApp: healthApp, [
            .activeEnergy(),
            .electrocardiogram(),
            .pushes(value: 117),
            .restingHeartRate(value: 91),
            .steps()
        ])
    }
}

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()
    }
}

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

About

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

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages