Skip to content

Fix for pytest.mark.enable_socket - #28

Closed
TripleDogDare wants to merge 1 commit into
miketheman:masterfrom
TripleDogDare:master
Closed

TripleDogDare wants to merge 1 commit into
miketheman:masterfrom
TripleDogDare:master

Conversation

@TripleDogDare

Copy link
Copy Markdown
Contributor

A few tests for enabled sockets had inverted success criteria.
Issue #15 is resolved by prioritizing the pytest markers over the CLI config.

@TripleDogDare

Copy link
Copy Markdown
Contributor Author

Not sure what's up with appveyor. Seems pretty unrelated.

@miketheman

Copy link
Copy Markdown
Owner

Thanks - I haven't had a chance to look at this, and will try to come back to evaluate this in a few weeks.
I think the test failures may be due to a dependency stopping support for pre-3.7.

Comment thread tests/test_socket.py
Comment on lines +115 to +116
with pytest.raises(BaseException):
assert_socket_blocked(result)

@atugushev atugushev Oct 30, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems there's no need in assert_socket_blocked check. The with block can be deleted. The main check is result.assert_outcomes(1, 0, 0).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's consistent with the other tests this way though.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Indeed, but it is strange. Look at the definition of it

def assert_socket_blocked(result):
    result.stdout.fnmatch_lines("""
        *SocketBlockedError: A test tried to use socket.socket.*
    """)

AFAICT, assert_socket_blocked() does not raise an exception ...

https://docs.pytest.org/en/latest/_modules/_pytest/pytester.html#Testdir.runpytest
which can return
https://docs.pytest.org/en/latest/reference.html#_pytest.pytester.RunResult

But when we use fnmatch on the line matcher, I guess that can throw due to pytest fail()?
https://docs.pytest.org/en/latest/reference.html#_pytest.pytester.LineMatcher.fnmatch_lines

Anyway the point is, let's make sure the logic is correct. The TLDR I think is that assert_socket_blocked(result) will throw an exception if socket IS NOT blocked.

So test_enable_socket_marker SHOULD NOT BLOCK the socket via @pytest.mark.enable_socket, so therefore assert_socket_blocked(result) SHOULD be raising an exception.

TBH this is a confusing way to do it, but as @TripleDogDare points out, this is the convention of all other tests. The "proper" thing to do IMO is write an assertion method called assert_socket_not_blocked() (but that is out of scope of this activity).

@atugushev

Copy link
Copy Markdown
Contributor

@TripleDogDare could you rebase onto master this PR? The issue with AppVeyor is fixed in #34.

@atugushev atugushev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've tested this locally and the fix works perfectly fine! Thanks for fixing this. Have a minor comment above.

Comment thread pytest_socket.py

if request.config.getoption('--disable-socket'):
request.getfixturevalue('socket_disabled')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

any comments how this fixes the issue? (if there is an ordering dependency we should probably add comments explaining it)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's an ordering issue. The things higher on this list have lower priority. The last thing to set the fixture value wins.

Generally for the purposes in which someone would be using this package (IMHO), people expect more granular settings to have highest priority.
I.E. a marker on a particular function overrides the global disable flag.

This is also the documented and expected behavior; the readme states:

To enable specific tests use of socket, pass in the fixture to the test or use a marker

I can add a bit of comments to the function here.

A few tests for enabled sockets had inverted success criteria.
Issue miketheman#15 is resolved by prioritizing the pytest markers over the CLI config.

Signed-off-by: Calvin Behling <[email protected]>
@TripleDogDare

Copy link
Copy Markdown
Contributor Author

Huh. That was not what I intended to do.

@TripleDogDare TripleDogDare reopened this Jan 2, 2020
@qlty-cloud-legacy

Copy link
Copy Markdown

Code Climate has analyzed commit 53b035b and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 95.0% (0.0% change).

View more on Code Climate.

@mcallaghan-bsm

Copy link
Copy Markdown

LGTM

@miketheman miketheman added the bug Something isn't working label May 29, 2020
@miketheman miketheman self-assigned this May 29, 2020
@miketheman

Copy link
Copy Markdown
Owner

Hey y'all - taking a look at this today, and I'm confirming the findings y'all have produced - thanks for engaging and clarifying. I think the mismatch crept in a larger-than-desirable refactor three years ago, and I've been behind on managing this project.

While diagnosing this, I'm continuing to think about the concept of "order" and how to resolve the approach of overrides - so I'm looking to pytest to find other patterns that combine the idea of get_closest_marker and config.get to better resolve this.

miketheman added a commit that referenced this pull request May 31, 2020
As reported in a couple of issues, the usage of markers (not fixtures)
was misbehaving, as the order in which the `_socket_marker` evaluation
was occurring left the command-line option disabled.

Fixes #15
Closes #28

Co-authored-by: Calvin Behling <[email protected]>
Signed-off-by: Albert Tugushev <[email protected]>
Signed-off-by: Matt Callaghan <[email protected]>
Signed-off-by: Mike Fiedler <[email protected]>
miketheman added a commit that referenced this pull request May 31, 2020
As reported in a couple of issues, the usage of markers (not fixtures)
was misbehaving, as the order in which the `_socket_marker` evaluation
was occurring left the command-line option disabled.

Fixes #15
Closes #28

Co-authored-by: Calvin Behling <[email protected]>
Signed-off-by: Albert Tugushev <[email protected]>
Signed-off-by: Matt Callaghan <[email protected]>
Signed-off-by: Mike Fiedler <[email protected]>
@miketheman

Copy link
Copy Markdown
Owner

Thanks for the notes and help here! I've incorporated these changes with slight modifications to the language in #42 and released on pypi as version 0.3.5
I'd also recommend subscribing here to receive notice of releases: https://libraries.io/pypi/pytest-socket/0.3.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants