Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

IMPORTANT: 0.6.0 is the final release. This package is now archived and no longer being developed.

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.

IMPORTANT: Retirement Roadmap

As of https://github.com/astropy/astropy/pull/14041 , this package is no longer used in astropy core library. We strongly advise any packages that still use this package to migrate away from it. When astropy 6.0 is released (tentatively Nov 2023), none of the active development branches of the core library would use this package anymore.

After that, we will just do one last release of pytest-openfiles and archive this repository.

To test for open files without this package, you can do this from the command line using the -W option:

pytest -W error::ResourceWarning

Alternately, you can also use pytest configuration file. The following example is for setup.cfg:

[tool:pytest]
filterwarnings =
    error::ResourceWarning

Also see https://docs.astropy.org/en/latest/development/testguide.html#testing-for-open-files on how to test for open files without this package when contributing to astropy.

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

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.

Release files for pytest-openfiles 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-openfiles 0.6.0
File Size Uploaded
pytest_openfiles-0.6.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-openfiles 0.6.0
File Interpreter ABI Platform
pytest_openfiles-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / pytest_openfiles-0.6.0.tar.gz

Download URL pytest_openfiles-0.6.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ff5160c34eaada82b983a2c316fcccb30e0094630e97784471f015956870a993
BLAKE2b-256 checksum
How to use checksums
6cc22f07cebdebfd53d11476ff77cfb3b709d16860dc1b2aac4903bb8ee0affe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / pytest_openfiles-0.6.0-py3-none-any.whl

Download URL pytest_openfiles-0.6.0-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
849b5b85644e31f2ce367c0fbbf53125b71a0a5d08500ddb08abcefc2dc3f99d
BLAKE2b-256 checksum
How to use checksums
41b272489d6f61ab3c72d6ce41835b31f27fa79450c5cfbe943a7a75e0badffd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page