Apple Developer Documentation

Prepare initial state before tests run, and clean up resources after tests complete.

테스트가 실행되기전 초기화 구문을 준비하고 테스트가 완료 되면 리소스를 clean up

Overview

To consistently and reliably check that your code produces the correct results, tests need to start from a known, predictable state. In some cases, you need to set up state once for all the test methods in a test class; in others, you need to reset to a known state before each test method.

After each test method or test class completes, you might want to delete files that you don’t need, such as temporary files or screenshots. Or, to assist in failure diagnosis, you might want to capture the final state after a test.

Prepare your known state for tests, and clean up temporary files after tests using the methods on XCTest and XCTestCase.

Decide When to Set up and Tear Down State in Your Test Class

When you run a test case, XCTest calls the XCTestCase setUp() class method first. Use this method to set up state common to all the test methods in your test class.

XCTest then runs each test method, calling setup and teardown methods in this order:

  1. XCTest runs the setup methods once before each test method starts: setUp() async throws first, then setUpWithError(), then setUp(). Use these methods to prepare state that you need for each test method.
  2. XCTest runs the test method.
  3. XCTest runs the teardown blocks that you added in the test method, in last-in, first-out order. Use these blocks to tear down state and clean up resources after a specific test method.
  4. XCTest runs the teardown methods once after each test method completes, with tearDown() first, then tearDownWithError(), then tearDown() async throws. Use these methods to tear down state after each test method.

XCTest는 각 테스트 메서드가 시작되기 전에 설정 메서드를 한 번 실행합니다.

먼저 'setUp() synchronic trows'를 실행한 다음, 'setUp()을 실행한 다음, 'setUp()을 실행한다.

다음 방법을 사용하여 각 검사 방법에 필요한 상태를 준비합니다.