Using CollectionFixture to Share a DatabaseFixture that requires a shared WebApplicationFactory? #2915
Replies: 3 comments
-
You can't use injection here. You have to create the instance of The reason is that we have very simplified lifetime support. We did not want to be in the business of resolving dependencies at the same level (i.e., collection fixture to collection fixture, class fixture to class fixture, etc.) because the rules can get out of hand quickly. You can only take dependencies on things from a wider scope. In v2, that means class fixtures can depend on collection fixtures; in v3, with the addition of assembly fixtures, that means class & collection fixtures can depend on assembly fixtures. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the confirmation Brad. |
Beta Was this translation helpful? Give feedback.
-
The initial solution I have implemented is to:
Main Collection to provide the WebApplicationFactory
Fixture uses the Injected WebApplicationFactory to initalize the test data
All tests use the static instance of the Database Fixture.
As I have quite a few separate test files, this change reduced the amount of data being saved to the Test Database by 90% (2000 rows down to 200 rows). If there is a better/simpler way to implement this, let me know. |
Beta Was this translation helpful? Give feedback.
-
For the integration tests in my project, I am recording my Test data in a Shared Test Database.
WebApplicationFactory is injected into my Database Fixtures, and is used to share an ApplicationDBContext between the Tests (amongst other things)
The Fixtures use the ApplicationDBContext to seed the Test Database with data, and the saved data is used by my tests.
I have multiple Database Fixtures so I can run a portion of the tests and only need to generate the required test data.
Many of my test classes use the same database fixture via IClassFixture.
This (as expected) causes the fixture constructor to run once for each test file.
As a result, the Test Data is recorded to the Database multiple times. I am looking to avoid this.
My current setup:
Main Collection to provide the WebApplicationFactory
My Fixtures then use the Injected WebApplicationFactory to
My Tests then utilize the Collection and Database Fixture, and retrieve the relevant test data for each test
As the above tests both use IClassFixture, the Fixture constructor is run twice, and the Test Data is generated twice.
I am looking to generate a shared SaveTestDatabaseFixture, so the test data is generated once, and the Test Files then share the fixture and data.
I have been attempting to do this by implementing SaveTestDatabaseFixture as part of a collection fixture, but I have been unable to identify if I can provide SaveTestDatabaseFixture with the required WebApplicationFactory, which by my understanding is being injected via the "Tests" fixture collection above.
If I try to do something like this, I get the following error.
From my understanding, this is because I am not Injecting the WebApplicationFactory into SaveTestDatabaseFixture.
Is it possible to define SaveTestDatabaseFixture as a Collection Fixture, and have WebApplicationFactory injected into it?
If not, are there any alternative methods to allow SaveTestDatabaseFixture to be generated once, shared between multiple tests, while having WebApplicationFactory injected into it?
If any more information is required, please let me know.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions