Skip to content

Instantly share code, notes, and snippets.

@nuno-vieira
Created December 28, 2020 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuno-vieira/2a56aeec45d4c40374fb251ace412344 to your computer and use it in GitHub Desktop.
Save nuno-vieira/2a56aeec45d4c40374fb251ace412344 to your computer and use it in GitHub Desktop.
XCTest Gherkin
import XCTest
extension XCTest {
func Given(_ text: String, block: () -> Void ) {
runActivity(callingFunction: resolveGherkinTag(from: #function), text: text, block: block)
}
func When(_ text: String, block: () -> Void ) {
runActivity(callingFunction: resolveGherkinTag(from: #function), text: text, block: block)
}
func Then(_ text: String, block: () -> Void ) {
runActivity(callingFunction: resolveGherkinTag(from: #function), text: text, block: block)
}
func And(_ text: String, block: () -> Void ) {
runActivity(callingFunction: resolveGherkinTag(from: #function), text: text, block: block)
}
func But(_ text: String, block: () -> Void ) {
runActivity(callingFunction: resolveGherkinTag(from: #function), text: text, block: block)
}
func runActivity(callingFunction: String, text: String, block: () -> Void) {
XCTContext.runActivity(named: callingFunction + " " + text) { activity in
block()
let screenshot = XCTAttachment(screenshot: XCUIScreen.main.screenshot())
screenshot.lifetime = .deleteOnSuccess
activity.add(screenshot)
}
}
private func resolveGherkinTag(from functionText: String) -> String {
let blockFunctionParameterString = "(_:block:)"
return functionText.replacingOccurrences(of: blockFunctionParameterString, with: "")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment