Skip to content

[ci] [python-package] Python tests leave files behind #6361

@jameslamb

Description

Description

The Python unit tests in this project leave some files behind when they are done running.

They should be modified to use Python-managed temporary files that are automatically removed, so that:

  • successive test runs don't accidentally rely on outputs from previous runs
  • files aren't left behind on developers' local systems

Reproducible example

Build the Python package and run the Python tests.

cmake -B build -S .
cmake --build build --target _lightgbm
sh build-python.sh install --precompile
pytest tests/python_package_test

(for more details on this, see #6350).

Look at the files created.

git status --ignored

As of latest master (b27d81e), you'll see all of these created by tests:

categorical.model
lgb.model
lgb.pkl
lgb_train_data.bin
model.txt
Tree4.gv.pdf
Tree4.gv

Approach

Find the tests that created those files, and ensure that they stop creating them.

For example, it looks like lgb.model probably comes from here:

gbm.save_model("lgb.model")

And that that could be avoided using pytests's tmp_path fixture, like this:

def test_ranking_with_position_information_with_file(tmp_path):

str(tmp_path / "rank.train"),

For more on how that works, see "How to use temporary directories and files in tests" (pytest docs).

Additional Comments

You do not need to put up a pull request fixing all of these! Contributions that fix any of these would be welcomed.

This list will be updated as these are fixed:

If you are interested in working on this, comment here to indicate that and to ask for help if you need it.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions