Cosmium.EmbeddedServer
0.0.8
dotnet add package Cosmium.EmbeddedServer --version 0.0.8
NuGet\Install-Package Cosmium.EmbeddedServer -Version 0.0.8
<PackageReference Include="Cosmium.EmbeddedServer" Version="0.0.8" />
<PackageVersion Include="Cosmium.EmbeddedServer" Version="0.0.8" />
<PackageReference Include="Cosmium.EmbeddedServer" />
paket add Cosmium.EmbeddedServer --version 0.0.8
#r "nuget: Cosmium.EmbeddedServer, 0.0.8"
#:package Cosmium.EmbeddedServer@0.0.8
#addin nuget:?package=Cosmium.EmbeddedServer&version=0.0.8
#tool nuget:?package=Cosmium.EmbeddedServer&version=0.0.8
Cosmium .NET – Effortless CosmosDB Emulation for Testing
Introduction
cosmium-dotnet
is a lightweight .NET library designed to make testing with CosmosDB effortless. It wraps and extends Cosmium, a CosmosDB emulator, enabling you to spin up an embedded, in-memory CosmosDB instance directly in your .NET applications and tests.
Motivation
When working on applications that use CosmosDB, running integration or unit tests against a real database can be impractical or cumbersome. While the original Cosmium project provides a great way to emulate CosmosDB, it still requires you to start and manage the emulator process externally - whether in CI pipelines or local environments.
cosmium-dotnet
solves this by embedding the emulator into your application with a single method call. This approach offers several key benefits:
- Simplified Test Setup: Start an in-memory CosmosDB emulator directly in your test code without any external processes.
- Practical for CI: No need to manage emulator processes during CI builds - Cosmium can run entirely in-memory.
- Faster Local Development: Run tests locally without relying on a real database or managing external dependencies.
By combining the powerful emulation capabilities of Cosmium with the simplicity of .NET, cosmium-dotnet
helps developers focus on testing, not infrastructure.
Getting Started
To start using cosmium-dotnet
, follow these steps:
Installation
Add the Cosmium.EmbeddedServer
NuGet package to your project:
dotnet add package Cosmium.EmbeddedServer
Quick Setup
Here’s a simple example to get you started with an in-memory CosmosDB emulator in your tests:
using Cosmium.EmbeddedServer;
// Initialize a new server instance
var serverInstance = new ServerInstance(
Guid.NewGuid().ToString(), // Unique server name
new ServerConfiguration { Port = 8082 } // If you're already running an emulator insance on 8081
);
// Create and configure the Cosmos client
var cosmosClient = (new CosmosClientBuilder(serverInstance.Endpoint, serverInstance.AccountKey))
.WithLimitToEndpoint(true)
.WithConnectionModeGateway() // Cosmium currently only supports connection mode 'Gateway'
.WithHttpClientFactory(() => new HttpClient(new HttpClientHandler()
{
// Since cosmium runs on a self-signed certificate, we need to bypass certificate validation
ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
}))
.Build();
// Use the client in your tests or application setup
// You might also want to create some databases and collections,
// The ServerInstance object exposes some useful methods for that:
var database = serverInstance.CreateDatabase("test-db");
database.CreateCollection("coll-1");
database.CreateCollection("coll-2");
In the real world you might want to make a base class for your integration tests that does this setup for you, you can take a look at Tests/Cosmium.EmbeddedServer.Tests/TestBase.cs for inspiration.
Custom json serialization
While inserting data into the emulator you might want to use a custom serializer, for that you can implement the interface IDocumentSerializer
and pass it to the constructor while creating the ServerInstance
.
Example Project
For a complete working example of how to use cosmium-dotnet
, check out the Todo App Example in the repository:
This project demonstrates how to:
- Integrate
cosmium-dotnet
into a simple TODO API project. - Write integration tests using the in-memory CosmosDB emulator.
Contributing and Development
This project uses native libraries to run the CosmosDB emulator, so it requires some additional setup to build and run the tests.
Prerequisites
- Create a
cosmium_dist
directory in the root of the repository. - Download the Cosmium native libraries (cosmium_<version>_shared-libraries.tar.gz) from the Cosmium releases page and place the .dll/.so/.dylib files in the
cosmium_dist
directory.
License
This project is MIT licensed.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- System.Text.Json (>= 8.0.5)
-
net8.0
- System.Text.Json (>= 8.0.5)
-
net9.0
- System.Text.Json (>= 9.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.0.8 | 188 | 3/16/2025 |
0.0.8-rc.3 | 137 | 3/13/2025 |
0.0.8-rc.2 | 183 | 3/12/2025 |
0.0.8-rc.1 | 172 | 3/12/2025 |
0.0.7 | 250 | 2/27/2025 |
0.0.6 | 241 | 2/9/2025 |
0.0.5 | 210 | 2/3/2025 |
0.0.4 | 213 | 1/27/2025 |
0.0.3 | 149 | 1/14/2025 |
0.0.2 | 158 | 1/9/2025 |
0.0.1 | 222 | 12/29/2024 |
0.0.1-alpha-3 | 162 | 12/20/2024 |
0.0.1-alpha-2 | 164 | 12/19/2024 |
0.0.1-alpha-1 | 186 | 12/19/2024 |