Skip to main content

PyTest plugin for docker-based testing on database images

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-asteroid-0.4.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

pytest_asteroid-0.4.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest-asteroid-0.4.1.tar.gz.

File metadata

  • Download URL: pytest-asteroid-0.4.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.2 Linux/5.15.0-1014-azure

File hashes

Hashes for pytest-asteroid-0.4.1.tar.gz
Algorithm Hash digest
SHA256 83e042af7dbfc09a8708edfb7708be7cc0babfa4f54ce90b3d33c2c4488fa625
MD5 11ecc651e7c4340d480d5ddfe8d0a8e1
BLAKE2b-256 614421c6dff7b207ac69ae18bc753143e8b18a7714d34125d08867fb438adac1

See more details on using hashes here.

File details

Details for the file pytest_asteroid-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_asteroid-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.2 Linux/5.15.0-1014-azure

File hashes

Hashes for pytest_asteroid-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1e587a942112a5193e40e808175911016d58a34a0bee46c4a689dce8e95b2fc
MD5 9bc817219e8affa2f6daea177184d253
BLAKE2b-256 f15760759474ac5d60e195124de0afba4538f7062040c21b7b2133e2b94ef9f6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page