Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable warnings by default #271

Closed
giampaolo opened this issue May 31, 2024 · 3 comments
Closed

Enable warnings by default #271

giampaolo opened this issue May 31, 2024 · 3 comments

Comments

@giampaolo
Copy link
Contributor

giampaolo commented May 31, 2024

Python unittest module automatically enables warnings before starting the test suite:

UnitTesting package should do the same. This is important especially for ResourceWarnings. E.g. with warnings enabled the code below:

class TestSomething(unittesting.DeferrableTestCase):
    def test_it(self):
         open(__file__)

...will print a warning like this in the console:

User.tests.test_file.TestSomething.test_it ... /home/giampaolo/.config/sublime-text/Packages/User/tests/test_file.py:66: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/giampaolo/.config/sublime-text/Packages/User/tests/test_file.py' mode='r' encoding='UTF-8'>
  open(__file__)
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Official Python doc states https://docs.python.org/3/library/warnings.html#overriding-the-default-filter:

Developers of test runners for Python code are advised to instead ensure that all warnings are displayed by default for the code under test, using code like:

import sys

if not sys.warnoptions:
    import os, warnings
    warnings.simplefilter("default") # Change the filter in this process
    os.environ["PYTHONWARNINGS"] = "default" # Also affect subprocesses
@giampaolo
Copy link
Contributor Author

giampaolo commented May 31, 2024

For the record, at the moment I'm using this as a workaround in my own unit tests:

import warnings
import unittesting

class SublimeTestCase(unittesting.DeferrableTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        warnings.simplefilter("error")  # for this process
        os.environ["PYTHONWARNINGS"] = "default"  # also for subprocesses

    @classmethod
    def tearDownClass(cls):
        warnings.resetwarnings()
        del os.environ["PYTHONWARNINGS"]
        super().tearDownClass()

@giampaolo
Copy link
Contributor Author

OK, this was easier than anticipated. Here's a PR (my second in this repo =)) which adds a new "warnings" settings:
#272

@deathaxe
Copy link
Member

deathaxe commented Jun 2, 2024

Fixed by #272

@deathaxe deathaxe closed this as completed Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants