XCTestCase

open class XCTestCase : XCTest.XCTestCase

The primary class to define tests in XCTBeton.

Extends the capabilities of XCTestCase with measure(metrics:options:block:) and XCTAssertMetric(_:_:_:_:file:line:). You can use these features if you want to write performance tests with assertions.

The following example shows how you can write a simple assertion:

import XCTBeton

class PerformanceTests: XCTestCase {
  func test_measureSum() {
    measure {
      let _ = (1..<1000).reduce(0, +)
    }
    XCTAssertMetric(.clock, .timeMonotonic, .average(maximum: 0.001))
  }
}
  • Default options for measure(_:). The default instance specifies 5 iteration counts.

    Declaration

    Swift

    open override class var defaultMeasureOptions: XCTMeasureOptions { get }
  • Asserts that the given metric has the expected aspect.

    You can make assertions to performance metrics if you measured your code using some variant of measure(_:). Make sure to assert only for XCTMetrics you configured, otherwise you get an assertion error.

    measure(metrics: [XCTCPUMetric()]) {
      let _ = (1..<1000).reduce(0, +)
    }
    XCTAssertMetric(.cpu, .time, .average(maximum: 0.002))
    // The following assertion would fail, no memory results collected.
    // XCTAssertMetric(.memory, .physical, .average(maximum: 20))
    

    Declaration

    Swift

    public final func XCTAssertMetric<Identifier>(
      _ metric: XCTAssertMetric<Identifier>,
      _ identifier: Identifier,
      _ aspect: XCTAssertMetric<Identifier>.Aspect,
      _ message: @autoclosure () -> String = "",
      file: StaticString = #filePath,
      line: UInt = #line
    )

    Parameters

    metric

    The asserted performance metric.

    identifier

    Part of the metric to make assertion for.

    aspect

    The aspect to compare the performance metrics with.

    message

    An optional description of a failure.

    file

    The file where the failure occurs. The default is the filename of the test case where you call this function.

    line

    The line number where the failure occurs. The default is the line number where you call this function.

  • Measures the performance of a block of code.

    Call this method from within a test method to measure the performance of a block of code. By default, this method measures the number of seconds the block of code takes to execute. Use measure(metrics:block:) for example to change the default metrics measured by this method.

    After using this method you can make assertions to the collected metrics using XCTAssertMetric(_:_:_:_:file:line:).

    Declaration

    Swift

    @nonobjc
    open override func measure(_ block: () -> Void)

    Parameters

    block

    A block whose performance is measured.

  • Records the selected metrics for a block of code.

    After using this method you can make assertions to the collected metrics using XCTAssertMetric(_:_:_:_:file:line:).

    Declaration

    Swift

    @nonobjc
    open func measure(metrics: [XCTMetric], block: () -> Void)

    Parameters

    metrics

    An array of metrics to measure, like CPU, memory, or elapsed time.

    block

    A block whose performance is measured.

  • Records the performance, using the specified measurement options, for a block of code.

    After using this method you can make assertions to the collected metrics using XCTAssertMetric(_:_:_:_:file:line:).

    Declaration

    Swift

    @nonobjc
    open func measure(options: XCTMeasureOptions, block: () -> Void)

    Parameters

    options

    Options to control the gathering of performance measurements.

    block

    A block whose performance is measured.

  • Records the selected metrics, using the specified measurement options, for a block of code.

    After using this method you can make assertions to the collected metrics using XCTAssertMetric(_:_:_:_:file:line:).

    Declaration

    Swift

    @nonobjc
    open func measure(metrics: [XCTMetric], options: XCTMeasureOptions, block: () -> Void)

    Parameters

    metrics

    An array of metrics to measure, like CPU, memory, or elapsed time.

    options

    Options to control the gathering of performance measurements.

    block

    A block whose performance is measured.