Multiple asynchronous tests and expectations

I have several tests, and each test tests the same asynchronous method for different results with the given parameters.

I found out that for asynchronous tests, we have to declare a wait, wait for a wait, and complete a wait. It is perfectly. Each test works correctly when done separately, but when I try to run the entire test class, some tests pass, while others fail or crash when they start and pass normally.

I was browsing online for the “fast 3 tests with expectation,” and anyone who explains expectations has only one example in one testing method. Is it impossible to expect expectations in multiple methods in the same class?

An example test is as follows:

func testLoginWrongUsernameOrPasswordFailure() {
  let viewModel = LoginViewModel()
  let loginAPI = APIManager()
  let expect = expectation(description: "testing for incorrect credentials")

  viewModel.loginWith(username: "qwerty", password: "qwerty", completion: { loginCompletion in

      do {
        try loginCompletion()
          XCTFail("Wrong Login didn't error")
          expect.fulfill()
        } catch let error {
          XCTAssertEqual(error as? LoginError, LoginError.wrongCredentials)
          expect.fulfill()
        }
      })

      waitForExpectations(timeout: 10) { error in
        XCTAssertNil(error)
      }
}

, ,

MCVE https://bitbucket.org/chirone/mcve_test API-, , .

API, , .

, , APIManager.

, , , , , SIGABRT :

XCTAssertEqual: ( " (MCVE.LoginError.wrongCredentials)" ) ( " (MCVE.LoginError.emptyFields)" ) -

*** - [XCTest Expectation ],/Library/Caches/com.apple.xbs/Sources/XCTest_Sim/XCTest-12124/Sources/XCTestFramework/Async/XCTestExpectation.m:101

*** - "NSInternalInconsistencyException", : " API - , [XCTest Expectation] .

SIGABRT , , XCTest, , .

, MCVE .

+4
1

? . XCTestCase, .

func wait(for: [XCTestExpectation], timeout: TimeInterval)

, , , for:.

. , Apple XCode- > Window- > Documentation and API Reference, XCTestCase.

+1

Source: https://habr.com/ru/post/1678188/


All Articles