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

Use unittest.skipIf decorator for skipping certain test functions. #1264

Merged
merged 2 commits into from
Jun 6, 2020

Conversation

rodrigc
Copy link
Contributor

@rodrigc rodrigc commented May 7, 2020

https://twistedmatrix.com/trac/ticket/9812

This reduces errors reported by mypy of the form:

src/twisted/words/test/test_domish.py:193:9: error: "Callable[[ElementTests], Any]" has no attribute "skip"  [attr-defined]
            test_addContentBytes.skip = (

@rodrigc rodrigc force-pushed the 9812-rodrigc-mypy branch 4 times, most recently from b73b52a to a9c60b2 Compare May 7, 2020 22:56
@rodrigc rodrigc closed this May 8, 2020
@rodrigc rodrigc reopened this May 8, 2020
@rodrigc rodrigc force-pushed the 9812-rodrigc-mypy branch 5 times, most recently from a2795d1 to 00228c5 Compare May 24, 2020 18:00
@rodrigc rodrigc requested a review from a team May 26, 2020 03:51
@rodrigc rodrigc force-pushed the 9812-rodrigc-mypy branch 6 times, most recently from 082f9af to 8619e4d Compare June 3, 2020 01:39
@rodrigc rodrigc requested a review from wiml June 4, 2020 06:04
@rodrigc
Copy link
Contributor Author

rodrigc commented Jun 4, 2020

@wiml Could you take a look at this?

Copy link
Contributor

@wiml wiml left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whew, that's a lot of files! (Might have been easier to split it into one PR for all of the import-style changes and one for the changes that affect tests/skipping). Looks generally good to me, I have left a few comments on possible oversights and stylistic opinions in-line.

from twisted.python.runtime import platform

doSkip = False
skipReason = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't especially like this pattern (having a pair of generically-named skip variables that are always set and used in sync). I think it'd be clearer to either use informatively-named flags (like you did with HAS_IPV6) or store the text in the skip flag (like the way this file was written before, type: Optional[str]).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this file, the existing pattern of skipping is a bit complicated, because there are multiple reasons for skipping tests, i.e. Windows, invalid tty, pyasn1 unavailable.

I tried to keep the pattern the same while introducing the use of skipIf

src/twisted/conch/test/test_session.py Outdated Show resolved Hide resolved
src/twisted/internet/test/test_unix.py Outdated Show resolved Hide resolved
testSSL.skip = ("Re-enable once the Python 3 SSL port is done.")
@skipIf(not interfaces.IReactorSSL(reactor, None),
"IReactorSSL is needed")
@skipIf(not ssl, "SSL support is missing")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, is there a situation where we have IReactorSSL but not the ssl module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, the tests were written like that before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From twisted/python/_setup.py

    requirements = [
        "zope.interface >= 4.4.2",
        "constantly >= 15.1",
        "incremental >= 16.10.1",
        "Automat >= 0.3.0",
        "hyperlink >= 17.1.1",
        "PyHamcrest >= 1.9.0",
        "attrs >= 17.4.0",
    ]

pyopenssl/cryptography is not a hard requirement.

You need to do the extra [tls] install.

_EXTRA_OPTIONS = dict(
    dev=_dev,
    tls=[
        'pyopenssl >= 16.0.0',
        # service_identity 18.1.0 added support for validating IP addresses in
        # certificate subjectAltNames
        'service_identity >= 18.1.0',
        # idna 2.3 introduced some changes that break a few things.  Avoid it.
        # The problems were fixed in 2.4.
        'idna >= 0.6, != 2.3',
    ],

But I agree that it might be practicle to always have pyopenssl as a dependency.

src/twisted/test/test_paths.py Outdated Show resolved Hide resolved
src/twisted/test/test_unix.py Outdated Show resolved Hide resolved
src/twisted/trial/test/test_assertions.py Show resolved Hide resolved
src/twisted/web/test/test_agent.py Show resolved Hide resolved
src/twisted/web/test/test_agent.py Outdated Show resolved Hide resolved
src/twisted/conch/test/test_scripts.py Show resolved Hide resolved
@rodrigc rodrigc force-pushed the 9812-rodrigc-mypy branch 5 times, most recently from d8f0f50 to 6b28463 Compare June 6, 2020 03:14
Comment on lines 27 to 30
skipWhenNoSSL = True
else:
skipWhenNoSSL = None
ssl = _ssl
skipWhenNoSSL = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce code duplication, maybe have something like this:

    ssl = None
    skipWhenNoSSL = (True, "SSL not available")
else:
    ssl = _ssl
    skipWhenNoSSL = (False, "")

@skipIf(*skipWhenNoSSL)
def test_wantedSupported(self):

Now I know that is a lot of work, so maybe just leave the duplicate code as it is.

Copy link
Member

@adiroiban adiroiban Jun 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And since there are a lot of SSL skip conditions, maybe it would make sense to have a shared code like this

from twisted.trial import conditionals


@conditionals.skipWhenNoTLS
def test_wantedSupported(self):

In this way, all the SSL skipping code is controller from a single place.

And there can be separate conditionals for each type of extra requirements, as defined in _setup.py

@@ -866,6 +866,13 @@ def test_setURLRelativePath(self):


class WebClientSSLTests(WebClientTests):

if ssl is None or not hasattr(ssl, 'DefaultOpenSSLContextFactory'):
skip = "OpenSSL not present"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the skipIf decorator here?

Copy link
Member

@adiroiban adiroiban left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes.

It would be nice to use this opportunity to organize the skipping conditions used in Twisted tests.

Like shared conditions for skipping on WIndows or on Unix, or when OpenSSL is missing


There is significant code duplication in some skip call, but we have the same duplication in trunk.

- @skipIf(not _PY35PLUS, "coroutines not available before Python 3.5")
+ onlyOnPy35Plus = not _PY35PLUS, "coroutines not available before Python 3.5"
+ @skipIf(*onlyOnPy35Plus)

As a drive by comment, I think that SOAP support can be removed


But this change is an overall improvement over trunk so I think that it can be merged.

@@ -517,13 +518,11 @@ def cbRendered(ignored):
return result


@skipIf(not pwd, "pwd module required")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be replaced by a shared skip condition for Unix... most Unix systems have pwd.
It is missing only on Win

This reduces errors reported by mypy of the form:

src/twisted/words/test/test_domish.py:193:9: error: "Callable[[ElementTests], Any]" has no attribute "skip"  [attr-defined]
            test_addContentBytes.skip = (
@rodrigc rodrigc merged commit 5f5c89d into trunk Jun 6, 2020
@rodrigc rodrigc deleted the 9812-rodrigc-mypy branch June 6, 2020 19:15
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

Successfully merging this pull request may close these issues.

None yet

3 participants