Skip to content

Instantly share code, notes, and snippets.

@aveuiller
Created January 12, 2021 21:34
Show Gist options
  • Save aveuiller/facfb5be45965a97efdcaf3fda7f6213 to your computer and use it in GitHub Desktop.
Save aveuiller/facfb5be45965a97efdcaf3fda7f6213 to your computer and use it in GitHub Desktop.
medium_flaky_tests
def current_time():
# Centralized method to retrieve the current datetime
return arrow.get()
def compute_stats():
# Compute some statistics about stored data
month = current_time().floor('month')
data = retrive_data(month)
return len(data)
@patch("current_time")
def test_compute_stats(time_mock):
# Test method checking the behaviour of compute_stats
time_mock.return_value = arrow.get().replace(day=20)
now = current_time()
add_data(now.shift(days=-5))
add_data(now.shift(days=-1))
stat = compute_stats()
assert stat == 2, "We retrieve the two data input"
def add_data(input_datetime, data):
# Store the data along with the input date
# [...]
def retrieve_data(lower_date):
# Retrieve all data from lower date to now
# [...]
def compute_stats():
# Compute some statistics about stored data
month = arrow.get().floor('month')
data = retrieve_data(month)
return len(data)
def test_compute_stats():
# Test method checking the behaviour of compute_stats
now = arrow.get()
add_data(now.shift(days=-5))
add_data(now.shift(days=-1))
stat = compute_stats()
assert stat == 2, "We retrieve the two data input"
class ConfiguredFeatureTest(unittest.TestCase):
def setUp(self):
config["entry"] = "anything"
def test_configured(self):
self.assertIsNotNone(config.get("entry"))
class ExistingFeatureTestCase(unittest.TestCase):
def test_feature_one(self):
self.assertTrue(existing_feature())
class NewFeatureTestCase(unittest.TestCase):
def setUp(self):
config["entry"] = "anything"
def tearDown(self):
config.clear()
def test_new_feature(self):
self.assertTrue(our_new_feature())
# Global state configuration
config = {}
def existing_feature():
if "common_entry" not in config:
raise ValueError("Not configured")
# Process [...]
return True
def our_new_feature():
if "common_entry" not in config:
raise ValueError("Not configured")
# Process [...]
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment