Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrapping in XCTestCase #77

Open
tanner0101 opened this issue Jun 7, 2019 · 9 comments
Open

bootstrapping in XCTestCase #77

tanner0101 opened this issue Jun 7, 2019 · 9 comments

Comments

@tanner0101
Copy link
Contributor

LoggingSystem.bootstrap must only be called once per process. One might assume to bootstrap the logger in XCTestCase.setUp, but this can be called multiple times and will result in an assertion.

To get around this, a global lazy variable can be used that is asserted in each setUp method. It will only be run once, the first time it is accessed, allowing for logging to be configured in XCTests:

Declare the global variable like so:

let isLoggingConfigured: Bool = {
    LoggingSystem.bootstrap { label in
        var handler = StreamLogHandler.standardOutput(label: label)
        handler.logLevel = .debug
        return handler
    }
    return true
}()

Then, in your XCTestCases, use like so:

import XCTest

final class FooTests: XCTestCase {
    override func setUp() {
        XCTAssert(isLoggingConfigured)
        ...
    }
}

We should consider documenting this or providing some sort of helper.

@ktoso
Copy link
Member

ktoso commented Jul 10, 2019

Sounds good to me, would you have a moment PR such section into the README.md perhaps?

@ktoso ktoso added kind/enhancement Improvements to existing feature. help wanted and removed kind/enhancement Improvements to existing feature. labels Jul 10, 2019
@maximkrouk
Copy link

But what if I want to test different LogHandlers with the LoggingSystem? Seems to be impossible for now 😢

@maximkrouk
Copy link

So I found a workaround in @testable import Logging, so LoggingSystem.internalBootstrap works and it seems to be a nice way to replace LoggingSystem's factory in tests 🌚

Btw you can find beta of my LoggingKit here 🙂

@weissi
Copy link
Member

weissi commented May 1, 2020

@maximkrouk you can construct a logger with a backend explicitly:

let logger = Logger(label: "foo") { label in
    MyAwesomeBackend(label)
}

see docs.

@leisurehound
Copy link
Contributor

Does this solution apply when using SwiftLog in an iOS app and its test cases? I'm finding the host app bootstraps the logging system fine, but then when loading tests I get the second bootstrapping error. I have attempted to use the lazy global variable approach above, but and finding its not protecting the bootstrapping from attempting again.

@weissi
Copy link
Member

weissi commented Jun 26, 2020

@leisurehound could you send us the code you use with the lazy global? That should work.

@leisurehound
Copy link
Contributor

sorry, I had an unrelated config issue that was causing this, once I fixed that the tests now run without double bootstrapping the logger. Sorry for the noise.

@ktoso
Copy link
Member

ktoso commented Jun 28, 2020

Thanks for looping back, closing the ticket then 👍

@ktoso ktoso closed this as completed Jun 28, 2020
@ktoso ktoso modified the milestone: 1.3.0 Jun 28, 2020
@ktoso
Copy link
Member

ktoso commented Jun 28, 2020

Actually, the original ticket stands as to documenting the pattern, so opening.

@ktoso ktoso reopened this Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants