Skip to content

A Python package for easily spinning up SQL Server containers for testing. Simplify your testing workflow by seamlessly creating and managing isolated SQL Server instances.

snifhex/mssql-testcontainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mssql-testcontainer

Python package

The mssql-testcontainer package provides a simple and convenient way to spin up a SQL Server container for testing purposes. It utilizes the docker library and PyODBC to create and manage the container, allowing you to connect to the SQL Server instance within your Python tests.

Installation

You can install mssql-testcontainer using pip:

pip install mssql-testcontainer

Usage

The package exposes the SQLServerContainer class, which you can use as a context manager to automatically manage the lifecycle of the SQL Server container. Here's an example of how to use it:

Simple Usage with Context Manager

import sqlalchemy
from mssql_testcontainer import SQLServerContainer

with SqlServerContainer() as mssql:
    engine = sqlalchemy.create_engine(mssql.get_connection_url())
    with engine.begin() as connection:
        result = connection.execute(sqlalchemy.text("select @@VERSION"))
        print(result)

# The container will be automatically stopped and removed after the 'with' block

Usage with Pytest Fixtures

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from mssql_testcontainer import SQLServerContainer

@pytest.fixture(scope="session")
def session():
    with SQLServerContainer() as mssql:
        engine = create_engine(mssql.get_connection_url(), fast_executemany=True)
        YourModel.metadata.create_all(engine)
        Session = sessionmaker()
        Session.configure(bind=engine)
        with Session() as session:
            yield session

About

A Python package for easily spinning up SQL Server containers for testing. Simplify your testing workflow by seamlessly creating and managing isolated SQL Server instances.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages