Best Python code snippet using assertpy_python

test_dict.py

Source:test_dict.py

**...****67** assert_that({'a': 1, 'b': 2, 'c': 3}).contains('a', 'x', 'z')**68** fail('should have raised error')**69** except AssertionError as ex:**70** assert_that(str(ex)).contains("to contain keys <'a', 'x', 'z'>, but did not contain keys <'x', 'z'>.")**71**def test_contains_multi_item_single_failure():**72** try:**73** assert_that({'a': 1, 'b': 2, 'c': 3}).contains('a', 'b', 'z')**74** fail('should have raised error')**75** except AssertionError as ex:**76** assert_that(str(ex)).contains("to contain keys <'a', 'b', 'z'>, but did not contain keys <z>.")**77**def test_contains_only():**78** assert_that({'a': 1, 'b': 2, 'c': 3}).contains_only('a', 'b', 'c')**79** assert_that(set(['a', 'b', 'c'])).contains_only('a', 'b', 'c')**80**def test_contains_only_failure():**81** try:**82** assert_that({'a': 1, 'b': 2}).contains_only('a', 'x')**83** fail('should have raised error')**84** except AssertionError as ex:**85** assert_that(str(ex)).contains("to contain only <'a', 'x'>, but did contain <b>.")**...**

test_list.py

Source:test_list.py

**...****75** assert_that(['a', 'b', 'c']).contains('a', 'x', 'z')**76** fail('should have raised error')**77** except AssertionError as ex:**78** assert_that(str(ex)).is_equal_to("Expected <['a', 'b', 'c']> to contain items <'a', 'x', 'z'>, but did not contain <'x', 'z'>.")**79**def test_contains_multi_item_single_failure():**80** try:**81** assert_that(['a', 'b', 'c']).contains('a', 'b', 'z')**82** fail('should have raised error')**83** except AssertionError as ex:**84** assert_that(str(ex)).is_equal_to("Expected <['a', 'b', 'c']> to contain items <'a', 'b', 'z'>, but did not contain <z>.")**85**def test_does_not_contain():**86** assert_that(['a', 'b', 'c']).does_not_contain('x')**87** assert_that(['a', 'b', 'c']).does_not_contain('x', 'y')**88** assert_that((1, 2, 3, 4)).does_not_contain(5)**89** assert_that((1, 2, 3, 4)).does_not_contain(5, 6)**90** assert_that({'a': 1, 'b': 2, 'c': 3}).does_not_contain('x')**91** assert_that({'a': 1, 'b': 2, 'c': 3}).does_not_contain('x', 'y')**92** assert_that(set(['a', 'b', 'c'])).does_not_contain('x')**93** assert_that(set(['a', 'b', 'c'])).does_not_contain('x', 'y')**...**

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.

- JUnit Tutorial
- TestNG Tutorial
- Webdriver Tutorial
- WebDriverIO Tutorial
- Protractor Tutorial
- Selenium 4 Tutorial
- Jenkins Tutorial
- NUnit Tutorial
- Jest Tutorial
- Playwright Tutorial
- Cypress Tutorial
- PyTest Tutorial

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!!