Skip to main content

This project helps you to vacate specific file path or deploy resource file into specific file path when unit testing.

Project description

Fixture File Handler

Test Test Coverage Maintainability Code Climate technical debt Updates PyPI - Python Version PyPI - Downloads Twitter URL

This project helps you to vacate specific file path or deploy resource file into specific file path when unit testing.

Context

The most popular setup / teardown tasks about file system on unit testing is almost 2 kinds.

  1. vacate specific file path for testing file export function
  2. deploy fixture file / directory into specific file path for testing file import /export function

Then we have to think about how to back up existing file / directory between unit testing because maybe developer wants to keep those handwritten files for development.

Fixture File Handler is framework to realize simply implement the vacate and deploy actions while keeping the existing files. Of course, even if there is no file in the target path, it works fine.

Ubiquitous Language

target

The target file path to vacate or deploy file / directory for unit testing.

backup

The file path to back up existing file / directory on target file path between unit testing.

resource

The file / directory you want to deploy and let product code read / write in unit testing. It may test resource file or template file like *.dist file.

Basic behavior

Vacator

target path backup path
existing file /dir  

↓ setup

target path backup path
  existing file / dir

↓ teardown

target path backup path
existing file / dir  

Deployer

target path backup path resource path
existing file / dir   resource file / dir

↓ setup

target path backup path resource path
resource file / dir existing file /dir resource file / dir

↓ teardown

target path backup path resource path
existing file / dir   resource file / dir

Common behavior

If file / directory already exists on backup path, setup raise BackupAlreadyExistError because it's unexpected situation and developer may want to resque those backup files.

Quickstart

1. Install

pip install fixturefilehandler

2-1. Case when unittest: implement setUp() and doCleanups()

from pathlib import Path
import unittest

from fixturefilehandler.factories import VacatorFactory
from fixturefilehandler.file_paths import RelativeVacateFilePath

VACATOR = VacatorFactory.create(
    RelativeVacateFilePath(
        Path('test.txt'),
        Path('test.txt.bak'),
        Path(__file__).parent
    )
)


class ConfigurableTestCase(unittest.TestCase):
    def setUp(self):
        VACATOR.setup()

    def doCleanups(self):
        VACATOR.teardown()

2-2. Case when pytest: implement fixture

from pathlib import Path
import pytest

from fixturefilehandler.factories import DeployerFactory
from fixturefilehandler.file_paths import RelativeDeployFilePath

DEPLOYER = DeployerFactory.create(
    RelativeDeployFilePath(
        Path('test.txt'),
        Path('test.txt.bak'),
        Path('testresources/test.txt.dist'),
        Path(__file__).parent
    )
)


@pytest.fixture
def fixture_file():
    DEPLOYER.setup()
    yield DEPLOYER.FILE_PATH
    DEPLOYER.teardown()


def test_something(fixture_file):
    """test something"""

API

file_paths

SimpleVacateFilePath

This instance holds path to target and backup. Each path is independent each other.

SimpleDeployFilePath

This instance holds path to target, backup, and resource. Each path is independent each other.

RelativeVacateFilePath

This instance holds path to target, backup, and base. Each path is relative based on base path.

RelativeDeployFilePath

This instance holds path to target, backup, resource, and base. Each path is relative based on base path.

How do I...

Use different paths for each test?

setup() and teardown() also accept file_paths argument.

Case when unittest:

from pathlib import Path

import unittest

from fixturefilehandler import ResourceFileDeployer
from fixturefilehandler.file_paths import RelativeDeployFilePath


class AdvancedConfigurableTestCase(unittest.TestCase):
    @property
    def file_path(self) -> RelativeDeployFilePath:
        return RelativeDeployFilePath(
            Path('test.txt'),
            Path('test.txt.bak'),
            Path(f'testresources/{self._testMethodName}.txt'),
            Path(__file__).parent
        )

    def setUp(self):
        ResourceFileDeployer.setup(self.file_path)

    def doCleanups(self):
        ResourceFileDeployer.teardown(self.file_path)

Case when pytest:

from pathlib import Path

import pytest

from fixturefilehandler import ResourceFileDeployer
from fixturefilehandler.file_paths import RelativeDeployFilePath


@pytest.fixture
def fixture_file_advanced(request):
    file_path = RelativeDeployFilePath(
        Path('test.txt'),
        Path('test.txt.bak'),
        Path(f'testresources/{request.node.name}.txt'),
        Path(__file__).parent
    )

    ResourceFileDeployer.setup(file_path)
    yield file_path
    ResourceFileDeployer.teardown(file_path)


def test_something(fixture_file_advanced):
    """test something"""

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

fixturefilehandler-1.4.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

fixturefilehandler-1.4.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file fixturefilehandler-1.4.0.tar.gz.

File metadata

  • Download URL: fixturefilehandler-1.4.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for fixturefilehandler-1.4.0.tar.gz
Algorithm Hash digest
SHA256 86e4650bb489a68c3ccb698f7a51ac6bff797dd22828237a0654c94481a4037f
MD5 9389309be3ec0b1907c3692d101580c8
BLAKE2b-256 819423b2a164d7d996a9269839a31d719fe16146358d1e628d05cda1876b55fb

See more details on using hashes here.

File details

Details for the file fixturefilehandler-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: fixturefilehandler-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for fixturefilehandler-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e383cc2b04e6c6975bea2f675eeeccf43987157a787ac62a3578a9263be744b0
MD5 91ac7c0ef9f2a3773a1e69862541b712
BLAKE2b-256 0ffa47c75a34fff2fadd09cab217673ae2c8e29b072f096c005b45dcbe0e3a44

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