Git Product home page Git Product logo

pytest-datafixtures's Introduction

pytest-datafixtures

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 name.
  • datafix_readbin - Returns binary contents of a data fixture by 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 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)

Requirements

  • Python 3.6+

pytest-datafixtures's People

Contributors

idlesign avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.