Skip to content

[python-package] [R-package] every test catching an error should check for a specific error message #6860

@jameslamb

Description

@jameslamb

Description

Unit tests that assert that an error occurs should check that a specific error is raised.

Like this in Python:

https://github.com/microsoft/LightGBM/blob/6437645c4a0c17046be59e4f57d09952e2e0185f/tests/python_package_test/test_engine.py#L782-L783

And this in R:

https://github.com/microsoft/LightGBM/blob/6437645c4a0c17046be59e4f57d09952e2e0185f/R-package/tests/testthat/test_basic.R#L209-L215

Benefits of this work

  • prevents tests from accidentally passing in the presence of an unrelated and unexpected error

Acceptance criteria

  • R: every use of expect_error() and expect_warning() in tests should pass argument regexp to catch a specific error/warning
  • Python: every use of pytest.raises() and pytest.warns() in tests should pass argument match to catch a specific error/warning, and should catch a more specific exception type than "Exception"

Approach

Do not try to fix all of these at once. Pull requests only changing 1 or a small number at a time are preferred.

Python:

R

Notes

How to run the Python tests:

# build lib_lightgbm (just do this once)
cmake -B build -S .
cmake --build build --target _lightgbm -j4

# install Python package (do this every time you change Python code in the library)
sh build-python.sh install --precompile

# run tests
python tests/test_python_package

How to run the R tests:

sh build-cran-package.sh --no-build-vignettes
R CMD INSTALL ./lightgbm*.tar.gz
cd ./R-package/tests
Rscript testthat.R

For other questions on how to set up your development environment, see #6350 (and post questions there about the development process).

Metadata

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