Skip to main content

Data fixtures for pytest made simple.

Project description

pytest-datafixtures

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

PyPI - Version License 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
|   |-- mydatafixture.xml
|   |-- mydata.bin   
|-- test_basic.py
|
|-- subdirectory
|---- datafixtures
|     |-- payload.json
|     |-- cert.cer
|---- test_other.py

Requirements

  • Python 3.10+

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 name.
  • datafix_readbin - Returns binary contents of a data fixture by name.
  • datafix_dump - Allows data dumping for further usage.

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 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 binary contents of a data fixture by 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)

datafix_dump

Dump data for later usage as a datafixture:

def test_datafix_dump(datafix_dump):
    # Dump text
    dumped_filepath = datafix_dump('sometext', encoding='cp1251')
    
    # Dump binary
    dumped_filepath = datafix_dump(b'\x12')

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.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

pytest_datafixtures-1.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest_datafixtures-1.1.0.tar.gz.

File metadata

File hashes

Hashes for pytest_datafixtures-1.1.0.tar.gz
Algorithm Hash digest
SHA256 66d519bfb03e96b6b6f3f08e688adeb39b098ff3ad0578f4d42fc79408a909d5
MD5 b7919fdfc874aeb916d6e22c5a3ab21b
BLAKE2b-256 b30aa16756b9700d6e711929c0c2eb8bb5219005c632871c41140fff39971391

See more details on using hashes here.

File details

Details for the file pytest_datafixtures-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_datafixtures-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e552dccb1232a8953c21b78921e8bbef05cb5ab362858554ccdc5aab50ed71df
MD5 5350a61439c77034f93099fc0c849721
BLAKE2b-256 ed6f39d3be6ed63b92320c8d9b96871a103c889bb10a851b689878baeb952062

See more details on using hashes here.

Supported by

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