Enhanced.Testing.Component.GrpcClient
1.3.0
See the version list below for details.
dotnet add package Enhanced.Testing.Component.GrpcClient --version 1.3.0
NuGet\Install-Package Enhanced.Testing.Component.GrpcClient -Version 1.3.0
<PackageReference Include="Enhanced.Testing.Component.GrpcClient" Version="1.3.0" />
<PackageVersion Include="Enhanced.Testing.Component.GrpcClient" Version="1.3.0" />
<PackageReference Include="Enhanced.Testing.Component.GrpcClient" />
paket add Enhanced.Testing.Component.GrpcClient --version 1.3.0
#r "nuget: Enhanced.Testing.Component.GrpcClient, 1.3.0"
#:package Enhanced.Testing.Component.GrpcClient@1.3.0
#addin nuget:?package=Enhanced.Testing.Component.GrpcClient&version=1.3.0
#tool nuget:?package=Enhanced.Testing.Component.GrpcClient&version=1.3.0
Enhanced.Testing.Component
This solution provides a set of harnesses for integrating various external components into your .NET testing environment. It simplifies the process of setting up and tearing down external dependencies such as databases, message brokers, and other services during testing.
Harnesses
The solution includes harnesses for the following components:
- PostgreSQL: Facilitates the setup of a PostgreSQL database container for integration testing.
- Redis: Provides support for both Redis and RedisStack, allowing for easy integration of Redis services into your
tests.
- Kafka: A harness for setting up a Kafka broker for message-based integration testing.
- gRPC Client: Simplifies the process of testing gRPC services by providing a client harness.
- HTTP Client: Offers a harness for testing HTTP-based services and APIs.
Getting Started
To use these testing components in your project, follow these steps:
Add Dependencies: Ensure your project references the
Enhanced.Testing.Componentnamespace and its dependencies.Configure Test Fixtures: Use the provided harnesses in your test fixtures for setting up and tearing down external services. See the
SampleServiceFixtureclass for an example.Integration Testing: Write your integration tests using the configured fixtures to interact with real instances of your external dependencies.
Example Usage
Below is an example of how to set up a test fixture using the PostgreSQL harness:
public class MyServiceFixture : IAsyncLifetime
{
private readonly IComponent _component;
public MyServiceFixture()
{
HttpClient = new HttpClientHarness();
PostgreSql = new PostgreSqlHarness { ConnectionName = "PeopleDb" };
PeopleDb = new DbContextHarness<PeopleDbContext>();
PeopleDb.EnsureCreatedOnStart(builder => builder.UseNpgsql(PostgreSql.GetConnectionString()));
_component = ComponentBuilder.Create<Program>()
.AddHarness(HttpClient)
.AddHarness(PostgreSql)
.AddHarness(PeopleDb)
.Build();
}
public HttpClientHarness HttpClient { get; }
public PostgreSqlHarness PostgreSql { get; }
public DbContextHarness<PeopleDbContext> PeopleDb { get; }
public Task InitializeAsync() => _component.StartAsync(CancellationToken.None);
public Task DisposeAsync() => _component.StopAsync(CancellationToken.None);
}
Features
Real dependencies in Docker
PostgreSQL, Kafka and Redis run as real containers via Testcontainers. Set
ConnectionName on a container harness and its connection string is passed to the application under test as
ConnectionStrings:{ConnectionName}, so the app needs no test-specific configuration.
Managed lifecycle
StartAsync starts the harnesses in the order they were added, then the application. StopAsync stops the
application first, then the harnesses in reverse order, and removes their containers. Add a harness after the
harnesses it depends on (for example, DbContextHarness after PostgreSqlHarness).
Clients for the application under test
HttpClientHarness and GrpcClientHarness create clients that call the application in memory through TestServer,
without opening network ports.
Test data and assertions
DbContextHarness<TContext>creates the schema on start (EnsureCreatedOnStart) and runs queries against the application's database (ExecuteAsync).KafkaHarnesscreates topics on start (Topics).KafkaProducerHarnesssends messages to the application, andKafkaConsumerHarnessreads what it produced (SeekToEnd,ConsumeMessage).RedisDatabaseHarnessgives direct access to the Redis database (Database).
Configuring containers
Container harnesses let you override the image and customize the underlying Testcontainers builder:
var postgreSql = new PostgreSqlHarness
{
Image = "postgres:17",
ConfigureContainer = builder => builder
.WithEnvironment("TZ", "UTC")
.WithResourceMapping("init.sql", "/docker-entrypoint-initdb.d/")
};
Custom harnesses
Derive from Harness (override OnConfigure, OnStart, OnStop) to plug in any dependency, or from
ContainerHarness<TContainer> (override OnCreateContainer, GetConnectionString) for any Testcontainers module.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Enhanced.Testing.Component (>= 1.3.0)
- Grpc.Net.Client (>= 2.85.0)
-
net11.0
- Enhanced.Testing.Component (>= 1.3.0)
- Grpc.Net.Client (>= 2.85.0)
-
net8.0
- Enhanced.Testing.Component (>= 1.3.0)
- Grpc.Net.Client (>= 2.85.0)
-
net9.0
- Enhanced.Testing.Component (>= 1.3.0)
- Grpc.Net.Client (>= 2.85.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.