dj-nitehawk / MongoDB.Entities

A data access library for MongoDB with an elegant api, LINQ support and built-in entity relationship management
https://mongodb-entities.com
MIT License
543 stars 69 forks source link

Q: Parallel integration tests with separate database per test class/collection #209

Closed m-sokol-esky closed 1 year ago

m-sokol-esky commented 1 year ago

I'm trying to run multiple integration tests in parallel using xUnit and WebApplicationFactory. I've run into multiple issues connected with static classes.

  1. BsonClassMap - I resolved this one by putting a lock on the application initialization so that only one app is initialized at once
  2. DB - this one I can't figure out.

I want to have test isolation, so that each test class gets its own database. The problem is that a lot of things are cached in in the DB class using the entity type as a key. Obviously the entiy types are the same across the test classes, so it results in tests using each others databases. Is there any way around this?

dj-nitehawk commented 1 year ago

unfortunately parallel testing with db per test class is not possible. because it's not possible to associate a single entity type with more than one database.

db per test class is only doable with a collection fixture. something like this. but that means no parallel tests.

parallel testing with WAF is only achievable if your seed data doesn't step on each other. you can see an example of this being done here.

the library is going to need a complete rewrite/redesign to support db per test. imho, i don't see the value in that as i personally like to do integration testing against a single test database by overriding the db name via appsettings.json.