Skip to main content

Data fixtures for pytest made simple

Project description

https://github.com/idlesign/pytest-datafixtures

release lic ci coverage

Description

Data fixtures for pytest made simple

Offers fixtures for your tests to simplify data fixtures access. Makes use of Python’s native Path objects.

Data fixtures (files) expected to be stored in datafixtures directory next to your test modules:

tests
|-- datafixtures
|-- test_basic.py
|
|-- subdirectory
|---- datafixtures
|---- test_other.py

Fixtures

  • datafix_dir - Path object for data fixtures directory from the current test module’s directory.

  • datafix - Path object for a file in data fixtures directory with the same name as the current test function.

  • datafix_read - Returns text contents of a data fixture by it’s name.

  • datafix_readbin - Returns binary contents of a data fixture by it’s name.

datafix_dir

Access data fixtures directory:

def test_me(datafix_dir):

    # datafix_dir returns a Path object.
    assert datafix_dir.exists()

    # Gather data fixtures filenames.
    files = list(f'{file.name}' for file in datafix_dir.iterdir())

    # Read some fixture as text.
    # The same as using `datafix_read` fixture (see below).
    filecontent = (datafix_dir / 'expected.html').read_text()

    # Or read binary.
    filecontent = (datafix_dir / 'dumped.bin').read_bytes()

datafix

Access a data fixture with test name:

def test_me(datafix):
    # Read datafixtures/test_me.txt file
    filecontents = datafix.with_suffix('.txt').read_text()

datafix_read

Access text contents of a data fixture by it’s name:

def test_datafix_read(datafix_read):
    # Read datafixtures/expected.html file
    filecontents = datafix_read('expected.html')

    # Read encoded and represent as an StringIO object.
    encoded_io = datafix_read('test_datafix.txt', encoding='cp1251', io=True)

datafix_readbin

Access text contents of a data fixture by it’s name:

def test_datafix_read(datafix_readbin):
    # Read datafixtures/dumped.bin file
    binary = datafix_readbin('dumped.bin')

    # Read binary and represent as an BytesIO object.
    bin_io = datafix_readbin('dumped.bin', io=True)

Requirements

  • Python 3.6+

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-datafixtures-1.0.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

pytest_datafixtures-1.0.0-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-datafixtures-1.0.0.tar.gz.

File metadata

File hashes

Hashes for pytest-datafixtures-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4641235d8ff37cf6ee0d35755c7cc756ce383c682f5ee9e247ab1f6db7f6a474
MD5 eef1ed4085ca7df5f24e9c12c9ebbda7
BLAKE2b-256 d8c0400f14e1205aa754ed799c52b451c11788ac548fcf9a9721c91d16a6eb4e

See more details on using hashes here.

File details

Details for the file pytest_datafixtures-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_datafixtures-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3d10011a3409655cd7a4c1ff38b3664e64973bdad43eac76c03bc21d74826fc8
MD5 a033efa618089a522344265e9382208a
BLAKE2b-256 19d97bb07904ec32629c6976fe462b3b48e838cd72a1d7407edff7ca34187a4d

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 Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page