Replies: 4 comments 3 replies
-
|
I was wondering the same and playing around with it I get crashes with the latest release and flaky test results when using the swift6 branch version. Sometimes I do get the correct mock instance in the test and sometimes it is a different one from the one that is @injected in the subject under test. Seems like the approach to register the mocks .cached and resetting the container before each test does not work in Swift Testing. |
Beta Was this translation helpful? Give feedback.
-
|
If you inject a specific container instance into your view models or services, then you can build and inject a separate container for each set of tests so that parallel testing works. If you access Container.shared or use one of the @injected property wrappers that depend on Container.shared, then your tests need to be serialized. This issue isn't specific to Factor or Resolver, and in fact impacts other scenarios like using a URLProtocol on URLSession.shared, or testing any code that depends on a global or statically shared variable. https://www.swiftwithvincent.com/blog/how-to-mock-any-network-call-with-urlprotocol |
Beta Was this translation helpful? Give feedback.
-
|
@hmlongco I have a proposal for solving this but it requires iOS 13 and macOS 10.15 minimum. struct SharedContainerTrait: TestTrait, SuiteTrait, TestScoping {
func provideScope(for test: Test, testCase: Test.Case?, performing function: () async throws -> Void) async throws {
try await Container.$shared.withValue(Container()) {
try await function()
}
}
}
extension Trait where Self == SharedContainerTrait {
static var factory: Self { Self() }
}Using the above trait I can write this test and they can run in parallel and use a different instance of the private extension Container {
var parallelTestDependency: Factory<Int> {
self { 0 }
}
}
@Suite
struct SwiftTestingParallelTests {
@Test(.factory)
func testWithOne() async throws {
Container.shared.parallelTestDependency.onTest {
1
}
#expect(Container.shared.parallelTestDependency() == 1)
}
@Test(.factory)
func testWithTwo() async throws {
Container.shared.parallelTestDependency.onTest {
2
}
#expect(Container.shared.parallelTestDependency() == 2)
}
@Test(.factory)
func testWithThree() async throws {
Container.shared.parallelTestDependency.onTest {
3
}
#expect(Container.shared.parallelTestDependency() == 3)
}
}What do you think about doing something like this? I have a branch where I tried this: |
Beta Was this translation helpful? Give feedback.
-
|
Factory now supports Swift Testing, including parallel testing. See; https://github.com/hmlongco/Factory?tab=readme-ov-file#xcode-16-testing As well as Testing page in documentation. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm wondering how injection is going to work with parallel tests in the new Swift testing framework.
If I have two different tests which want to inject different values to the same container object they can't really run in parallel
Beta Was this translation helpful? Give feedback.
All reactions