Best Python code snippet using sure_python
test_old_api.py
Source:test_old_api.py
...65 something.modified = True66 def teardown(context):67 del something.modified68 @sure.that_with_context(setup, teardown)69 def something_was_modified(context):70 assert hasattr(something, "modified")71 assert something.modified72 something_was_modified()73 assert not hasattr(something, "modified")74def test_that_is_a():75 "that() is_a(object)"76 something = "something"77 assert that(something).is_a(text_type)78 assert isinstance(something, text_type)79def test_that_equals():80 "that() equals(string)"81 something = "something"82 assert that('something').equals(something)83 assert something == 'something'84def test_that_differs():85 "that() differs(object)"86 something = "something"87 assert that(something).differs("23123%FYTUGIHOfdf")88 assert something != "23123%FYTUGIHOfdf"89def test_that_has():90 "that() has(object)"91 class Class:92 name = "some class"93 Object = Class()94 dictionary = {95 'name': 'John',96 }97 name = "john"98 assert hasattr(Class, 'name')99 assert that(Class).has("name")100 assert that(Class).like("name")101 assert "name" in that(Class)102 assert hasattr(Object, 'name')103 assert that(Object).has("name")104 assert that(Object).like("name")105 assert "name" in that(Object)106 assert 'name' in dictionary107 assert that(dictionary).has("name")108 assert that(dictionary).like("name")109 assert "name" in that(dictionary)110 assert that(name).has("john")111 assert that(name).like("john")112 assert "john" in that(name)113 assert that(name).has("hn")114 assert that(name).like("hn")115 assert "hn" in that(name)116 assert that(name).has("jo")117 assert that(name).like("jo")118 assert "jo" in that(name)119def test_that_at_key_equals():120 "that().at(object).equals(object)"121 class Class:122 name = "some class"123 Object = Class()124 dictionary = {125 'name': 'John',126 }127 assert that(Class).at("name").equals('some class')128 assert that(Object).at("name").equals('some class')129 assert that(dictionary).at("name").equals('John')130def test_that_len_is():131 "that() len_is(number)"132 lst = range(1000)133 assert that(lst).len_is(1000)134 assert len(lst) == 1000135 assert that(lst).len_is(lst)136def test_that_len_greater_than():137 "that() len_greater_than(number)"138 lst = range(1000)139 lst2 = range(100)140 assert that(lst).len_greater_than(100)141 assert len(lst) == 1000142 assert that(lst).len_greater_than(lst2)143def test_that_len_greater_than_should_raise_assertion_error():144 "that() len_greater_than(number) raise AssertionError"145 lst = list(range(1000))146 try:147 that(lst).len_greater_than(1000)148 except AssertionError as e:149 assert_equals(150 str(e),151 'the length of the list should be greater then %d, but is %d' \152 % (1000, 1000))153def test_that_len_greater_than_or_equals():154 "that() len_greater_than_or_equals(number)"155 lst = list(range(1000))156 lst2 = list(range(100))157 assert that(lst).len_greater_than_or_equals(100)158 assert that(lst).len_greater_than_or_equals(1000)159 assert len(lst) == 1000160 assert that(lst).len_greater_than_or_equals(lst2)161 assert that(lst).len_greater_than_or_equals(lst)162def test_that_len_greater_than_or_equals_should_raise_assertion_error():163 "that() len_greater_than_or_equals(number) raise AssertionError"164 lst = list(range(1000))165 try:166 that(lst).len_greater_than_or_equals(1001)167 except AssertionError as e:168 assert_equals(169 str(e),170 'the length of %r should be greater then or equals %d, but is %d' \171 % (lst, 1001, 1000))172def test_that_len_lower_than():173 "that() len_lower_than(number)"174 lst = list(range(100))175 lst2 = list(range(1000))176 assert that(lst).len_lower_than(101)177 assert len(lst) == 100178 assert that(lst).len_lower_than(lst2)179def test_that_len_lower_than_should_raise_assertion_error():180 "that() len_lower_than(number) raise AssertionError"181 lst = list(range(1000))182 try:183 that(lst).len_lower_than(1000)184 except AssertionError as e:185 assert_equals(186 str(e),187 'the length of %r should be lower then %d, but is %d' % \188 (lst, 1000, 1000))189def test_that_len_lower_than_or_equals():190 "that() len_lower_than_or_equals(number)"191 lst = list(range(1000))192 lst2 = list(range(1001))193 assert that(lst).len_lower_than_or_equals(1001)194 assert that(lst).len_lower_than_or_equals(1000)195 assert len(lst) == 1000196 assert that(lst).len_lower_than_or_equals(lst2)197 assert that(lst).len_lower_than_or_equals(lst)198def test_that_len_lower_than_or_equals_should_raise_assertion_error():199 "that() len_lower_than_or_equals(number) raise AssertionError"200 lst = list(range(1000))201 try:202 that(lst).len_lower_than_or_equals(100)203 except AssertionError as e:204 assert_equals(205 str(e),206 'the length of %r should be lower then or equals %d, but is %d' % \207 (lst, 100, 1000))208def test_that_checking_all_atributes():209 "that(iterable).the_attribute('name').equals('value')"210 class shape(object):211 def __init__(self, name):212 self.kind = 'geometrical form'213 self.name = name214 shapes = [215 shape('circle'),216 shape('square'),217 shape('rectangle'),218 shape('triangle'),219 ]220 assert that(shapes).the_attribute("kind").equals('geometrical form')221def test_that_checking_all_atributes_of_range():222 "that(iterable, within_range=(1, 2)).the_attribute('name').equals('value')"223 class shape(object):224 def __init__(self, name):225 self.kind = 'geometrical form'226 self.name = name227 def __repr__(self):228 return '<%s:%s>' % (self.kind, self.name)229 shapes = [230 shape('circle'),231 shape('square'),232 shape('square'),233 shape('triangle'),234 ]235 assert shapes[0].name != 'square'236 assert shapes[3].name != 'square'237 assert shapes[1].name == 'square'238 assert shapes[2].name == 'square'239 assert that(shapes, within_range=(1, 2)). \240 the_attribute("name"). \241 equals('square')242def test_that_checking_all_elements():243 "that(iterable).every_one_is('value')"244 shapes = [245 'cube',246 'ball',247 'ball',248 'piramid',249 ]250 assert shapes[0] != 'ball'251 assert shapes[3] != 'ball'252 assert shapes[1] == 'ball'253 assert shapes[2] == 'ball'254 assert that(shapes, within_range=(1, 2)).every_one_is('ball')255def test_that_checking_each_matches():256 "that(iterable).in_each('').equals('value')"257 class animal(object):258 def __init__(self, kind):259 self.attributes = {260 'class': 'mammal',261 'kind': kind,262 }263 animals = [264 animal('dog'),265 animal('cat'),266 animal('cow'),267 animal('cow'),268 animal('cow'),269 ]270 assert animals[0].attributes['kind'] != 'cow'271 assert animals[1].attributes['kind'] != 'cow'272 assert animals[2].attributes['kind'] == 'cow'273 assert animals[3].attributes['kind'] == 'cow'274 assert animals[4].attributes['kind'] == 'cow'275 assert animals[0].attributes['class'] == 'mammal'276 assert animals[1].attributes['class'] == 'mammal'277 assert animals[2].attributes['class'] == 'mammal'278 assert animals[3].attributes['class'] == 'mammal'279 assert animals[4].attributes['class'] == 'mammal'280 assert that(animals).in_each("attributes['class']").matches('mammal')281 assert that(animals).in_each("attributes['class']"). \282 matches(['mammal','mammal','mammal','mammal','mammal'])283 assert that(animals).in_each("attributes['kind']"). \284 matches(['dog','cat','cow','cow','cow'])285 try:286 assert that(animals).in_each("attributes['kind']").matches(['dog'])287 assert False, 'should not reach here'288 except AssertionError as e:289 assert that(text_type(e)).equals(290 '%r has 5 items, but the matching list has 1: %r' % (291 ['dog','cat','cow','cow','cow'], ['dog'],292 )293 )294def test_that_raises():295 "that(callable, with_args=[arg1], and_kwargs={'arg2': 'value'}).raises(SomeException)"296 global called297 called = False298 def function(arg1=None, arg2=None):299 global called300 called = True301 if arg1 == 1 and arg2 == 2:302 raise RuntimeError('yeah, it failed')303 return "OK"304 try:305 function(1, 2)306 assert False, 'should not reach here'307 except RuntimeError as e:308 assert text_type(e) == 'yeah, it failed'309 except Exception:310 assert False, 'should not reach here'311 finally:312 assert called313 called = False314 assert_raises(RuntimeError, function, 1, 2)315 called = False316 assert_equals(function(3, 5), 'OK')317 assert called318 called = False319 assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \320 raises(RuntimeError)321 assert called322 called = False323 assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \324 raises(RuntimeError, 'yeah, it failed')325 assert called326 called = False327 assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \328 raises('yeah, it failed')329 assert called330 called = False331 assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \332 raises(RuntimeError)333 assert called334 called = False335 assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \336 raises(RuntimeError, 'yeah, it failed')337 assert called338 called = False339 assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \340 raises('yeah, it failed')341 assert called342 called = False343 assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \344 raises(r'it fail')345 assert called346 called = False347 assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \348 raises(RuntimeError, r'it fail')349 assert called350def test_that_looks_like():351 "that('String\\n with BREAKLINE').looks_like('string with breakline')"352 assert that('String\n with BREAKLINE').looks_like('string with breakline')353def test_that_raises_with_args():354 "that(callable, with_args=['foo']).raises(FooError)"355 class FooError(Exception):356 pass357 def my_function(string):358 if string == 'foo':359 raise FooError('OOps')360 assert that(my_function, with_args=['foo']).raises(FooError, 'OOps')361def test_that_does_not_raise_with_args():362 "that(callable).doesnt_raise(FooError) and does_not_raise"363 class FooError(Exception):364 pass365 def my_function(string):366 if string == 'foo':367 raise FooError('OOps')368 assert that(my_function, with_args=['foo']).raises(FooError, 'OOps')369def test_that_contains_string():370 "that('foobar').contains('foo')"371 assert 'foo' in 'foobar'372 assert that('foobar').contains('foo')373def test_that_doesnt_contain_string():374 "that('foobar').does_not_contain('123'), .doesnt_contain"375 assert '123' not in 'foobar'376 assert that('foobar').doesnt_contain('123')377 assert that('foobar').does_not_contain('123')378def test_that_contains_none():379 "that('foobar').contains(None)"380 def assertions():381 # We can't use unicode in Py2, otherwise it will try to coerce382 assert that('foobar' if PY3 else b'foobar').contains(None)383 assert that(assertions).raises(384 TypeError,385 "'in <string>' requires string as left operand, not NoneType",386 )387def test_that_none_contains_string():388 "that(None).contains('bungalow')"389 try:390 assert that(None).contains('bungalow')391 assert False, 'should not reach here'392 except Exception as e:393 assert_equals(394 text_type(e),395 "argument of type 'NoneType' is not iterable",396 )397def test_that_some_iterable_is_empty():398 "that(some_iterable).is_empty and that(something).are_empty"399 assert that([]).is_empty400 assert that([]).are_empty401 assert that(tuple()).is_empty402 assert that({}).are_empty403 def fail_single():404 assert that((1,)).is_empty405 assert that(fail_single).raises('(1,) is not empty, it has 1 item')406 def fail_plural():407 assert that((1, 2)).is_empty408 assert that(fail_plural).raises('(1, 2) is not empty, it has 2 items')409def test_that_something_is_empty_raises():410 "that(something_not_iterable).is_empty and that(something_not_iterable).are_empty raises"411 obj = object()412 def fail():413 assert that(obj).is_empty414 assert False, 'should not reach here'415 assert that(fail).raises('%r is not iterable' % obj)416def test_that_something_iterable_matches_another():417 "that(something_iterable).matches(another_iterable)"418 # types must be unicode in py3, bute bytestrings in py2419 KlassOne = type('KlassOne' if PY3 else b'KlassOne', (object,), {})420 KlassTwo = type('KlassTwo' if PY3 else b'KlassTwo', (object,), {})421 one = [422 ("/1", KlassOne),423 ("/2", KlassTwo),424 ]425 two = [426 ("/1", KlassOne),427 ("/2", KlassTwo),428 ]429 assert that(one).matches(two)430 assert that(one).equals(two)431 def fail_1():432 assert that([1]).matches(xrange(2))433 class Fail2(object):434 def __init__(self):435 assert that(xrange(1)).matches([2])436 class Fail3(object):437 def __call__(self):438 assert that(xrange(1)).matches([2])439 xrange_name = xrange.__name__440 assert that(fail_1).raises('X is a list and Y is a {0} instead'.format(xrange_name))441 assert that(Fail2).raises('X is a {0} and Y is a list instead'.format(xrange_name))442 assert that(Fail3()).raises('X is a {0} and Y is a list instead'.format(xrange_name))443def test_within_pass():444 "within(five=miliseconds) will pass"445 from sure import within, miliseconds446 within(five=miliseconds)(lambda *a: None)()447def test_within_fail():448 "within(five=miliseconds) will fail"449 import time450 from sure import within, miliseconds451 def sleepy(*a):452 time.sleep(0.7)453 failed = False454 try:455 within(five=miliseconds)(sleepy)()456 except AssertionError as e:457 failed = True458 assert_equals('sleepy did not run within five miliseconds', str(e))459 assert failed, 'within(five=miliseconds)(sleepy) did not fail'460def test_word_to_number():461 assert_equals(sure.word_to_number('one'), 1)462 assert_equals(sure.word_to_number('two'), 2)463 assert_equals(sure.word_to_number('three'), 3)464 assert_equals(sure.word_to_number('four'), 4)465 assert_equals(sure.word_to_number('five'), 5)466 assert_equals(sure.word_to_number('six'), 6)467 assert_equals(sure.word_to_number('seven'), 7)468 assert_equals(sure.word_to_number('eight'), 8)469 assert_equals(sure.word_to_number('nine'), 9)470 assert_equals(sure.word_to_number('ten'), 10)471 assert_equals(sure.word_to_number('eleven'), 11)472 assert_equals(sure.word_to_number('twelve'), 12)473def test_word_to_number_fail():474 failed = False475 try:476 sure.word_to_number('twenty')477 except AssertionError as e:478 failed = True479 assert_equals(480 text_type(e),481 'sure supports only literal numbers from one ' \482 'to twelve, you tried the word "twenty"')483 assert failed, 'should raise assertion error'484def test_microsecond_unit():485 "testing microseconds convertion"486 cfrom, cto = sure.UNITS[sure.microsecond]487 assert_equals(cfrom(1), 100000)488 assert_equals(cto(1), 1)489 cfrom, cto = sure.UNITS[sure.microseconds]490 assert_equals(cfrom(1), 100000)491 assert_equals(cto(1), 1)492def test_milisecond_unit():493 "testing miliseconds convertion"494 cfrom, cto = sure.UNITS[sure.milisecond]495 assert_equals(cfrom(1), 1000)496 assert_equals(cto(100), 1)497 cfrom, cto = sure.UNITS[sure.miliseconds]498 assert_equals(cfrom(1), 1000)499 assert_equals(cto(100), 1)500def test_second_unit():501 "testing seconds convertion"502 cfrom, cto = sure.UNITS[sure.second]503 assert_equals(cfrom(1), 1)504 assert_equals(cto(100000), 1)505 cfrom, cto = sure.UNITS[sure.seconds]506 assert_equals(cfrom(1), 1)507 assert_equals(cto(100000), 1)508def test_minute_unit():509 "testing minutes convertion"510 cfrom, cto = sure.UNITS[sure.minute]511 assert_equals(cfrom(60), 1)512 assert_equals(cto(1), 6000000)513 cfrom, cto = sure.UNITS[sure.minutes]514 assert_equals(cfrom(60), 1)515 assert_equals(cto(1), 6000000)516def test_within_pass_utc():517 "within(five=miliseconds) gives utc parameter"518 from sure import within, miliseconds519 from datetime import datetime520 def assert_utc(utc):521 assert isinstance(utc, datetime)522 within(five=miliseconds)(assert_utc)()523def test_that_is_a_matcher_should_absorb_callables_to_be_used_as_matcher():524 "that.is_a_matcher should absorb callables to be used as matcher"525 @that.is_a_matcher526 def is_truthful(what):527 assert bool(what), '%s is so untrue' % (what)528 return 'foobar'529 assert that('friend').is_truthful()530 assert_equals(that('friend').is_truthful(), 'foobar')531def test_accepts_setup_list():532 "sure.with_context() accepts a list of callbacks for setup"533 def setup1(context):534 context.first_name = "John"535 def setup2(context):536 context.last_name = "Resig"537 @sure.that_with_context([setup1, setup2])538 def john_is_within_context(context):539 assert context.first_name == 'John'540 assert context.last_name == 'Resig'541 john_is_within_context()542 assert_equals(543 john_is_within_context.__name__,544 'john_is_within_context',545 )546def test_accepts_teardown_list():547 "sure.with_context() runs teardown before the function itself"548 class something:549 modified = True550 finished = 'nope'551 def setup(context):552 something.modified = False553 def teardown1(context):554 something.modified = True555 def teardown2(context):556 something.finished = 'yep'557 @sure.that_with_context(setup, [teardown1, teardown2])558 def something_was_modified(context):559 assert not something.modified560 assert something.finished == 'nope'561 something_was_modified()562 assert something.modified563 assert something.finished == 'yep'564def test_scenario_is_alias_for_context_on_setup_and_teardown():565 "@scenario aliases @that_with_context for setup and teardown"566 from sure import scenario567 def setup(context):568 context.name = "Robert C Martin"569 def teardown(context):570 assert_equals(context.name, "Robert C Martin")571 @scenario([setup], [teardown])572 def robert_is_within_context(context):573 "Robert is within context"574 assert isinstance(context, VariablesBag)575 assert hasattr(context, "name")...
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!!