How can I launch WireMock for XCTest UI tests under Xcode 10 and macOS 10.14?

RehcsifMit

We are using a standalone wiremock instance as a mock server for our Xcode UI tests. We have an test observer class which is responsible for spinning up this instance (if required) and tearing it down upon completion of the test run. The code for the observer is as follows:

import AppKit
import XCTest
import WiremockClient

class SSUITestObserver: NSObject, XCTestObservation {

    enum TestObserverError : Error {
        case MockServerStartupError(String)
    }

    lazy var testBundleURL: URL = Bundle(for: SSUITestCase.self).bundleURL
    lazy var testBundleBinURL: URL = self.testBundleURL.appendingPathComponent("..", isDirectory: true)
    lazy var mockServerHomeURL: URL = self.testBundleURL.appendingPathComponent("Contents/Resources/", isDirectory: true)
    lazy var mockServerJarURL: URL = self.mockServerHomeURL.appendingPathComponent("wiremock-standalone-2.18.0.jar", isDirectory: false)

    override init() {
        super.init()

        NSLog("UI Test Observer Initialized")
        XCTestObservationCenter.shared.addTestObserver(self)
    }

    func testBundleWillStart(_ testBundle: Bundle) {
        NSLog("***Test Bundle starting")
        do {
            // Start the Wiremock server
            try ensureMockServerIsRunning()
        } catch {
            fatalError("\n Failed during test bundle setup: \(error)\n")
        }


    }

    public func testBundleDidFinish(_ testBundle: Bundle) {
        NSLog("***Test Bundle completed")
        stopMockServer()
    }

    func ensureMockServerIsRunning() throws {
        WiremockClient.baseURL = SSUIIntegrationTestCase.mockServerAddress

        guard !WiremockClient.isServerRunning() else { return }

        let args = ["-jar",
                    self.mockServerJarURL.path,
                    "--port", "3000",
                    "--root-dir", self.mockServerHomeURL.path]

        _ = Process.launchedProcess(launchPath: "/usr/bin/java", arguments: args)

        for _ in 1...9 {
            if WiremockClient.isServerRunning() { return }
            sleep(1)
        }

        throw TestObserverError.MockServerStartupError("Error staring up the mock server instance!")
    }

    func stopMockServer() {
        WiremockClient.shutdownServer()
    }

    func resetMockServerStubs() {
        WiremockClient.reset()
    }
}

All was well until I moved to macOS 10.14. Formerly, we were not code signing the UITest target. Upon moving to 10.14, running tests now fails with a bootstrap error before the tests even begin to run. I discovered that turning on automatic code-signing for on the tests gets around this problem.

ただし、これにより2番目の問題が発生します。launchedProcess上記行で、ワイヤーモックサーバーを起動しようとすると失敗しjava.lang.RuntimeException: java.net.SocketException: Operation not permittedます。テストを実行する前にサーバーを(コマンドラインなどで)起動すると、すべて正常に動作します。

では、どうすればこのキャッチ22から抜け出すことができますか?10.13ではすべてがうまく機能しました。コード署名がモックサーバーの起動と何の関係があるのか​​は私にはわかりません。

RehcsifWith

質問に対する直接の回答は得られませんでしたが、回避策は見つかりました。TestObserverを使用してWireMockサーバーを起動する代わりに、テストで事前アクションを使用できます。

これを行うには、UIテストプロジェクトのスキームを編集します。

スキームの編集

  1. 横にある開示矢印をクリックしてからTest、をクリックしますPre-actions
  2. をクリックし+てアクションスクリプトを追加します
  3. Use the shell of your choice (I kept the default, /bin/sh)
  4. Under Provide build settings from, choose your test target
  5. Enter a script that spins up your mock server.

For the script, I used the following code:

exec > /tmp/preaction-log.txt 2>&1

# Attempt to connect to an existing wiremock server, and exit if we succeed:
curl http://localhost:3000/__admin/mappings > /dev/null 2>&1 && exit 0 || echo "Attemmpting to spin up a Wiremock Server:"

# No existing server, so spin one up:
WIREMOCK_DIR=${BUILT_PRODUCTS_DIR}/${EXECUTABLE_NAME}-Runner.app/Contents/PlugIns/${TARGET_NAME}.xctest/Contents/Resources/

/usr/bin/java -jar "${WIREMOCK_DIR}"wiremock-standalone-2.18.0.jar --port 3000 --root-dir "${WIREMOCK_PATH}" &

Here's what the script does:

  1. Logs all output to /tmp/preaction-log.txt, for debugging, as pre-action scripts do not get logged to the build log.
  2. Checks for a running server on the desired port (3000 in our case). I used the same method WireMockClient uses: attempting to access the "mappings" API. If this succeeds, we exit the script.
  3. If we get to this point we can assume we need to spin up a server. So we do, using some XCode environment variables to point to where we store the server executable within our project (if you always have the WireMock client installed on your system(s) you can change the path to whatever you like).

これで、テストケースを実行するたびにこのスクリプトが実行されます。TestObserver内での実行の最後に、サーバーをシャットダウンします。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I open the output of a backround process in terminal under Windows 10?

分類Dev

How do I switch on Device Encryption under Windows 10 Home?

分類Dev

How can I perform a "swipe left" action via UI tests

分類Dev

How do I show the code folding ribbon in Xcode 10?

分類Dev

Can I upload Xcode builds on macOS 10.12

分類Dev

How to install libcurl under MacOS10.12 and use for Xcode?

分類Dev

How can I launch applications from 2 ttys on launch?

分類Dev

How can I unblock the installation of a program in Windows 10

分類Dev

How can i subscribe data with forkJoin in angular 10?

分類Dev

How can I turn off tablet mode Windows 10?

分類Dev

How can I reinstall Windows 10's calculator app?

分類Dev

How can I hide the clock from Windows 10 taskbar

分類Dev

How i can reset keyboard to their original defaults in Windows 10?

分類Dev

How can I launch Grub-Customizer?

分類Dev

How to get the Quick Help back in Xcode 10?

分類Dev

Xcode UI tests - adding tests to test classes

分類Dev

Xcode 10 - Can't Build React Native Application

分類Dev

macOS 10.14(beta)Command_Line_Tools_macOS_10.14_for_Xcode_10_Betaのインストール方法

分類Dev

Invalid architectures Xcode 10

分類Dev

Xcode 10 new update?

分類Dev

how can I debug go tests with gdb

分類Dev

How can I launch this PowerShell script in a new hidden window

分類Dev

How can I script genymotion emulator to launch a given avd, headless?

分類Dev

How can I launch a specific application bypassing the global menu in Unity?

分類Dev

How can I launch a specific application bypassing the global menu in Unity?

分類Dev

Launch Windows 10 Settings using JavaScript?

分類Dev

Windows10 - Explorer crashes on Application launch

分類Dev

Windows 10 UWP apps launch and then disappear immediately

分類Dev

How can I move an img under background

Related 関連記事

  1. 1

    How can I open the output of a backround process in terminal under Windows 10?

  2. 2

    How do I switch on Device Encryption under Windows 10 Home?

  3. 3

    How can I perform a "swipe left" action via UI tests

  4. 4

    How do I show the code folding ribbon in Xcode 10?

  5. 5

    Can I upload Xcode builds on macOS 10.12

  6. 6

    How to install libcurl under MacOS10.12 and use for Xcode?

  7. 7

    How can I launch applications from 2 ttys on launch?

  8. 8

    How can I unblock the installation of a program in Windows 10

  9. 9

    How can i subscribe data with forkJoin in angular 10?

  10. 10

    How can I turn off tablet mode Windows 10?

  11. 11

    How can I reinstall Windows 10's calculator app?

  12. 12

    How can I hide the clock from Windows 10 taskbar

  13. 13

    How i can reset keyboard to their original defaults in Windows 10?

  14. 14

    How can I launch Grub-Customizer?

  15. 15

    How to get the Quick Help back in Xcode 10?

  16. 16

    Xcode UI tests - adding tests to test classes

  17. 17

    Xcode 10 - Can't Build React Native Application

  18. 18

    macOS 10.14(beta)Command_Line_Tools_macOS_10.14_for_Xcode_10_Betaのインストール方法

  19. 19

    Invalid architectures Xcode 10

  20. 20

    Xcode 10 new update?

  21. 21

    how can I debug go tests with gdb

  22. 22

    How can I launch this PowerShell script in a new hidden window

  23. 23

    How can I script genymotion emulator to launch a given avd, headless?

  24. 24

    How can I launch a specific application bypassing the global menu in Unity?

  25. 25

    How can I launch a specific application bypassing the global menu in Unity?

  26. 26

    Launch Windows 10 Settings using JavaScript?

  27. 27

    Windows10 - Explorer crashes on Application launch

  28. 28

    Windows 10 UWP apps launch and then disappear immediately

  29. 29

    How can I move an img under background

ホットタグ

アーカイブ