Sharing WebApplicationFactory between test classes #2893
Replies: 1 comment
-
Unfortunately this expectation is incorrect. What you're describing is a collection fixture, where all the test classes in question share a collection. The fact that In essence, these two sets of declarations are the same w/rt the lifetime & ownership of the class fixture instances: public class BaseClass : IClassFixture<Foo> { }
public class TestClass1 : BaseClass { }
public class TestClass2 : BaseClass { } public class TestClass1 : IClassFixture<Foo> { }
public class TestClass2 : IClassFixture<Foo> { } In both cases, You should add Documentation for shared context: https://xunit.net/docs/shared-context It's worth pointing out, if you're thinking about trying to move the inheritance up to the test collection definition class, that all the same rules apply; that is, these two things are identical: public class BaseCollection : ICollectionFixture<Foo> { }
[CollectionDefinition(nameof(Collection1))]
public class Collection1 : BaseCollection { }
[CollectionDefinition(nameo(Collection2))]
public class Collection2 : BaseCollection { } [CollectionDefinition(nameof(Collection1))]
public class Collection1 : ICollectionFixture<Foo> { }
[CollectionDefinition(nameo(Collection2))]
public class Collection2 : ICollectionFixture<Foo> { } In both cases, Additionally, there is no feature for "hierarchy" in fixtures at the same level; that is, there's no such thing as a class fixture that's shared across multiple classes and/or as a dependency from another class fixture. The only way to "share" something is to move it up a layer: class fixtures can share an instance of a collection fixture (and in v3, collection fixtures can share an instance of an assembly fixture). |
Beta Was this translation helpful? Give feedback.
-
Hello XUnit community, I'm writing now integration tests and after adding second test class I've noticed that the lifecycle behavior isn't what I would expect it to be.
I use Testcontainers to spawn containerized dependencies, WebApplicationFactory as service provider and for all the heavy lifting and XUnit
IAsyncLifecycle
to control the lifespan of it.I've got CustomWebApplicationFactory with implemented
IAsyncLifecycle
:Also I have a collection definition:
My containers are just wrappers with some custom actions required to do some setup, but for major part they're just like this:
And an integration test base class:
And finally my test class:
Now what I would expect with such setup is that it should create single CustomWebApplicationFactory for all tests that inherit from IntegrationTestBase and reuse containers and IHost. What I get is that every test class spawns new containers and new WebApplicationFactory what is a complete waste of resources, given that I need to wait for ~2 minutes to have all required containers ready and really would like to have just a single instance of this whole setup for arbitrary amount of test classes.
What I've tried and failed:
[Collection(nameof(SharedTestInfrastructureCollection))]
attribute to the test class itself - obviously with no difference in behavior.Overall, maybe I move in completely wrong direction and should use something else instead to achieve the required behavior mentioned above. Would be very greatful for any suggestions.
Beta Was this translation helpful? Give feedback.
All reactions