Luka
Luka

Reputation: 97

NET CORE - Unit Tests - CustomWebApplicationFactory

I'm creating unit tests using CustomWebApplicationFactory. My scenario is the following: Two tests classes (Test1 and Test2). Both classes are using CustomWebApplicationFactory pointing to a net core API startup.

If I run all tests from class Test1 all is ok. Same if I run all tests from class Test2. The problem comes if I run all tests at the same time. Tests crashed for different reasons. I guess that CustomWebApplicationFactory are sharing something beetween two tests classes when are running at the same time.

CustomWebApplicationFactory doesn't up two instances of the startup API. I'm not sure what it's happen but it seems that both test classes are sharing static fields or something similar.

Is it anyway to start two complety different instances? Or another way to do that?

My goal is to have multiple test classes but all pointing to the same API and runnig all tests a the same time without having errors between them.

Thanks

Upvotes: 0

Views: 774

Answers (1)

crgolden
crgolden

Reputation: 4634

I have faced the same issue multiple times with the WebApplicationFactory. I know you mention you would like to run all the tests at the same time, but the solution that has worked for me is to only run the tests sequentially.

You can add an AssemblyInfo.cs file to your integration tests project like:

<ProjectPath>\Properties\AssemblyInfo.cs

With this inside:

using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

This will force your tests to run sequentially and should clear the errors you see.

Upvotes: 4

Related Questions