From dcbfe9f842f7375c592c799b89fafbcffe8b2cf7 Mon Sep 17 00:00:00 2001 From: Shane Maloney Date: Mon, 8 Apr 2024 10:03:52 +0100 Subject: [PATCH] Fix test issue where warning was being raised but not captured (#151) * Fix test issue where warning was being raised but not captured * Switch to recwarn instead of pytest.warn * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update parfive/tests/test_downloader.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update parfive/tests/test_downloader.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Stuart Mumford --- parfive/tests/test_downloader.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/parfive/tests/test_downloader.py b/parfive/tests/test_downloader.py index a34ab40..3cfd255 100644 --- a/parfive/tests/test_downloader.py +++ b/parfive/tests/test_downloader.py @@ -578,11 +578,14 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def run(self): - self.result = self._target(*self._args, **self._kwargs) + try: + self.result = self._target(*self._args, **self._kwargs) + finally: + del self._target, self._args, self._kwargs @skip_windows -def test_download_out_of_main_thread(httpserver, tmpdir): +def test_download_out_of_main_thread(httpserver, tmpdir, recwarn): tmpdir = str(tmpdir) httpserver.serve_content( "SIMPLE = T", headers={"Content-Disposition": "attachment; filename=testfile.fits"} @@ -593,11 +596,16 @@ def test_download_out_of_main_thread(httpserver, tmpdir): thread = CustomThread(target=dl.download) thread.start() - - with pytest.warns( - UserWarning, - match="This download has been started in a thread which is not the main thread. You will not be able to interrupt the download.", - ): - thread.join() + thread.join() validate_test_file(thread.result) + + # We use recwarn here as for some reason pytest.warns did not reliably pickup this warning. + assert len(recwarn) > 0 + assert any( + [ + "This download has been started in a thread which is not the main thread. You will not be able to interrupt the download." + == w.message.args[0] + for w in recwarn + ] + )