Best Python code snippet using freezegun
test_datetimes.py
Source:test_datetimes.py
...318 def test_the_class(self):319 assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)320 def test_still_the_same(self):321 assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)322 def test_class_name_preserved_by_decorator(self):323 assert self.__class__.__name__ == "Tester"324 class NotATestClass:325 def perform_operation(self):326 return datetime.date.today()327 @freeze_time('2001-01-01')328 def test_class_decorator_ignores_nested_class(self):329 not_a_test = self.NotATestClass()330 assert not_a_test.perform_operation() == datetime.date(2001, 1, 1)331 a_mock = Callable()332 def test_class_decorator_wraps_callable_object_py3(self):333 assert self.a_mock.__wrapped__.__class__ == Callable334 @staticmethod335 def helper():336 return datetime.date.today()337 def test_class_decorator_respects_staticmethod(self):338 assert self.helper() == datetime.date(2012, 1, 14)339@freeze_time("Jan 14th, 2012")340def test_nice_datetime():341 assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)342@freeze_time("2012-01-14")343def test_datetime_date_method():344 now = datetime.datetime.now()345 assert now.date() == FakeDate(2012, 1, 14)346def test_context_manager():347 with freeze_time("2012-01-14"):348 assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)349 assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)350def test_nested_context_manager():351 with freeze_time("2012-01-14"):352 with freeze_time("2012-12-25"):353 _assert_datetime_date_and_time_are_all_equal(datetime.datetime(2012, 12, 25))354 _assert_datetime_date_and_time_are_all_equal(datetime.datetime(2012, 1, 14))355 assert datetime.datetime.now() > datetime.datetime(2013, 1, 1)356def _assert_datetime_date_and_time_are_all_equal(expected_datetime):357 assert datetime.datetime.now() == expected_datetime358 assert datetime.date.today() == expected_datetime.date()359 assert datetime.datetime.fromtimestamp(time.time()) == expected_datetime360def test_nested_context_manager_with_tz_offsets():361 with freeze_time("2012-01-14 23:00:00", tz_offset=2):362 with freeze_time("2012-12-25 19:00:00", tz_offset=6):363 assert datetime.datetime.now() == datetime.datetime(2012, 12, 26, 1)364 assert datetime.date.today() == datetime.date(2012, 12, 26)365 # no assertion for time.time() since it's not affected by tz_offset366 assert datetime.datetime.now() == datetime.datetime(2012, 1, 15, 1)367 assert datetime.date.today() == datetime.date(2012, 1, 15)368 assert datetime.datetime.now() > datetime.datetime(2013, 1, 1)369@freeze_time("Jan 14th, 2012")370def test_isinstance_with_active():371 now = datetime.datetime.now()372 assert utils.is_fake_datetime(now)373 assert utils.is_fake_date(now.date())374 today = datetime.date.today()375 assert utils.is_fake_date(today)376def test_isinstance_without_active():377 now = datetime.datetime.now()378 assert isinstance(now, datetime.datetime)379 assert isinstance(now, datetime.date)380 assert isinstance(now.date(), datetime.date)381 today = datetime.date.today()382 assert isinstance(today, datetime.date)383class TestUnitTestMethodDecorator(unittest.TestCase):384 @freeze_time('2013-04-09')385 def test_method_decorator_works_on_unittest(self):386 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())387 @freeze_time('2013-04-09', as_kwarg='frozen_time')388 def test_method_decorator_works_on_unittest_kwarg_frozen_time(self, frozen_time):389 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())390 self.assertEqual(datetime.date(2013, 4, 9), frozen_time.time_to_freeze.today())391 @freeze_time('2013-04-09', as_kwarg='hello')392 def test_method_decorator_works_on_unittest_kwarg_hello(self, **kwargs):393 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())394 self.assertEqual(datetime.date(2013, 4, 9), kwargs.get('hello').time_to_freeze.today())395 @freeze_time(lambda: datetime.date(year=2013, month=4, day=9), as_kwarg='frozen_time')396 def test_method_decorator_works_on_unittest_kwarg_frozen_time_with_func(self, frozen_time):397 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())398 self.assertEqual(datetime.date(2013, 4, 9), frozen_time.time_to_freeze.today())399@freeze_time('2013-04-09')400class TestUnitTestClassDecorator(unittest.TestCase):401 @classmethod402 def setUpClass(cls):403 assert datetime.date(2013, 4, 9) == datetime.date.today()404 def setUp(self):405 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())406 def tearDown(self):407 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())408 @classmethod409 def tearDownClass(cls):410 assert datetime.date(2013, 4, 9) == datetime.date.today()411 def test_class_decorator_works_on_unittest(self):412 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())413 def test_class_name_preserved_by_decorator(self):414 self.assertEqual(self.__class__.__name__, "TestUnitTestClassDecorator")415@freeze_time('2013-04-09')416class TestUnitTestClassDecoratorWithNoSetUpOrTearDown(unittest.TestCase):417 def test_class_decorator_works_on_unittest(self):418 self.assertEqual(datetime.date(2013, 4, 9), datetime.date.today())419class TestUnitTestClassDecoratorSubclass(TestUnitTestClassDecorator):420 @classmethod421 def setUpClass(cls):422 # the super() call can fail if the class decoration was done wrong423 super().setUpClass()424 @classmethod425 def tearDownClass(cls):426 # the super() call can fail if the class decoration was done wrong427 super().tearDownClass()428 def test_class_name_preserved_by_decorator(self):429 self.assertEqual(self.__class__.__name__,430 "TestUnitTestClassDecoratorSubclass")431class BaseInheritanceFreezableTests(unittest.TestCase):432 @classmethod433 def setUpClass(cls):434 pass435 @classmethod436 def tearDownClass(cls):437 pass438class UnfrozenInheritedTests(BaseInheritanceFreezableTests):439 def test_time_is_not_frozen(self):440 # In this class, time should not be frozen - and the below decorated441 # class shouldn't affect that442 self.assertNotEqual(datetime.date(2013, 4, 9), datetime.date.today())...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!