How test parallelism affects logging and state
mainExpecto runs tests in parallel by default. This has two major implications:
- State Management: Avoid using global variables, global singletons, or mutating code in your tests. If you must use them, you must sequence your tests (using
--sequenced) or use locks. - Logging: Standard
printfnandConsole.Xfunctions are not thread-safe and will result in interleaved output. To log correctly within the test context, useExpecto.Logging.
Thread-safe logging example:
open Expecto.Logging
open Expecto.Logging.Message
let logger = Log.create "MyTests"
testCase "reading prop" <| fun () ->
let subject = MyComponent()
logger.info(
eventX "Has prop {property}"
>> setField "property" subject.property)
Expect.equal subject.property "Goodbye" "Should have goodbye as its property"