Best Python code snippet using assertpy_python
test_quotes.py
Source:test_quotes.py  
...10                                        "accept": "application/json"11                                    })12        with soft_assertions():13            #Verify status code14            assert_that(response.status_code).is_equal_to(200)15            #Verify response payload16            assert_that(response.json()["data"][0][0]).contains_key("id")17            assert_that(response.json()["data"][0][0]).contains_key("quote")18            assert_that(response.json()["data"][0][0]).contains_key("author")19            assert_that(response.json()["message"]).is_equal_to("Quotes data retrieved successfully")20            #Verify headers21            assert_that(response.headers["content-type"]).is_equal_to("application/json")22            assert_that(response.headers["x-egnyte"]).is_not_empty()23            assert_that(response.headers["server"]).is_equal_to("uvicorn")24            #Verify basic performance sanity25            assert_that(response.elapsed).is_less_than_or_equal_to(timedelta(milliseconds=500))26    def test_02_post_quote_response_response_200(self):27        response = requests.post("http://127.0.0.1:8080/quote/",28                                 headers={29                                     "accept": "application/json"30                                 },31                                 json={32                                     "quote": "In the end, it's not the years in your life that count. It's the life "33                                              "in your years.",34                                     "author": "Abraham Lincoln"35                                 })36        with soft_assertions():37            assert_that(response.status_code).is_equal_to(200)38            # Verify Headers39            assert_that(response.headers["content-type"]).is_equal_to("application/json")40            assert_that(response.headers["server"]).is_equal_to("uvicorn")41            assert_that(response.headers["x-egnyte"]).is_not_empty()42            # Verify Response body!43            assert_that(response.json()["data"][0]).contains_key("id")44            assert_that(response.json()["data"][0]).contains_key("quote")45            assert_that(response.json()["data"][0]).contains_key("author")46            assert_that(response.json()["message"]).is_equal_to("Quote added successfully.")47            # Verify basic performance sanity48            assert_that(response.elapsed).is_less_than_or_equal_to(timedelta(milliseconds=500))49    def test_03_get_quote_with_id_response_200(self):50        # Create first, get second51        create_quote = requests.post("http://127.0.0.1:8080/quote/",52                                     headers={53                                         "accept": "application/json",54                                         "Content-Type": "application/json"55                                     },56                                     json={57                                         "quote": "Life is really simple, but we insist on making it complicated.",58                                         "author": "Abraham Lincoln"59                                     })60        # get created quote it:61        quote_id = create_quote.json()["data"][0]["id"]62        get_quote_with_id = requests.get(f"http://127.0.0.1:8080/quote/{quote_id}",63                                         headers={"accept": "application/json"})64        with soft_assertions():65            assert_that(get_quote_with_id.status_code).is_equal_to(200)66            # assert everything67            #Verify Headers68            assert_that(get_quote_with_id.headers["content-type"]).is_equal_to("application/json")69            assert_that(get_quote_with_id.headers["server"]).is_equal_to("uvicorn")70            assert_that(get_quote_with_id.headers["x-egnyte"]).is_not_empty()71            # validate data72            assert_that(get_quote_with_id.json()["data"][0]).contains_key("id")73            assert_that(get_quote_with_id.json()["data"][0]).contains_key("quote")74            assert_that(get_quote_with_id.json()["data"][0]).contains_key("author")75            assert_that(get_quote_with_id.json()["code"]).is_equal_to(200)76            assert_that(get_quote_with_id.json()["message"]).is_equal_to("Quote data retrieved successfully")77    def test_04_put_quote_with_id_response_200(self):78        # Create first, update second, get updated quote79        create_quote = requests.post("http://127.0.0.1:8080/quote/",80                                     headers={81                                         "accept": "application/json",82                                         "Content-Type": "application/json"83                                     },84                                     json={85                                         "quote": "I never dreamed about success, I worked for it.",86                                         "author": "Estee Lauder"87                                     })88        # get created quote it:89        quote_id = create_quote.json()["data"][0]["id"]90        update_quote_with_id = requests.put(f"http://127.0.0.1:8080/quote/{quote_id}",91                                            headers={92                                                "accept": "application/json",93                                                "Content-Type": "application/json"94                                            },95                                            json={96                                            "quote": "Youâve got to get up every morning with determination if youâre "97                                                     "going to go to bed with satisfaction.",98                                            "author": "George Lorimer."99                                            })100        with soft_assertions():101            assert_that(create_quote.status_code).is_equal_to(200)102            assert_that(update_quote_with_id.status_code).is_equal_to(200)103            # assert everything104            assert_that(update_quote_with_id.json()["data"][0]).contains("update is successful")105            assert_that(update_quote_with_id.json()["message"]).is_equal_to("Quote updated successfully")106            assert_that(update_quote_with_id.headers["content-type"]).is_equal_to("application/json")107            assert_that(update_quote_with_id.headers["server"]).is_equal_to("uvicorn")108            assert_that(update_quote_with_id.headers["x-egnyte"]).is_not_empty()109            # Verify basic performance sanity110            assert_that(update_quote_with_id.elapsed).is_less_than_or_equal_to(timedelta(milliseconds=500))111    def test_05_delete_quote_with_id_response_200(self):112        # Create first, delete second, verify 404 status code when trying to get quote with id again113        create_quote = requests.post("http://127.0.0.1:8080/quote/",114                                     headers={115                                         "accept": "application/json",116                                         "Content-Type": "application/json"117                                     },118                                     json={119                                         "quote": "If you get tired, learn to rest, not to quit.",120                                         "author": "Banksy"121                                     })122        # get created quote it:123        quote_id = create_quote.json()["data"][0]["id"]124        delete_quote = requests.delete(f"http://127.0.0.1:8080/quote/{quote_id}",125                                       headers={"accept": "application/json"})126        get_quote_with_id = requests.get(f"http://127.0.0.1:8080/quote/{quote_id}",127                                         headers={"accept": "application/json"})128        with soft_assertions():129            assert_that(create_quote.status_code).is_equal_to(200)130            assert_that(delete_quote.status_code).is_equal_to(200)131            assert_that(get_quote_with_id.status_code).is_equal_to(404)132            # assert create133            # assert delete134            assert_that(delete_quote.json()["data"][0]).is_equal_to(f"Quote with ID: {quote_id} removed")135            assert_that(delete_quote.json()["message"]).is_equal_to("Quote deleted successfully")136            assert_that(delete_quote.headers["content-type"]).is_equal_to("application/json")137            assert_that(delete_quote.headers["server"]).is_equal_to("uvicorn")138            assert_that(delete_quote.headers["x-egnyte"]).is_not_empty()139            # assert get non-existing quote140            assert_that(get_quote_with_id.json()["detail"]).is_equal_to(f"Quote with {quote_id} doesn't exist.")141            assert_that(get_quote_with_id.headers["content-type"]).is_equal_to("application/json")142            assert_that(get_quote_with_id.headers["server"]).is_equal_to("uvicorn")...test_handler.py
Source:test_handler.py  
...48class TestReportsGetAPI:49    def test_lambda_handler_todoitems(self, fixture_todoitems_event):50        ret = app.lambda_handler(fixture_todoitems_event, "")51        data = json.loads(ret["body"])52        assert_that(ret["statusCode"]).is_equal_to(requests.codes.ok)53        assert_that(data["data"]).is_length(5)54        # assert_that(data["data"]).extracting("name").contains("Feed Fish")55        assert ret["statusCode"] == 20056        assert "message" in ret["body"]57        assert data["message"] == "Success"58    def test_lambda_handler_todoitems_byid(self, fixture_todoitems_byid_event):59        ret = app.lambda_handler(fixture_todoitems_byid_event, "")60        data = json.loads(ret["body"])61        assert_that(ret["statusCode"]).is_equal_to(requests.codes.ok)62        assert_that(data["data"]["name"]).is_equal_to("Feed Fish")63        assert ret["statusCode"] == 20064        assert "message" in ret["body"]65        assert data["message"] == "Success"66    def test_lambda_handler_locations(self, fixture_locations_event):67        ret = app.lambda_handler(fixture_locations_event, "")68        data = json.loads(ret["body"])69        assert_that(ret["statusCode"]).is_equal_to(requests.codes.ok)70        assert_that(data["data"]).is_length(7)71        assert ret["statusCode"] == 20072        assert "message" in ret["body"]73        assert data["message"] == "Success"74    def test_lambda_handler_locations_byid(self, fixture_locations_byid_event):75        ret = app.lambda_handler(fixture_locations_byid_event, "")76        data = json.loads(ret["body"])77        assert_that(ret["statusCode"]).is_equal_to(requests.codes.ok)78        assert_that(data["data"]["street_address"]).is_equal_to("2014 Jabberwocky Rd")79        assert_that(data["data"]["city"]).is_equal_to("Southlake")80        assert_that(data["data"]["state_province"]).is_equal_to("Texas")81        assert ret["statusCode"] == 20082        assert "message" in ret["body"]83        assert data["message"] == "Success"84    def test_lambda_handler_users(self, fixture_users_event):85        ret = app.lambda_handler(fixture_users_event, "")86        data = json.loads(ret["body"])87        assert_that(ret["statusCode"]).is_equal_to(requests.codes.ok)88        assert_that(data["data"]).is_length(4)89        assert ret["statusCode"] == 20090        assert "message" in ret["body"]91        assert data["message"] == "Success"92    def test_lambda_handler_users_byid(self, fixture_users_byid_event):93        ret = app.lambda_handler(fixture_users_byid_event, "")94        data = json.loads(ret["body"])95        assert_that(ret["statusCode"]).is_equal_to(requests.codes.ok)96        assert_that(data["data"]["firstname"]).is_equal_to("Freddy")97        assert_that(data["data"]["lastname"]).is_equal_to("Benson")98        assert_that(data["data"]["username"]).is_equal_to("freddy")99        assert_that(data["data"]["email"]).is_equal_to("fbenson@jackal.com")100        assert ret["statusCode"] == 200101        assert "message" in ret["body"]...test_file_chunk.py
Source:test_file_chunk.py  
...21        os.system("rm test_file")22    def test_init(self):23        with open("test_file", "rb") as f:24            fc = FileChunk(f)25            assert_that(fc.part_size).is_equal_to(64 * 1024 * 1024)26            assert_that(fc.seekable).is_equal_to(True)27            assert_that(fc.hooks).is_equal_to({})28            assert_that(fc.segments).is_equal_to(65536)29    def test_register_hook(self):30        with open("test_file", "rb") as f:31            fc = FileChunk(f, hooks=TEST_HOOKS)32            assert_that(fc.hooks).contains_key("read")33            assert_that(fc.hooks["read"]).is_instance_of(list)34            assert_that(callable(fc.hooks["read"][0])).is_true()35    def test_size(self):36        with open("test_file", "rb") as f:37            fc = FileChunk(f)38            assert_that(fc.size).is_equal_to(1048576000)39    def test_parts(self):40        with open("test_file", "rb") as f:41            fc = FileChunk(f)42            assert_that(fc.parts).is_equal_to(16)43    def test_seek(self):44        with open("test_file", "rb") as f:45            fc = FileChunk(f)46            fc.seek(1, os.SEEK_SET)47            assert_that(fc.fd.tell()).is_equal_to(PART_SIZE)48            fc.seek(0, os.SEEK_END)49            data = fc.read()50            assert_that(len(data)).is_equal_to(0)51    def test_read(self):52        with open("test_file", "rb") as f:53            fc = FileChunk(f)54            data = fc.read()55            assert_that(len(data)).is_equal_to(SEGMENT_SIZE)56    def test_read_part(self):57        with open("test_file", "rb") as f:58            fc = FileChunk(f)59            data = fc.read_part()60            assert_that(len(data)).is_equal_to(PART_SIZE)61    def test_next(self):62        with open("test_file", "rb") as f:63            fc = FileChunk(f)64            index = 065            for _ in fc:66                index += 167            assert_that(index).is_equal_to(16)68if __name__ == "__main__":...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!!
