How can I assert that a value of a string is equal to one of any string values in an array using XCTAssert in Swift? -


i'm new swift , followed simple tutorial make magic 8 ball cocoa app every time click ball shows different piece of advice. trying practice ui automated tests asserting (xctassert) "piece of advice" label equal 1 of string values in array. array looks , in viewcontroller.swift:

var advicelist = [     "yes",     "no",     "tom says 'do it!'",     "maybe",     "try again later",     "how can know?",     "totally",     "never",     ] 

how can make assertion in uitests.swift file asserts string shown equal 1 of string values in array above?

it's possible you're asking how access application state ui test, or in general ui testing.

i think it's pretty interesting question i'm going answer because it's don't know lot , prompt other people chime in , correct.

background: basic magic 8 ball project

i set basic project view controller contains 2 views: label , button. tapping button updates label text random message:

import uikit  struct eightball {     static let messages = ["yes", "no", "it's not certain"]      var newmessage: string {         let randomindex = int(arc4random_uniform(uint32(eightball.messages.count)))         return eightball.messages[randomindex]     } }  class viewcontroller: uiviewcontroller {      let ball = eightball()      @iboutlet weak var messagelabel: uilabel!      @ibaction func shakeball(_ sender: any) {         messagelabel.text = ball.newmessage     } } 

a basic ui test

here's commented ui test showing how automate tapping on button, , grabbing value of label, , checking value of label valid message.

import xctest  class magicuitests: xctestcase {      // method called before invocation of each test method in class.     override func setup() {         super.setup()          // in ui tests best stop when failure occurs.         continueafterfailure = true         // ui tests must launch application test. doing in setup make sure happens each test method.         xcuiapplication().launch()     }      func testvalidmessage() {          // grab reference application         let app = xcuiapplication()          // #1         // grab reference label accesability identifier 'messagelabel'         let messagelabelstatictext = app.statictexts["messagelabel"]          // tap button text 'shake'         app.buttons["shake"].tap()          // text of label         let messagelabeltext = messagelabelstatictext.label          // #2         // check if text in label matches 1 of allowed messages         let isvalidmessage = eightball.messages.contains(messagelabeltext)          // test fail if message not valid         xctassert(isvalidmessage)     } } 

at #1 approach i'm using label access labels accessibilityidentifier property. project entered through storyboard, if you're setting views in code can directly set accessibilityidentifier property yourself.

the other thing that's confusing here access elements in view you're not navigating view hierarchy, proxy of hierarchy, why syntax label odd 'statictexts' (the references @ bottom of post explain in more detail).

enter image description here

for #2 i'm inspecting structure defined in project. in unit test access importing @testable import projectname unfortunately approach doesn't work ui test.

instead, you'll have make sure source file want access ui test included target. can in xcode panel checking name of ui test:

enter image description here

more ui testing references:


Comments