Best Python code snippet using sure_python
test_assertion_builder.py
Source:test_assertion_builder.py
...34 assert this(4).should.equal(2 + 2)35 assert this(time).should_not.equal(datetime.now())36 def opposite():37 assert this(4).should.equal(8)38 def opposite_not():39 assert this(4).should_not.equal(4)40 expect(opposite).when.called.to.throw(AssertionError)41 expect(opposite).when.called.to.throw("X is 4 whereas Y is 8")42 expect(opposite_not).when.called.to.throw(AssertionError)43 expect(opposite_not).when.called.to.throw(44 "4 should differ to 4, but is the same thing")45def test_2_within_0a2():46 ("this(1).should.be.within(0, 2)")47 assert this(1).should.be.within(0, 2)48 assert this(4).should_not.be.within(0, 2)49 def opposite():50 assert this(1).should.be.within(2, 4)51 def opposite_not():52 assert this(1).should_not.be.within(0, 2)53 expect(opposite).when.called.to.throw(AssertionError)54 expect(opposite).when.called.to.throw("expected 1 to be within 2 and 4")55 expect(opposite_not).when.called.to.throw(AssertionError)56 expect(opposite_not).when.called.to.throw("expected 1 to NOT be within 0 and 2")57def test_true_be_ok():58 ("this(True).should.be.ok")59 assert this(True).should.be.ok60 assert this(False).should_not.be.ok61 def opposite():62 assert this(False).should.be.ok63 def opposite_not():64 assert this(True).should_not.be.ok65 expect(opposite).when.called.to.throw(AssertionError)66 expect(opposite).when.called.to.throw("expected `False` to be truthy")67 expect(opposite_not).when.called.to.throw(AssertionError)68 expect(opposite_not).when.called.to.throw("expected `True` to be falsy")69def test_false_be_falsy():70 ("this(False).should.be.false")71 assert this(False).should.be.falsy72 assert this(True).should_not.be.falsy73 def opposite():74 assert this(True).should.be.falsy75 def opposite_not():76 assert this(False).should_not.be.falsy77 expect(opposite).when.called.to.throw(AssertionError)78 expect(opposite).when.called.to.throw("expected `True` to be falsy")79 expect(opposite_not).when.called.to.throw(AssertionError)80 expect(opposite_not).when.called.to.throw("expected `False` to be truthy")81def test_none():82 ("this(None).should.be.none")83 assert this(None).should.be.none84 assert this(not None).should_not.be.none85 def opposite():86 assert this("cool").should.be.none87 def opposite_not():88 assert this(None).should_not.be.none89 expect(opposite).when.called.to.throw(AssertionError)90 expect(opposite).when.called.to.throw("expected `cool` to be None")91 expect(opposite_not).when.called.to.throw(AssertionError)92 expect(opposite_not).when.called.to.throw("expected `None` to not be None")93def test_should_be_a():94 ("this(None).should.be.none")95 assert this(1).should.be.an(int)96 assert this([]).should.be.a('collections.Iterable')97 assert this({}).should_not.be.a(list)98 def opposite():99 assert this(1).should_not.be.an(int)100 def opposite_not():101 assert this([]).should_not.be.a('list')102 expect(opposite).when.called.to.throw(AssertionError)103 expect(opposite).when.called.to.throw("expected `1` to not be an int")104 expect(opposite_not).when.called.to.throw(AssertionError)105 expect(opposite_not).when.called.to.throw("expected `[]` to not be a list")106def test_should_be_callable():107 ("this(function).should.be.callable")108 assert this(lambda: None).should.be.callable109 assert this("aa").should_not.be.callable110 def opposite():111 assert this("foo").should.be.callable112 def opposite_not():113 assert this(opposite).should_not.be.callable114 expect(opposite).when.called.to.throw(AssertionError)115 expect(opposite).when.called.to.throw(compat_repr(116 "expected 'foo' to be callable"))117 expect(opposite_not).when.called.to.throw(AssertionError)118 expect(opposite_not).when.called.to.throw(119 "expected `{0}` to not be callable but it is".format(repr(opposite)))120def test_iterable_should_be_empty():121 ("this(iterable).should.be.empty")122 assert this([]).should.be.empty123 assert this([1, 2, 3]).should_not.be.empty124 def opposite():125 assert this([3, 2, 1]).should.be.empty126 def opposite_not():127 assert this({}).should_not.be.empty128 expect(opposite).when.called.to.throw(AssertionError)129 expect(opposite).when.called.to.throw(130 "expected `[3, 2, 1]` to be empty but it has 3 items")131 expect(opposite_not).when.called.to.throw(AssertionError)132 expect(opposite_not).when.called.to.throw("expected `{}` to not be empty")133def test_iterable_should_have_length_of():134 ("this(iterable).should.have.length_of(N)")135 assert this({'foo': 'bar', 'a': 'b'}).should.have.length_of(2)136 assert this([1, 2, 3]).should_not.have.length_of(4)137 def opposite():138 assert this(('foo', 'bar', 'a', 'b')).should.have.length_of(1)139 def opposite_not():140 assert this([1, 2, 3]).should_not.have.length_of(3)141 expect(opposite).when.called.to.throw(AssertionError)142 expect(opposite).when.called.to.throw(compat_repr(143 "the length of ('foo', 'bar', 'a', 'b') should be 1, but is 4"))144 expect(opposite_not).when.called.to.throw(AssertionError)145 expect(opposite_not).when.called.to.throw(146 "the length of [1, 2, 3] should not be 3")147def test_greater_than():148 ("this(X).should.be.greater_than(Y)")149 assert this(5).should.be.greater_than(4)150 assert this(1).should_not.be.greater_than(2)151 def opposite():152 assert this(4).should.be.greater_than(5)153 def opposite_not():154 assert this(2).should_not.be.greater_than(1)155 expect(opposite).when.called.to.throw(AssertionError)156 expect(opposite).when.called.to.throw(157 "expected `4` to be greater than `5`")158 expect(opposite_not).when.called.to.throw(AssertionError)159 expect(opposite_not).when.called.to.throw(160 "expected `2` to not be greater than `1`")161def test_greater_than_or_equal_to():162 ("this(X).should.be.greater_than_or_equal_to(Y)")163 assert this(4).should.be.greater_than_or_equal_to(4)164 assert this(1).should_not.be.greater_than_or_equal_to(2)165 def opposite():166 assert this(4).should.be.greater_than_or_equal_to(5)167 def opposite_not():168 assert this(2).should_not.be.greater_than_or_equal_to(1)169 expect(opposite).when.called.to.throw(AssertionError)170 expect(opposite).when.called.to.throw(171 "expected `4` to be greater than or equal to `5`")172 expect(opposite_not).when.called.to.throw(AssertionError)173 expect(opposite_not).when.called.to.throw(174 "expected `2` to not be greater than or equal to `1`")175def test_lower_than():176 ("this(X).should.be.lower_than(Y)")177 assert this(4).should.be.lower_than(5)178 assert this(2).should_not.be.lower_than(1)179 def opposite():180 assert this(5).should.be.lower_than(4)181 def opposite_not():182 assert this(1).should_not.be.lower_than(2)183 expect(opposite).when.called.to.throw(AssertionError)184 expect(opposite).when.called.to.throw(185 "expected `5` to be lower than `4`")186 expect(opposite_not).when.called.to.throw(AssertionError)187 expect(opposite_not).when.called.to.throw(188 "expected `1` to not be lower than `2`")189def test_lower_than_or_equal_to():190 ("this(X).should.be.lower_than_or_equal_to(Y)")191 assert this(5).should.be.lower_than_or_equal_to(5)192 assert this(2).should_not.be.lower_than_or_equal_to(1)193 def opposite():194 assert this(5).should.be.lower_than_or_equal_to(4)195 def opposite_not():196 assert this(1).should_not.be.lower_than_or_equal_to(2)197 expect(opposite).when.called.to.throw(AssertionError)198 expect(opposite).when.called.to.throw(199 "expected `5` to be lower than or equal to `4`")200 expect(opposite_not).when.called.to.throw(AssertionError)201 expect(opposite_not).when.called.to.throw(202 "expected `1` to not be lower than or equal to `2`")203def test_be():204 ("this(X).should.be(X) when X is a reference to the same object")205 d1 = {}206 d2 = d1207 d3 = {}208 assert isinstance(this(d2).should.be(d1), bool)209 assert this(d2).should.be(d1)210 assert this(d3).should_not.be(d1)211 def wrong_should():212 return this(d3).should.be(d1)213 def wrong_should_not():214 return this(d2).should_not.be(d1)215 wrong_should_not.when.called.should.throw(216 AssertionError,217 '{} should not be the same object as {}, but it is',218 )219 wrong_should.when.called.should.throw(220 AssertionError,221 '{} should be the same object as {}, but it is not',222 )223def test_have_property():224 ("this(instance).should.have.property(property_name)")225 class Person(object):226 name = "John Doe"227 def __repr__(self):228 return r"Person()"229 jay = Person()230 assert this(jay).should.have.property("name")231 assert this(jay).should_not.have.property("age")232 def opposite():233 assert this(jay).should_not.have.property("name")234 def opposite_not():235 assert this(jay).should.have.property("age")236 expect(opposite).when.called.to.throw(AssertionError)237 expect(opposite).when.called.to.throw(compat_repr(238 "Person() should not have the property `name`, but it is 'John Doe'"))239 expect(opposite_not).when.called.to.throw(AssertionError)240 expect(opposite_not).when.called.to.throw(241 "Person() should have the property `age` but does not")242def test_have_property_with_value():243 ("this(instance).should.have.property(property_name).being or "244 ".with_value should allow chain up")245 class Person(object):246 name = "John Doe"247 def __repr__(self):248 return r"Person()"249 jay = Person()250 assert this(jay).should.have.property("name").being.equal("John Doe")251 assert this(jay).should.have.property("name").not_being.equal("Foo")252 def opposite():253 assert this(jay).should.have.property("name").not_being.equal(254 "John Doe")255 def opposite_not():256 assert this(jay).should.have.property("name").being.equal(257 "Foo")258 expect(opposite).when.called.to.throw(AssertionError)259 expect(opposite).when.called.to.throw(compat_repr(260 "'John Doe' should differ to 'John Doe', but is the same thing"))261 expect(opposite_not).when.called.to.throw(AssertionError)262 expect(opposite_not).when.called.to.throw(compat_repr(263 "X is 'John Doe' whereas Y is 'Foo'"))264def test_have_key():265 ("this(dictionary).should.have.key(key_name)")266 jay = {'name': "John Doe"}267 assert this(jay).should.have.key("name")268 assert this(jay).should_not.have.key("age")269 def opposite():270 assert this(jay).should_not.have.key("name")271 def opposite_not():272 assert this(jay).should.have.key("age")273 expect(opposite).when.called.to.throw(AssertionError)274 expect(opposite).when.called.to.throw(compat_repr(275 "{'name': 'John Doe'} should not have the key `name`, "276 "but it is 'John Doe'"))277 expect(opposite_not).when.called.to.throw(AssertionError)278 expect(opposite_not).when.called.to.throw(compat_repr(279 "{'name': 'John Doe'} should have the key `age` but does not"))280def test_have_key_with_value():281 ("this(dictionary).should.have.key(key_name).being or "282 ".with_value should allow chain up")283 jay = dict(name="John Doe")284 assert this(jay).should.have.key("name").being.equal("John Doe")285 assert this(jay).should.have.key("name").not_being.equal("Foo")286 def opposite():287 assert this(jay).should.have.key("name").not_being.equal(288 "John Doe")289 def opposite_not():290 assert this(jay).should.have.key("name").being.equal(291 "Foo")292 expect(opposite).when.called.to.throw(AssertionError)293 expect(opposite).when.called.to.throw(compat_repr(294 "'John Doe' should differ to 'John Doe', but is the same thing"))295 expect(opposite_not).when.called.to.throw(AssertionError)296 expect(opposite_not).when.called.to.throw(compat_repr(297 "X is 'John Doe' whereas Y is 'Foo'"))298def test_look_like():299 ("this(' aa \n ').should.look_like('aa')")300 assert this(' \n aa \n ').should.look_like('AA')301 assert this(' \n bb \n ').should_not.look_like('aa')302 def opposite():303 assert this('\n aa \n').should.look_like('bb')304 def opposite_not():305 assert this('\n aa \n').should_not.look_like('aa')306 expect(opposite).when.called.to.throw(AssertionError)307 expect(opposite).when.called.to.throw(compat_repr(r"'\n aa \n' does not look like 'bb'"))308 expect(opposite_not).when.called.to.throw(AssertionError)309 expect(opposite_not).when.called.to.throw(compat_repr(r"'\n aa \n' should not look like 'aa' but does"))310def test_equal_with_repr_of_complex_types_and_unicode():311 ("test usage of repr() inside expect(complex1).to.equal(complex2)")312 class Y(object):313 def __init__(self, x):314 self.x = x315 def __repr__(self):316 if PY3:317 # PY3K should return the regular (unicode) string318 return self.x319 else:320 return self.x.encode('utf-8')321 def __eq__(self, other):322 return self.x == other.x323 y1 = dict(324 a=2,325 b=Y('Gabriel Falcão'),326 c='Foo',327 )328 expect(y1).to.equal(dict(329 a=2,330 b=Y('Gabriel Falcão'),331 c='Foo',332 ))333def test_equal_with_repr_of_complex_types_and_repr():334 ("test usage of repr() inside expect(complex1).to.equal(complex2)")335 class Y(object):336 def __init__(self, x):337 self.x = x338 def __repr__(self):339 if PY3:340 # PY3K should return the regular (unicode) string341 return self.x342 else:343 return self.x.encode('utf-8')344 def __eq__(self, other):345 return self.x == other.x346 y1 = {347 'a': 2,348 'b': Y('Gabriel Falcão'),349 'c': 'Foo',350 }351 expect(y1).to.equal({352 'a': 2,353 'b': Y('Gabriel Falcão'),354 'c': 'Foo',355 })356 expect(y1).to_not.equal({357 'a': 2,358 'b': Y('Gabriel Falçao'),359 'c': 'Foo',360 })361 def opposite():362 expect(y1).to.equal({363 'a': 2,364 'b': Y('Gabriel Falçao'),365 'c': 'Foo',366 })367 def opposite_not():368 expect(y1).to_not.equal({369 'a': 2,370 'b': Y('Gabriel Falcão'),371 'c': 'Foo',372 })373 expect(opposite).when.called.to.throw(AssertionError)374 expect(opposite).when.called.to.throw(compat_repr("X['b'] != Y['b']"))375 expect(opposite_not).when.called.to.throw(AssertionError)376 expect(opposite_not).when.called.to.throw(compat_repr(377 "{'a': 2, 'b': Gabriel Falcão, 'c': 'Foo'} should differ to {'a': 2, 'b': Gabriel Falcão, 'c': 'Foo'}, but is the same thing"))378def test_match_regex():379 ("expect('some string').to.match(r'\w{4} \w{6}') matches regex")380 assert this("some string").should.match(r"\w{4} \w{6}")381 assert this("some string").should_not.match(r"^\d*$")382 def opposite():383 assert this("some string").should.match(r"\d{2} \d{4}")384 def opposite_not():385 assert this("some string").should_not.match(r"some string")386 expect(opposite).when.called.to.throw(387 AssertionError,388 "'some string' doesn't match the regular expression /\d{2} \d{4}/")389 expect(opposite_not).when.called.to.throw(AssertionError)390 expect(opposite_not).when.called.to.throw(391 "'some string' should not match the regular expression /some string/")392def test_match_contain():393 ("expect('some string').to.contain('tri')")394 assert this("some string").should.contain("tri")395 assert this("some string").should_not.contain('foo')396 def opposite():397 assert this("some string").should.contain("bar")398 def opposite_not():399 assert this("some string").should_not.contain(r"string")400 expect(opposite).when.called.to.throw(AssertionError)401 if PY3:402 expect(opposite).when.called.to.throw(403 "'bar' should be in 'some string'")404 else:405 expect(opposite).when.called.to.throw(406 "u'bar' should be in u'some string'")407 expect(opposite_not).when.called.to.throw(AssertionError)408 if PY3:409 expect(opposite_not).when.called.to.throw(410 "'string' should NOT be in 'some string'")411 else:412 expect(opposite_not).when.called.to.throw(413 "u'string' should NOT be in u'some string'")414def test_catching_exceptions():415 # Given that I have a function that raises an exceptiont that does *not*416 # inherit from the `Exception` class417 def blah():418 raise SystemExit(2)419 # When I call it testing which exception it's raising, Then it should be420 # successful421 expect(blah).when.called_with().should.throw(SystemExit)422def test_catching_exceptions_with_params():423 # Given that I have a function that raises an exceptiont that does *not*424 # inherit from the `Exception` class425 def blah(foo):426 raise SystemExit(2)427 # When I call it testing which exception it's raising, Then it should be428 # successful429 expect(blah).when.called_with(0).should.throw(SystemExit)430def test_success_with_params():431 def blah(foo):432 pass433 expect(blah).when.called_with(0).should_not.throw(TypeError)434def test_success_with_params_exception():435 def blah():436 pass437 expect(blah).when.called_with(0).should.throw(TypeError)438def test_throw_matching_regex():439 def blah(num):440 if num == 1:441 msg = 'this message'442 else:443 msg = 'another thing'444 raise ValueError(msg)445 expect(blah).when.called_with(1).should.throw(ValueError, 'this message')446 expect(blah).when.called_with(1).should.throw(re.compile(r'(this message|another thing)'))447 expect(blah).when.called_with(2).should.throw(ValueError, 'another thing')448 expect(blah).when.called_with(2).should.throw(ValueError, re.compile(r'(this message|another thing)'))449 try:450 expect(blah).when.called_with(1).should.throw(re.compile(r'invalid regex'))451 raise RuntimeError('should not have reached here')452 except AssertionError as e:453 expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 633]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'")454 try:455 expect(blah).when.called_with(1).should.throw(ValueError, re.compile(r'invalid regex'))456 raise RuntimeError('should not have reached here')457 except AssertionError as e:458 expect(str(e)).to.equal("When calling 'blah [tests/test_assertion_builder.py line 633]' the exception message does not match. Expected to match regex: u'invalid regex'\n against:\n u'this message'")459def test_should_not_be_different():460 ("'something'.should_not.be.different('SOMETHING'.lower())")461 part1 = '''<root>462 <a-tag with-attribute="one">AND A VALUE</a-tag>463</root>'''464 part2 = '''<root>465 <a-tag with-attribute="two">AND A VALUE</a-tag>466</root>'''467 assert this(part1).should.be.different_of(part2)468 assert this(part2).should_not.be.different_of(part2)469 def opposite():470 assert this(part2).should.be.different_of(part2)471 def opposite_not():472 assert this(part1).should_not.be.different_of(part2)473 expect(opposite).when.called.to.throw(AssertionError)474 expect(opposite).when.called.to.throw('''<root>475 <a-tag with-attribute="two">AND A VALUE</a-tag>476</root> should be different of <root>477 <a-tag with-attribute="two">AND A VALUE</a-tag>478</root>''')479 expect(opposite_not).when.called.to.throw(AssertionError)480 expect(opposite_not).when.called.to.throw('''Difference:481 <root>482- <a-tag with-attribute="one">AND A VALUE</a-tag>483? --484+ <a-tag with-attribute="two">AND A VALUE</a-tag>485? ++...
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!!