Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable declaration inside of class in Swift

Tags:

swift

xctest

I'm new in swift automated testing with XCTest framework. In my work I faced with correct arrangement some parts of code inside of test Please explain the difference between variable declaration in the beginning of the class and in the certain function. Which way is preferred?

Var.1

class someClass: XCTestCase {
   let app = XCUIApplication()

   func someFunc {
       print (app.debugdescription)
   }
}

Var. 2

class someClass: XCTestCase { 

 func someFunc {
       let app = XCUIApplication()
       print (app.debugdescription)
   }
}
like image 704
Andrius Shiaulis Avatar asked Nov 08 '25 00:11

Andrius Shiaulis


1 Answers

Var. 1 gives you the ability to configure your XCUIApplication in setup() and use it in the tests. Like this:

class someClass: XCTestCase {
   var app: XCUIApplication!

   func setUp {
      app = XCUIApplication()
      app.launchArguments.append("launchArgument")
      setupSnapshot(app)
      app.launch()
   }

   func test1 {
      XCTAssertNotNil(app)
   }

   func test2 {
      XCTAssertNotNil(app)
   }
}

With Var. 2 you can setup your app differently in each test. Like this:

class someClass: XCTestCase {

   func test1 {          
      let app = XCUIApplication()
      app.launchArguments.append("withTestUser")
      app.launch()

      XCTAssertNotNil(app)
   }

   func test2 {          
      let app = XCUIApplication()
      app.launchArguments.append("withNoData")
      app.launch()

      XCTAssertNotNil(app)
   }
}
like image 62
Daniel Avatar answered Nov 09 '25 14:11

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!