-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Various Test Class Examples #1094
Comments
Here's what I'm doing: https://github.com/viceroypenguin/VsaTemplate/tree/master/Web.Tests |
Not got to the point of testing my test application using a test web application, specific I will be testing a signal R hub, but here is how I have setup my This be can be cleaned up but has worked well for me so far. Here is a quick gist: https://gist.github.com/jacob7395/cd0095c825e8ce229d5265df1527eac9 Not cleaned out my types but should be able to make sense of it My working plan is to take the above and use a similar thing to spin up a test web application configuring my di in the same way. |
Here's what I'm doing with a test webapplicationfactory & testcontainers to spin up a database for each test session https://gist.github.com/dukesteen/e6aa4784e9164f145198a2f44be53196 @jacob7395 might be useful for you |
Thanks, I'll take a look in more detail when I start testing it with the web app hopefully next week 🤞 I have used test web application for NUnit will be interesting to see how the implementation differs with TUnit. |
Does anyone have an example of a single test project with multiple test bases / abstract classes that need containers? Currently, I've got an abstract |
You could have each container wrapped in its own class, and have that implement Also implement Inject it into your base class via Constructor injection, or property injection, with the attribute The initialize should run on the first test that uses that class, and the dispose on the last test that uses. If no test uses it, they shouldn't be called. Example of a web application factory is here: TUnit/TUnit.Example.WebProject.Tests/MyFactory.cs Lines 9 to 32 in 051c012
And how it is injected into a test base class: TUnit/TUnit.Example.WebProject.Tests/TestBase.cs Lines 5 to 6 in 2206806
Does that help? |
@thomhurst Perfect! For anyone wondering here's what a final state might look like (removed a bunch of my specific stuff). // Project = Tests.csproj
// FilePath = WebTestBase.cs
[ParallelLimiter<WebTestBaseParallelLimit>]
public abstract class WebTestBase
{
[ClassDataSource<WebTestBaseFactory>(Shared = SharedType.PerTestSession)]
public required WebTestBaseFactory TestBaseFactory { get; set; } = null!;
[Before(Test)]
public async Task BeforeAnyInheritedTests()
{
await TestBaseState.ResetDatabaseAsync();
// other per-test stuff that needs done every test within this base.
}
}
public class WebTestBaseFactory : WebApplicationFactory<Program>, IAsyncInitializer, IAsyncDisposable
{
private MsSqlContainer _database = null!;
public new async ValueTask DisposeAsync()
{
await _database.DisposeAsync();
await base.DisposeAsync();
}
public async Task InitializeAsync()
{
_database = await MsSqlContainerFactory.CreateAsync();
// This will be hit after the web host is configured so is safe.
var dbContext = Services.GetRequiredService<ApplicationDbContext>();
await dbContext.Database.MigrateAsync();
}
public async Task ResetDatabaseAsync() => // reset the database.
// Configure Web Host
}
public class WebTestBaseParallelLimit : IParallelLimit
{
public int Limit => 1;
} // Project = Tests.csproj
// FilePath = IntegrationTestBase.cs
[ParallelLimiter<IntegrationTestBaseParallelLimit>]
public abstract class IntegrationTestBase
{
[ClassDataSource<IntegrationTestBaseFactory>(Shared = SharedType.PerTestSession)]
public required IntegrationTestBaseFactory TestBaseFactory { get; set; } = null!;
[Before(Test)]
public async Task BeforeAnyInheritedTests()
{
await TestBaseState.ResetDatabaseAsync();
// other per-test stuff that needs done every test within this base.
}
}
public class IntegrationTestBaseFactory : IAsyncInitializer, IAsyncDisposable
{
private MsSqlContainer _database = null!;
public async ValueTask DisposeAsync()
{
await _database.DisposeAsync();
}
public async Task InitializeAsync()
{
_database = await MsSqlContainerFactory.CreateAsync();
}
public async Task ResetDatabaseAsync() => // reset the database.
}
public class IntegrationTestBaseParallelLimit : IParallelLimit
{
public int Limit => 1;
} |
Here's what I'm doing: |
is there a good way to use the lifecyclehook via
what i'm planing would be to create a database container with all migrations in the shared project that will than be used with project 1 and 2 |
Tests vary far and wide and can be vastly different from one another.
TUnit tries to be flexible in meeting different testing needs.
I'd find it really helpful to gather examples of test class examples and how you went about it.
For instance, testing an API against an in memory server. Did you inject in the server with a ClassDataSource and use an IAsyncInitializer? Or did you just statically set one up using a [Before(TestSession)] hook?
All of this would be useful, and we could create a section on the docs site dedicated to examples of how tests could be set up for particular scenarios.
The text was updated successfully, but these errors were encountered: