Skip to content

Latest commit

 

History

History
143 lines (102 loc) · 7.53 KB

README.md

File metadata and controls

143 lines (102 loc) · 7.53 KB

Microcks Testcontainers .NET

.NET library for Testcontainers that enables embedding Microcks into your unit tests with lightweight, throwaway instance thanks to containers.

GitHub Workflow Status Version License Project Chat Artifact HUB CNCF Landscape

Build Status

Latest released version is 0.1.0.

Current development version is 0.2.0.

Sonarcloud Quality metrics

Code Smells Reliability Rating Bugs Coverage Technical Debt Security Rating Maintainability Rating

Fossa license and security scans

FOSSA Status FOSSA Status FOSSA Status

OpenSSF best practices on Microcks core

CII Best Practices OpenSSF Scorecard

Community

To get involved with our community, please make sure you are familiar with the project's Code of Conduct.

How to use it?

Include it into your project dependencies

dotnet add package Microcks.Testcontainers --version 0.1.0

Startup the container

You just have to specify the container image you'd like to use. This library requires a Microcks uber distribution (with no MongoDB dependency).

MicrocksContainer container = new MicrocksBuilder()
	.WithImage("quay.io/microcks/microcks-uber:1.10.0")
	.Build();
await container.StartAsync();

Import content in Microcks

To use Microcks mocks or contract-testing features, you first need to import OpenAPI, Postman Collection, GraphQL or gRPC artifacts. Artifacts can be imported as main/Primary ones or as secondary ones. See Multi-artifacts support for details.

You can do it before starting the container using simple paths:

MicrocksContainer container = new MicrocksBuilder()
	  .WithMainArtifacts("apipastries-openapi.yaml")
	  .WithSecondaryArtifacts("apipastries-postman-collection.json")
	  .Build();
await container.StartAsync();

or once the container started using File arguments:

container.ImportAsMainArtifact("apipastries-openapi.yaml");
container.ImportAsSecondaryArtifact("apipastries-postman-collection.json");

You can also import full repository snapshots at once:

MicrocksContainer container = new MicrocksBuilder()
      .WithSnapshots("microcks-repository.json")
	  .Build();
await container.StartAsync();

Using mock endpoints for your dependencies

During your test setup, you'd probably need to retrieve mock endpoints provided by Microcks containers to setup your base API url calls. You can do it like this:

var baseApiUrl = container.GetRestMockEndpoint("API Pastries", "0.0.1");

The container provides methods for different supported API styles/protocols (Soap, GraphQL, gRPC,...).

The container also provides GetHttpEndpoint() for raw access to those API endpoints.

Launching new contract-tests

If you want to ensure that your application under test is conformant to an OpenAPI contract (or other type of contract), you can launch a Microcks contract/conformance test using the local server port you're actually running.

private int port;

public async Task InitializeAsync()
{
	container = new MicrocksBuilder()
		.WithExposedPort(port)
		.Build();
	await container.StartAsync();
}

[Fact]
public async Task testOpenAPIContract()
{
	var testRequest = new TestRequest
	{
		ServiceId = "API Pastries:0.0.1",
		RunnerType = TestRunnerType.OPEN_API_SCHEMA,
		TestEndpoint = $"http://host.testcontainers.internal:{port}",
		Timeout = TimeSpan.FromMilliseconds(2000)
	};
	TestResult testResult = await container.TestEndpointAsync(testRequest);

	testResult.Success.Should().BeTrue();
}

The TestResult gives you access to all details regarding success of failure on different test cases.