Skip to content

Commit

Permalink
Move all DNS tests into a mock environment
Browse files Browse the repository at this point in the history
This avoids all of the "flakiness" in the tests and actually tests the
behavior, which is a better test setup.
  • Loading branch information
michaelherold committed Apr 18, 2015
1 parent b61dd4e commit 395ba79
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 232 deletions.
19 changes: 14 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ def get_version():
return locals['VERSION']
raise RuntimeError('No version information found.')


def tests_require():
requirements = [
"coverage",
"testtools >= 0.9.21",
"testscenarios >= 0.3"
]

if sys.version_info[0] < 3:
requirements.append("mock >= 1.0.1")

return requirements

setup(
name="pyIsEmail",
version=get_version(),
Expand Down Expand Up @@ -53,11 +66,7 @@ def get_version():
install_requires=[
"%s >= 1.10.0" % dnspython,
],
tests_require=[
"coverage",
"testtools >= 0.9.21",
"testscenarios >= 0.3"
],
tests_require=tests_require(),
test_suite="tests",
**kwargs
)
110 changes: 0 additions & 110 deletions tests/data/dns-tests.xml

This file was deleted.

9 changes: 0 additions & 9 deletions tests/data/tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ Date Tests Version Notes
<diagnosis>ISEMAIL_VALID</diagnosis>
<source>Michael Rushton</source>
<sourcelink>http://squiloople.com/tag/email/</sourcelink>
<flaky>True</flaky>
</test>
<test id="6">
<address>@io</address>
Expand Down Expand Up @@ -223,11 +222,6 @@ Date Tests Version Notes
<diagnosis>ISEMAIL_VALID</diagnosis>
<source>Michael Rushton</source>
<sourcelink>http://squiloople.com/tag/email/</sourcelink>
<flaky>True</flaky>
<comment>
Marked as flaky on 2015-04-15. It seems the DNS record(s) have
been removed for this domain, so it is not expected to pass anymore.
</comment>
</test>
<test id="34">
<address>test@.iana.org</address>
Expand Down Expand Up @@ -686,12 +680,9 @@ Date Tests Version Notes
<address>test@xn--hxajbheg2az3al.xn--jxalpdlp</address>
<comment>
A valid IDN from ICANN's <a href="http://idn.icann.org/#The_example.test_names">IDN TLD evaluation gateway</a>.
Marked as flaky on 2014-01-27, as the DNS lookup started
returning false and the IDN page is down for maintenance.
</comment>
<category>ISEMAIL_VALID_CATEGORY</category>
<diagnosis>ISEMAIL_VALID</diagnosis>
<flaky>True</flaky>
<source>Michael Rushton</source>
<sourcelink>http://squiloople.com/tag/email/</sourcelink>
</test>
Expand Down
55 changes: 4 additions & 51 deletions tests/test_is_email.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from unittest import expectedFailure
from testscenarios import TestWithScenarios
from pyisemail import is_email
from pyisemail.diagnosis import BaseDiagnosis
Expand All @@ -7,11 +6,11 @@

class IsEmailTest(TestWithScenarios):

scenarios = get_scenarios("tests.xml") + get_scenarios("dns-tests.xml")
scenarios = get_scenarios("tests.xml")
threshold = BaseDiagnosis.CATEGORIES['THRESHOLD']

def test_without_diagnosis(self):
result = is_email(self.address, check_dns=True)
result = is_email(self.address)
expected = create_diagnosis(self.diagnosis) < self.threshold

self.assertEqual(
Expand All @@ -22,58 +21,12 @@ def test_without_diagnosis(self):
)

def test_with_diagnosis(self):
result = is_email(self.address, check_dns=True, diagnose=True)
result = is_email(self.address, diagnose=True)
expected = create_diagnosis(self.diagnosis)

try:
self.assertEqual(
result,
expected,
("%s (%s): Got %s, but expected %s."
% (self.id, self.address, result, expected))
)
except AssertionError as err:
if self.flaky is True:
pass
else:
raise err


class IsEmailFlakyTest(TestWithScenarios):

"""Test suite for flaky is_email tests.
Due to different DNS servers handling missing domains differently, these
tests are marked as flaky. A flaky test might succeed in one environment
and fail in another, purely due to DNS issues. An ideal fix for this
behavior would be to mock the DNS check to alleviate this problem, but for
now this will have to do.
"""

scenarios = (get_scenarios("tests.xml", flaky=True) +
get_scenarios("dns-tests.xml", flaky=True))
threshold = BaseDiagnosis.CATEGORIES['THRESHOLD']

def test_without_diagnosis(self):
result = is_email(self.address, check_dns=True)
expected = create_diagnosis(self.diagnosis) < self.threshold

self.assertEqual(
result,
expected,
("%s (%s): Got %s, but expected %s."
% (self.id, self.address, result, expected))
)

@expectedFailure
def test_with_diagnosis(self):
result = is_email(self.address, check_dns=True, diagnose=True)
expected = create_diagnosis(self.diagnosis)

self.assertEqual(
result,
expected,
("%s (%s): Got %s, but expected %s."
% (self.id, self.address, result, expected))
% (self.id, self.address, result, expected))
)
Loading

0 comments on commit 395ba79

Please sign in to comment.