Skip to main content

Overview

ASTEROID: Automated, Solution for Testing Efficiently on Replicable, Operative, and Isolated Databases.

This pytest plugin is made for testing with MySQL docker images and is based on the great lovely-pytest-docker plugin by Lovely Systems.

pytest-asteroid extends the lovely-pytest-docker plugin by adding:

  • an availability check to make sure the MySQL image is ready for connection before running the database test suite.
  • a simple reset state functionality to handle state dependency issues between tests.

How do I get set up?

Dependencies

Make sure your system has Docker Engine installed and that the docker daemon is running before executing your tests.

Installation

Install pytest-asteroid using pip or poetry. We prefer to use poetry as it reduces the amount of files needed in the project and simplifies dependency management and virtual environments.

Install with poetry:

$ poetry add pytest-asteroid --dev

Examples of usage

In order to use ASTEROID make sure to have the following environmental variables set for the test DB docker image:

  • MYSQL_DATABASE
  • MYSQL_ROOT_PASSWORD

Using the get_docker_db_port fixture

# content from conftest.py

###############################################################################
# * Connection to test database
# This fixture uses the ASTEROID fixture get_docker_db_port which, on first
# session-scoped call, will envoke the docker_service startup.
###############################################################################
# Overwrite this fixture if a custom connection type is required
###############################################################################


# NOTE: Here we are using class fixture as we need the connection to close again
# after each test class run if we want to reset state.
# If attemting to reset state, while a connection is open,
# we will have a deadlock.
@pytest.fixture(scope="class")
def get_connection(get_docker_db_port):
    conn = pymysql.connect(
        database=os.environ["MYSQL_DATABASE"],
        port=get_docker_db_port("mysql_db", timeout=30.0),
        user="root",
        password=os.environ["MYSQL_ROOT_PASSWORD"],
        cursorclass=pymysql.cursors.DictCursor,
    )
    # Start a transaction and rollback to reset state after each test class execution
    yield conn
    conn.close()

Using the get_connection and reset_or_save_db_state fixtures

# content from test_state.py

class TestSaveState:
    # Test cases to check reset state works

    def test_connection_insert(self, get_connection, reset_or_save_db_state):
        reset_or_save_db_state("mysql_db", "superheroes")

        conn = get_connection
        # setup: insert new data
        with conn.cursor() as cur:
            cur.execute(
                """
                INSERT INTO superheroes (name, cape, height_cm, weigth_kg)
                VALUES('Dr. Strange', true, 188, 82)
                """
            )
            conn.commit()
            cur.execute("SELECT * FROM superheroes WHERE name = 'Dr. Strange'")
            rows = cur.fetchall()
            assert len(rows) == 1

Clarification

Use the examples in the project repository folder tests/ for inspiration on how to use pytest-asteroid and to see examples of test file structure.

Release files for pytest-asteroid 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-asteroid 0.4.1
File Size Uploaded
pytest-asteroid-0.4.1.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-asteroid 0.4.1
File Interpreter ABI Platform
pytest_asteroid-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.8 kB

Release files / pytest-asteroid-0.4.1.tar.gz

Download URL pytest-asteroid-0.4.1.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
83e042af7dbfc09a8708edfb7708be7cc0babfa4f54ce90b3d33c2c4488fa625
BLAKE2b-256 checksum
How to use checksums
614421c6dff7b207ac69ae18bc753143e8b18a7714d34125d08867fb438adac1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.2 Linux/5.15.0-1014-azure

Release files / pytest_asteroid-0.4.1-py3-none-any.whl

Download URL pytest_asteroid-0.4.1-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c1e587a942112a5193e40e808175911016d58a34a0bee46c4a689dce8e95b2fc
BLAKE2b-256 checksum
How to use checksums
f15760759474ac5d60e195124de0afba4538f7062040c21b7b2133e2b94ef9f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.14 CPython/3.10.2 Linux/5.15.0-1014-azure

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page