Skip to main content

Pytest plugin for detecting inadvertent open file handles

Project description

This package provides a plugin for the pytest framework that allows developers to detect whether any file handles or other file-like objects were inadvertently left open at the end of a unit test. It has been moved from the core astropy project since it is of use more generally.

Motivation

The pytest-openfiles plugin allows for the detection of open I/O resources at the end of unit tests. This is particularly useful for testing code that manipulates file handles or other I/O resources. It allows developers to ensure that this kind of code properly cleans up I/O resources when they are no longer needed.

Installation

The pytest-openfiles plugin can be installed using pip:

$ pip install pytest-openfiles

It is also possible to install the latest development version from the source repository:

$ git clone https://github.com/astropy/pytest-openfiles
$ cd pytest-openfiles
$ python ./setup.py install

In either case, the plugin will automatically be registered for use with pytest.

Usage

This plugin adds the --open-files option to the pytest command. When running tests with --open-files, if a file is opened during the course of a unit test but that file is not closed before the test finishes, the test will fail.

In some cases certain files are expected to remain open between tests. In order to prevent these files from causing tests to fail, they can be ignored using the configuration file variable open_files_ignore. This variable is added to the [tool:pytest] section of a package’s top-level setup.cfg file.

Files can be ignored using a fully specified filename:

[tool:pytest]
open_files_ignore = "/home/user/monty/output.log"

It is also possible to ignore files with a particular name regardless of where they reside in the file system:

[tool:pytest]
open_files_ignore = "output.log"

In this example, all files named output.log will be ignored if they are found to remain open after a test completes. Paths and filenames can include * and ? as wildcards:

[tool:pytest]
open_files_ignore = "*.ttf"

It is also possible to ignore open files for particular test cases by decorating them with the openfiles_ignore decorator:

import pytest

@pytest.mark.openfiles_ignore
def test_open_file_and_ignore():
    """We want to ignore this test when checking for open file handles."""

The test function will not be skipped, but any files that are left open by the test will be ignored by this plugin.

Development Status

Travis CI Status

Questions, bug reports, and feature requests can be submitted on github.

License

This plugin is licensed under a 3-clause BSD style license - see the LICENSE.rst file.

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-openfiles-0.5.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

pytest_openfiles-0.5.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest-openfiles-0.5.0.tar.gz.

File metadata

  • Download URL: pytest-openfiles-0.5.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for pytest-openfiles-0.5.0.tar.gz
Algorithm Hash digest
SHA256 179c2911d8aee3441fee051aba08e0d9b4dab61b829ae4811906d5c49a3b0a58
MD5 a3704c256f9343a37095328a5e552d22
BLAKE2b-256 1101c242971e175c4af014184325b773637f13fb1f01e4e02abd9f50dfa1062a

See more details on using hashes here.

File details

Details for the file pytest_openfiles-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_openfiles-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for pytest_openfiles-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b02389b5666d552e236ccf8d4e971abe97b320653ee77316de23db181dbe4f3a
MD5 f51e1aba402f8669ee79c55ca3752ee2
BLAKE2b-256 c585039b16aed2c8017033d96c8d35ded2e0b2d165b0fd7f38bfe04bb0b669a7

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