0

My test suite contains some test cases. I have some private functions where I will check elements existence. Consider I have three test cases:

func test_1() {
    ...
    checkListViewElements()
    ...
}

func test_2() {
    ...
}

func test_3() {
    ...
    checkListViewElements()
    ...
}

private func checkListViewElements() {
    //Checking existence
}

Since I consider each test case as independent, the private function checkListViewElements() may get repeat within the test cases.

Problem:

  • When I run the whole test suite, all the three test cases(test_1, test_2 and test_3) will be executed.
  • The private method checkListViewElements() will be called twice. This will result with the increased amount of test suite completion time.

What I wanted:

  • I have so many functions like checkListViewElements() within my code. I want them to run only once when I run the whole test suite. (Remember, for each test case, the application terminates and open freshly)

What I tried:

var tagForListViewElementsCheck = "firstTime" //Global variable

private func checkListViewElements() {
   if tagForListViewElementsCheck == "firstTime" {
      //Checking existence
      tagForListViewElementsCheck = "notFirstTime"
   }

   else {
      //Skip
   }
}
  • If I use local variables as tag, it works fine. But here, I have to create each tag for each private method. I really hated that.
  • I tried with dispatch_once and it seems not supported in Swift 4
  • Then I tried with static structs by referring this. It also does not seems working.

If there is any other nice approach to do it? Thanks in advance!

Confused
  • 3,846
  • 7
  • 45
  • 72

1 Answers1

0

You may use function func setUp(). Its call only once per suite. More info at What is the purpose of XCTestCase's setUp method?.

Kryštof Matěj
  • 462
  • 3
  • 16