Skip to content

Ensure unittest example MyTestCase can be run #126864

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

Open
samimakinen-twoday opened this issue Nov 15, 2024 · 2 comments
Open

Ensure unittest example MyTestCase can be run #126864

samimakinen-twoday opened this issue Nov 15, 2024 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@samimakinen-twoday
Copy link

Documentation

https://docs.python.org/3/library/unittest.html#organizing-test-code

In the examples test functions create instance of Widget class but there is no import for the class.
It is impossible to write unit tests without knowing how to import the modules you are about to test, so it would be nice to see how it is actually done.

@StanFromIreland
Copy link
Contributor

Do you mean there should be an import X? I don't really see it as too important here, we are not going to provide the source for the hypothetical module anyway.

@picnixz
Copy link
Member

picnixz commented Apr 20, 2025

I think there are some places where we could add dummy objects. For instance:

class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    @unittest.skipIf(mylib.__version__ < (1, 3),
                     "not supported in this library version")
    def test_format(self):
        # Tests that work for only a certain version of the library.
        pass

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass

    def test_maybe_skipped(self):
        if not external_resource_available():
            self.skipTest("external resource not available")
        # test code that depends on the external resource
        pass

However, we say "This is the output of running the example above in verbose mode:" which is incorrect. Instead, let's replace mylib.__version__ < (1,3) by another check, implement external_resource_available and import sys.

For the Widget test, I don't think we need to add a Widget implementation because it'd be too painful to write IMO. We also don't specify the output and don't say "if your run this, you get that". Note that we already had some E2E test with TestStringMethods but the paragraph using Widget is about organizing the code and focuses on setUp(), tearDown() and execution order.

@picnixz picnixz changed the title Module unittest examples cannot work Ensure unittest example MyTestCase can be run Apr 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
Status: Todo
Status: Todo
Development

No branches or pull requests

3 participants