Skip to content

Postgres2Go - dotnet library to easily setup instance of PostgreSQL for integration tests

License

Notifications You must be signed in to change notification settings

bt-skyrise/Postgres2Go

Repository files navigation

 Postgres2Go



GitHub release Master build status Develop build status GitHub license

Stable nuget Downloads

Description

Inspired by Mongo2Go

Easily spin up PostgreSQL instances for integration tests. It is .NET library that makes easy to launch new instance of PostgreSQL server for duration of integration test suite. Each instance os PostgreSQL server run in an isolated envrionement side by side of another instance of PostgreSQL server. It does not require additional external dependencies. Allows test data access layer or application when state of database should be predictable.

It targets .NET Standard 2.0, .NET 4.6, .NET 4.7.

This Nuget package contains bundled version of PostgreSQL for Windows, Linux.

Installation

Postgres2Go can be installed

using nuget.exe and powershell:

Install-Package Postgres2Go

or using dotnet cli:

dotnet add package Postgres2Go

or using paket.exe:

paket add Postgres2Go

Usage

To start new instance of PostgreSQL server within integration test suite use PostgresRunner like this:

public class Program
{
    public static void Main(params string[] args) 
    {
        using(var runner = PostgresRunner.Start()) 
        using(var conn = new NpgsqlConnection(runner.GetConnectionString()))
        using(var cmd = new NpgsqlCommand("select version()", conn))
        {
            conn.Open();
            var version = cmd.ExecuteScalar() as string;

            Console.WriteLine($"PostgreSQL version: {version}");
        }
    }
}

PostgresRunner.Start() accepts parameters:

  • DataDirectory directory where new instance of PostgreSQL cluster will keep its data; if not provided then special directory will be created inside of TEMP folder
  • BinariesSearchPattern path part where PostgreSQL distribution should be located
  • Port TCP port used by PostgreSQL instance; if not provided then first free TCP port above 15433 will be used

When test come into tear down phase then execute method Dispose() to cleanup environment. This will remove database created for test and PostgreSQL cluster.

Other examples can be found under src/Postgres2Go.Samples

Credits

Copyright (c) 2017 Johannes Hoppe

Copyright (c) 2018 Skyrise

Special thanks to all Contributors

License

This software is distributed under MIT License. It contains third-party files located in the pg-dist folder that are distributed under PostgreSQL License