SublimeText / UnitTesting

Testing Sublime Text Packages
MIT License
112 stars 32 forks source link

Enable warnings by default #271

Closed giampaolo closed 5 months ago

giampaolo commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

OK, this was easier than anticipated. Here's a PR (my second in this repo =)) which adds a new "warnings" settings: https://github.com/SublimeText/UnitTesting/pull/272

deathaxe commented 5 months ago

Fixed by #272