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

Modify test and utils.py to fix flaky test #203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

blazyy
Copy link

@blazyy blazyy commented Nov 2, 2022

The test test_get_ip() under the SanityTest class inside test_noipy.py was found to be flaky, i.e. a test that both passes and fail despite no changes to the code or the tests itself.

Using the pytest-flakefinder plugin, when the test was re-run more than once, it was failing. Upon looking at the code, the reason was because the the value of HTTPBIN_URL inside utils.py was not being set back to its default value of https://httpbin.org/ip

To fix it, I just changed the test_get_ip() test and have it be provided with the HTTPBIN_URL value instead of the value being read from the global variable.

To reproduce flakiness:

  • Install pytest-flakefinder by following the instructions here.
  • At root directory, run pytest --flake-finder test/test_noipy.py::SanityTest::test_get_ip

After implementing the fix, all tests pass successfully, both with and without the pytest-flakefinder plugin being used.

In general, flaky tests are a pain to fix with regards to locating the root of the actual problem, so it's a good idea to fix them when you detect them. I'm aware that the tests might not be re-run at all, but this change makes the entire module more robust and future-proof so it's just a small fix for you to consider.

Copy link
Owner

@pv8 pv8 left a comment

Choose a reason for hiding this comment

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

@blazyy, thank you for your contribution! I've added a suggestion, please take a look.

@@ -21,10 +19,10 @@ def read_input(message):
return input(message)


def get_ip():
def get_ip(httpbin_url):
Copy link
Owner

@pv8 pv8 Jan 17, 2023

Choose a reason for hiding this comment

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

Since this method is being used in other parts of the code (I need to improve the tests in general 😞), what about attributing a default value to the new function parameter?

Suggested change
def get_ip(httpbin_url):
def get_ip(req_ip_url=HTTPBIN_URL):

@@ -31,14 +31,12 @@ def tearDown(self):
shutil.rmtree(self.test_dir)

def test_get_ip(self):
ip = utils.get_ip()
ip = utils.get_ip('https://httpbin.org/ip')
Copy link
Owner

Choose a reason for hiding this comment

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

Please, take a look at the above comment.

Suggested change
ip = utils.get_ip('https://httpbin.org/ip')
ip = utils.get_ip()

utils.HTTPBIN_URL = 'http://example.nothing'

ip = utils.get_ip()
ip = utils.get_ip('http://example.nothing')
Copy link
Owner

Choose a reason for hiding this comment

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

Please, take a look at the above comment.

Suggested change
ip = utils.get_ip('http://example.nothing')
ip = utils.get_ip(req_ip_url='http://example.nothing')

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

Successfully merging this pull request may close these issues.

2 participants