Best Python code snippet using assertpy_python
test_datetime.py
Source:test_datetime.py  
...152            assert_that(self.d1).is_greater_than(123)153            fail('should have raised error')154        except TypeError as ex:155            assert_that(str(ex)).is_equal_to('given arg must be <datetime>, but was <int>')156    def test_is_greater_than_or_equal_to(self):157        assert_that(self.d1).is_greater_than_or_equal_to(self.d1)158    def test_is_greater_than_or_equal_to_failure(self):159        try:160            d2 = datetime.datetime.today()161            assert_that(self.d1).is_greater_than_or_equal_to(d2)162            fail('should have raised error')163        except AssertionError as ex:164            assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be greater than or equal to <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')165    def test_is_greater_than_or_equal_to_bad_arg_type_failure(self):166        try:167            assert_that(self.d1).is_greater_than_or_equal_to(123)168            fail('should have raised error')169        except TypeError as ex:170            assert_that(str(ex)).is_equal_to('given arg must be <datetime>, but was <int>')171    def test_is_less_than(self):172        d2 = datetime.datetime.today()173        assert_that(self.d1).is_less_than(d2)174    def test_is_less_than_failure(self):175        try:176            d2 = datetime.datetime.today()177            assert_that(d2).is_less_than(self.d1)178            fail('should have raised error')179        except AssertionError as ex:180            assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be less than <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')181    def test_is_less_than_bad_arg_type_failure(self):182        try:183            assert_that(self.d1).is_less_than(123)184            fail('should have raised error')185        except TypeError as ex:186            assert_that(str(ex)).is_equal_to('given arg must be <datetime>, but was <int>')187    def test_is_less_than_or_equal_to(self):188        assert_that(self.d1).is_less_than_or_equal_to(self.d1)189    def test_is_less_than_or_equal_to_failure(self):190        try:191            d2 = datetime.datetime.today()192            assert_that(d2).is_less_than_or_equal_to(self.d1)193            fail('should have raised error')194        except AssertionError as ex:195            assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be less than or equal to <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')196    def test_is_less_than_or_equal_to_bad_arg_type_failure(self):197        try:198            assert_that(self.d1).is_less_than_or_equal_to(123)199            fail('should have raised error')200        except TypeError as ex:201            assert_that(str(ex)).is_equal_to('given arg must be <datetime>, but was <int>')202    def test_is_between(self):203        d2 = datetime.datetime.today()204        d3 = datetime.datetime.today()205        assert_that(d2).is_between(self.d1, d3)206    def test_is_between_failure(self):207        try:208            d2 = datetime.datetime.today()209            d3 = datetime.datetime.today()210            assert_that(self.d1).is_between(d2, d3)211            fail('should have raised error')212        except AssertionError as ex:213            assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be between <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> and <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}>, but was not.')214    def test_is_between_bad_arg1_type_failure(self):215        try:216            assert_that(self.d1).is_between(123, 456)217            fail('should have raised error')218        except TypeError as ex:219            assert_that(str(ex)).is_equal_to('given low arg must be <datetime>, but was <int>')220    def test_is_between_bad_arg2_type_failure(self):221        try:222            d2 = datetime.datetime.today()223            assert_that(self.d1).is_between(d2, 123)224            fail('should have raised error')225        except TypeError as ex:226            assert_that(str(ex)).is_equal_to('given high arg must be <datetime>, but was <datetime>')227    def test_is_close_to(self):228        d2 = datetime.datetime.today()229        assert_that(self.d1).is_close_to(d2, datetime.timedelta(minutes=5))230    def test_is_close_to_failure(self):231        try:232            d2 = self.d1 + datetime.timedelta(minutes=5)233            assert_that(self.d1).is_close_to(d2, datetime.timedelta(minutes=1))234            fail('should have raised error')235        except AssertionError as ex:236            assert_that(str(ex)).matches('Expected <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> to be close to <\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}> within tolerance <\d+:\d{2}:\d{2}>, but was not.')237    def test_is_close_to_bad_arg_type_failure(self):238        try:239            assert_that(self.d1).is_close_to(123, 456)240            fail('should have raised error')241        except TypeError as ex:242            assert_that(str(ex)).is_equal_to('given arg must be datetime, but was <int>')243    def test_is_close_to_bad_tolerance_arg_type_failure(self):244        try:245            d2 = datetime.datetime.today()246            assert_that(self.d1).is_close_to(d2, 123)247            fail('should have raised error')248        except TypeError as ex:249            assert_that(str(ex)).is_equal_to('given tolerance arg must be timedelta, but was <int>')250class TestTimedelta(object):251    def setup(self):252        self.t1 = datetime.timedelta(seconds=60)253    def test_is_greater_than(self):254        d2 = datetime.timedelta(seconds=120)255        assert_that(d2).is_greater_than(self.t1)256    def test_is_greater_than_failure(self):257        try:258            t2 = datetime.timedelta(seconds=90)259            assert_that(self.t1).is_greater_than(t2)260            fail('should have raised error')261        except AssertionError as ex:262            assert_that(str(ex)).matches('Expected <\d{1,2}:\d{2}:\d{2}> to be greater than <\d{1,2}:\d{2}:\d{2}>, but was not.')263    def test_is_greater_than_bad_arg_type_failure(self):264        try:265            assert_that(self.t1).is_greater_than(123)266            fail('should have raised error')267        except TypeError as ex:268            assert_that(str(ex)).is_equal_to('given arg must be <timedelta>, but was <int>')269    def test_is_greater_than_or_equal_to(self):270        assert_that(self.t1).is_greater_than_or_equal_to(self.t1)271    def test_is_greater_than_or_equal_to_failure(self):272        try:273            t2 = datetime.timedelta(seconds=90)274            assert_that(self.t1).is_greater_than_or_equal_to(t2)275            fail('should have raised error')276        except AssertionError as ex:277            assert_that(str(ex)).matches('Expected <\d{1,2}:\d{2}:\d{2}> to be greater than or equal to <\d{1,2}:\d{2}:\d{2}>, but was not.')278    def test_is_greater_than_or_equal_to_bad_arg_type_failure(self):279        try:280            assert_that(self.t1).is_greater_than_or_equal_to(123)281            fail('should have raised error')282        except TypeError as ex:283            assert_that(str(ex)).is_equal_to('given arg must be <timedelta>, but was <int>')...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!!
