Best Python code snippet using assertpy_python
TestAPI.py
Source:TestAPI.py  
...34        """)35    def test_01_read_root(self):36        r = requests.get(f'{libs.data_website.links.link}')37        print(r.json())38        with soft_assertions():39            assert_that(r.status_code).is_equal_to(200)40            assert_that(r.json()['message']).is_equal_to('Welcome to ÅuczniczQA API Testing app.')41            assert_that(r.json()['message']).is_type_of(str)42    # # TEST CASE 0243    # '''44    # TEST CASE 0245    # Steps:46    # 1. Make the request to API - links in data_website.py47    # 2. Check if HTTP status is 20048    # 3. Check if access token is String49    # '''50    @allure.description("""51            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message52            """)53    @pytest.mark.signup54    def test_01_signup_Happy_Path(self):55        obj = {"fullname": Test_api.data.fullname, "email": Test_api.data.email, "password": Test_api.data.password}56        print(Test_api.data.fullname, " ", Test_api.data.email, " ", Test_api.data.password)57        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_signup, json=obj)58        print(r.json())59        with soft_assertions():60            assert_that(r.status_code).is_equal_to(200)61            assert_that(r.json()).contains('fullname')62            assert_that(r.json()).contains('email')63            assert_that(r.json()).contains('id')64            assert_that(r.json()['fullname']).is_type_of(str)65            assert_that(r.json()['email']).contains('@')66            assert_that(r.json()['id']).is_type_of(str)67            assert_that(r.headers).contains_key('content-length')68            assert_that(r.headers).contains_key('date')69    @pytest.mark.signup70    def test_02_signup_Sad_Path(self):71        fullname = Test_api.data.fullname72        email = Test_api.data.fullname73        password = Test_api.data.password74        print(Test_api.data.fullname)75        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_signup, json={76            'fullname': fullname,77            'email': email,78            'password': password79        })80        print(libs.data_website.links.link + "/" + libs.data_website.links.admin_signup)81        print(r.json())82        with soft_assertions():83            assert_that(r.status_code).is_equal_to(422)84            # assert_that(r.json()["detail"]["msg"]).is_equal_to('value is not a valid email address')85    @allure.description("""86            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message87            """)88    @pytest.mark.signup89    def test_03_signup_Sad_Path(self):90        email = Test_api.data.email91        password = Test_api.data.password92        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_signup, json={93            'email': email,94            'password': password95        })96        with soft_assertions():97            assert_that(r.status_code).is_equal_to(422)98            # assert_that(r.json()['msg'][0]).is_equal_to('field required')99            # assert_that(r.json()['msg'][1]).is_equal_to('value is not a valid email address')100    @allure.description("""101            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message102            """)103    @pytest.mark.signup104    def test_04_signup_Sad_Path(self):105        fullname = Test_api.data.fullname106        email = Test_api.data.email107        password = Test_api.data.password108        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_signup, json={109            'fullname': fullname,110            'email': email.replace("@", ""),111            'password': password112        })113        with soft_assertions():114            assert_that(r.status_code).is_equal_to(422)115    @allure.description("""116            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message117            """)118    @allure.step119    @pytest.mark.signup120    def test_05_signup_Sad_Path(self):121        r = requests.post(f'{libs.data_website.links.link + libs.data_website.links.admin_signup}')122        with soft_assertions():123            assert_that(r.status_code).is_equal_to(422)124    @allure.description("""125            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message126            """)127    @pytest.mark.signup128    def test_06_signup_Happy_Path(self):129        email = Test_api.data.email130        password = Test_api.data.password131        r = requests.post(f'{libs.data_website.links.link + libs.data_website.links.admin_signup}', json={132            'fullname': 123,133            'email': email,134            'password': password135        })136        with soft_assertions():137            assert_that(r.status_code).is_equal_to(200)138    @allure.description("""139            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message140            """)141    @pytest.mark.signup142    def test_07_signup_Sad_Path(self):143        fullname = Test_api.data.fullname144        email = Test_api.data.email145        password = Test_api.data.password146        r = requests.delete(libs.data_website.links.link + libs.data_website.links.admin_signup, json={147            'fullname': fullname,148            'email': email,149            'password': password150        })151        with soft_assertions():152            assert_that(r.status_code).is_equal_to(405)153    @allure.description("""154            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message155            """)156    @pytest.mark.login157    def test_01_login_Happy_Path(self):158        username = Test_api.data.email159        password = Test_api.data.password160        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_login,161                          json={"username": username, "password": password})162        print(username, password)163        with soft_assertions():164            assert_that(r.status_code).is_equal_to(200)165            assert_that(r.json()['access_token']).is_type_of(str)166            assert_that(r.headers).contains_key('content-length')167            assert_that(r.headers).contains_key('date')168        Test_api.access_token = r.json()['access_token']169    @allure.description("""170            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message171            """)172    @pytest.mark.login173    def test_02_login_Sad_Path(self):174        username = Test_api.data.fullname175        password = Test_api.data.password176        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_login,177                          json={"username": username, "password": password})178        print(username, password)179        print(r.json())180        with soft_assertions():181            assert_that(r.status_code).is_equal_to(200)182            assert_that(r.json()).is_equal_to("Incorrect email or password")183            assert_that(r.headers).contains_key('content-length')184            assert_that(r.headers['content-length']).is_type_of(str)185            assert_that(r.headers).contains_key('date')186    @allure.description("""187            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message188            """)189    @pytest.mark.login190    def test_03_login_Sad_Path_2(self):191        username = Test_api.data.fullname192        password = Test_api.data.password193        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_login, json=194        {"username": username, "password": password})195        print(r.json())196        with soft_assertions():197            assert_that(r.status_code).is_equal_to(200)198            assert_that(r.json()).is_equal_to('Incorrect email or password')199            assert_that(r.headers).contains_key('content-length')200            assert_that(r.headers['content-length']).is_type_of(str)201            assert_that(r.headers).contains_key('date')202    @allure.description("""203            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message204            """)205    @pytest.mark.login206    def test_04_login_Sad_Path_2(self):207        password = Test_api.data.password208        r = requests.post(libs.data_website.links.link + libs.data_website.links.admin_login,209                          headers={'accept': 'application/xml'},210                          json={"password": password})211        print(r.json())212        with soft_assertions():213            assert_that(r.status_code).is_equal_to(422)214            assert_that(r.json()['detail'][0]['msg']).is_equal_to('field required')215            assert_that(r.headers).contains_key('content-length')216            assert_that(r.headers['content-length']).is_type_of(str)217            assert_that(r.headers).contains_key('date')218    @allure.description("""219            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message220            """)221    @pytest.mark.student222    def test_05_get_student_list_Happy_Path(self):223        access_token = Test_api.access_token224        r = requests.get(libs.data_website.links.link + libs.data_website.links.get_student,225                         headers={"accept": "application/json",226                                  "Content-Type": "application/json",227                                  "Authorization": f"Bearer {access_token}"})228        print(r.json())229        with soft_assertions():230            assert_that(r.status_code).is_equal_to(200)231            assert_that(r.json()['data'][0][0]).contains_key('id', 'fullname', 'email', 'course_of_study', 'year',232                                                             'GPA')233            assert_that(r.json()['data'][0][0]['id']).is_type_of(str)234            assert_that(r.json()['data'][0][0]['fullname']).is_type_of(str)235            assert_that(r.json()['data'][0][0]['email']).is_type_of(str)236            assert_that(r.json()['data'][0][0]['course_of_study']).is_type_of(str)237            assert_that(r.json()['data'][0][0]['year']).is_type_of(int)238            assert_that(r.json()['data'][0][0]['GPA']).is_type_of(float)239            assert_that(r.json()['data'][0][0]['email']).contains('@')240            assert_that(r.json()['data'][0][0]['year']).is_less_than_or_equal_to(5)241            assert_that(r.json()['data'][0][0]['GPA']).is_less_than_or_equal_to(6)242    @allure.description("""243            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message244            """)245    @pytest.mark.student246    def test_06_create_student_list_Happy_Path(self):247        ID = Test_api.id_of_api248        access_token = Test_api.access_token249        fullname = Test_api.data.fullname250        email = Test_api.data.email251        course_of_study = Test_api.data.course_of_study252        year = Test_api.data.year253        GPA = Test_api.data.gpa254        r = requests.post(libs.data_website.links.link + libs.data_website.links.create_student,255                          headers={"accept": "application/json",256                                   "Content-Type": "application/json",257                                   "Authorization": f"Bearer {access_token}"}, json={258                'fullname': fullname,259                'email': email,260                'course_of_study': course_of_study,261                'year': year,262                'gpa': GPA})263        with soft_assertions():264            assert_that(r.status_code).is_equal_to(200)265        print(r.json())266        self.ID = str(r.json()['data'][0]['id'])267        r2 = requests.get(libs.data_website.links.link + libs.data_website.links.get_student + f"/{ID}",268                          headers={"accept": "application/json",269                                   "Content-Type": "application/json",270                                   "Authorization": f"Bearer {access_token}"})271        print(r2.text)272        with soft_assertions():273            assert_that(r2.status_code).is_equal_to(200)274            # assert_that(r.json()['data'][0][0]['fullname']).is_equal_to(r2.json()['data'][0][0]['fullname'])275            # assert_that(r.json()['data'][0][0]['email']).is_equal_to(r2.json()['data'][0][0]['email'])276            # assert_that(r.json()['data'][0][0]['course_of_study']).is_equal_to(r2.json()['data'][0][0]['course_of_study'])277            # assert_that(r.json()['data'][0][0]['year']).is_equal_to(r2.json()['data'][0][0]['year'])278            # assert_that(r.json()['data'][0][0]['GPA']).is_equal_to(r2.json()['data'][0][0]['GPA'])279            # assert_that(r2.json()['data'][0][0]['id']).is_equal_to(ID)280    @pytest.mark.xfail281    @pytest.mark.student282    @allure.description("""283            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message284            """)285    def test_07_create_student_list_Sad_Path(self):286        access_token = Test_api.access_token287        fullname = Test_api.data.fullname288        email = Test_api.data.email289        course_of_study = Test_api.data.course_of_study290        year = -2022291        GPA = Test_api.data.gpa292        r = requests.post(f'{libs.data_website.links.link + libs.data_website.links.create_student}',293                          headers={"accept": "application/json",294                                   "Content-Type": "application/json",295                                   "Authorization": f"Bearer {access_token}"}, json={296                'fullname': fullname,297                'email': email,298                'course_of_study': course_of_study,299                'year': year,300                'gpa': GPA})301        with soft_assertions():302            assert_that(r.status_code).is_equal_to(403)303    @allure.description("""304            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message305            """)306    @pytest.mark.student307    def test_08_create_student_list_Sad_Path(self):308        access_token = Test_api.access_token309        fullname = Test_api.data.fullname310        email = Test_api.data.email311        year = Test_api.data.year312        GPA = Test_api.data.gpa313        r = requests.post(f'{libs.data_website.links.link + libs.data_website.links.create_student}',314                          headers={"accept": "application/json",315                                   "Content-Type": "application/json",316                                   "Authorization": f"Bearer {access_token}"}, json={317                'fullname': fullname,318                'email': email,319                'year': year,320                'gpa': GPA})321        with soft_assertions():322            assert_that(r.status_code).is_equal_to(422)323    @pytest.mark.student324    @allure.description("""325            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message326            """)327    def test_09_create_student_list_Sad_Path(self):328        access_token = Test_api.access_token329        print(access_token)330        fullname = Test_api.data.fullname331        email = Test_api.data.email332        course_of_study = Test_api.data.course_of_study333        year = " "334        GPA = Test_api.data.gpa335        r = requests.post(f'{libs.data_website.links.link + libs.data_website.links.create_student}',336                          headers={"accept": "application/json",337                                   "Content-Type": "application/json",338                                   "Authorization": f"Bearer {access_token}"}, json={339                'fullname': fullname,340                'email': email,341                'course_of_study': course_of_study,342                'year': year,343                'gpa': GPA})344        with soft_assertions():345            assert_that(r.status_code).is_equal_to(422)346    @pytest.mark.xfail347    @pytest.mark.student348    @allure.description("""349            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message350            """)351    def test_10_create_student_list_Sad_Path(self):352        access_token = Test_api.access_token353        fullname = 123354        email = Test_api.data.email355        course_of_study = Test_api.data.course_of_study356        year = Test_api.data.year357        GPA = Test_api.data.gpa358        r = requests.post(f'{libs.data_website.links.link + libs.data_website.links.create_student}',359                          headers={"accept": "application/json",360                                   "Content-Type": "application/json",361                                   "Authorization": f"Bearer {access_token}"}, json={362                'fullname': fullname,363                'email': email,364                'course_of_study': course_of_study,365                'year': year,366                'gpa': GPA})367        with soft_assertions():368            assert_that(r.status_code).is_equal_to(422)369    @pytest.mark.university370    @allure.description("""371            Testing by GET Request of Reading Root Endpoint with appriopriate HTTP Status Code and message372            """)373    def test_01_create_university_list_Happy_Path(self):374        id_uni = ID_UNIVERSITY()375        access_token = Test_api.access_token376        name = Test_api.data.name377        city = Test_api.data.city378        timezone = Test_api.data.time379        print(type(timezone))380        r = requests.post(libs.data_website.links.link + libs.data_website.links.university,381                          headers={"accept": "application/json",382                                   "Authorization": f"Bearer {access_token}",383                                   "Content-Type": "application/json",384                                   }, json={385                'name': name,386                'city': city,387                'timezone': timezone})388        self.id_uni = r.json()['data'][0][0]['id']389        with soft_assertions():390            assert_that(r.status_code).is_equal_to(200)391        print(r.json())392        r2 = requests.get(libs.data_website.links.link + libs.data_website.links.university / id_uni,393                          headers={"accept": "application/json",394                                   "Content-Type": "application/json",395                                   "Authorization": f"Bearer {access_token}"})396        print(r2.json())397        with soft_assertions():398            assert_that(r.json()['data'][0][0]['fullname']).is_equal_to(r2.json()['data'][0][0]['fullname'])399            assert_that(r.json()['data'][0][0]['email']).is_equal_to(r2.json()['data'][0][0]['email'])400            assert_that(r.json()['data'][0][0]['course_of_study']).is_equal_to(401                r2.json()['data'][0][0]['course_of_study'])402            assert_that(r.json()['data'][0][0]['year']).is_equal_to(r2.json()['data'][0][0]['year'])403            assert_that(r.json()['data'][0][0]['GPA']).is_equal_to(r2.json()['data'][0][0]['GPA'])...test_setgame.py
Source:test_setgame.py  
...8################################################9def test_it_deals_twelve_cards(standard_deck):10    game = Game()11    game.cards = standard_deck12    with soft_assertions():13        assert_that(game.deal()).is_true()14        assert_that(game.board).is_length(12)15        assert_that(game.cards).is_length(69)16def test_it_deals_eighteen_cards(larger_deck):17    game = Game()18    game.cards = larger_deck19    with soft_assertions():20        assert_that(game.deal()).is_true()21        assert_that(game.board).is_length(18)22        assert_that(game.cards).is_length(63)23################################################24# test that it adds players25################################################26def test_it_adds_players():27    game = Game()28    game.add_player("jeff")29    game.add_player("ted")30    game.add_player("ron")31    with soft_assertions():32        assert_that(game.players).contains_key("jeff")33        assert_that(game.players).contains_key("ted")34        assert_that(game.players).contains_key("ron")35def test_it_cant_add_the_same_player_twice():36    game = Game()37    game.add_player("jeff")38    with pytest.raises(RuntimeError):39        game.add_player("jeff")40################################################41# test that a player can find a Set42################################################43def test_handle_player_finds_valid_set(standard_deck):44    game = Game()45    game.cards = standard_deck46    game.add_player("jeff")47    game.deal()48    cards = [49        Card(50            shape=Shape.OVAL,51            shading=Shading.SOLID,52            number=Number.THREE,53            color=Color.GREEN,54        ),55        Card(56            shape=Shape.SQUIGGLE,57            shading=Shading.EMPTY,58            number=Number.ONE,59            color=Color.RED,60        ),61        Card(62            shape=Shape.DIAMOND,63            shading=Shading.STRIPED,64            number=Number.TWO,65            color=Color.BLUE,66        ),67    ]68    with soft_assertions():69        assert_that(game.handle_player_finds_set(cards, player="jeff")).is_true()70        assert_that(game.players["jeff"]).is_length(1)71        assert_that(game.board).is_length(12)72        assert_that(game.cards).is_length(66)73def test_handle_player_finds_set_not_on_board(standard_deck):74    game = Game()75    game.cards = standard_deck76    game.add_player("jeff")77    game.deal()78    cards = [79        Card(80            shape=Shape.SQUIGGLE,81            shading=Shading.SOLID,82            number=Number.ONE,83            color=Color.RED,84        ),85        Card(86            shape=Shape.SQUIGGLE,87            shading=Shading.EMPTY,88            number=Number.ONE,89            color=Color.GREEN,90        ),91        Card(92            shape=Shape.SQUIGGLE,93            shading=Shading.STRIPED,94            number=Number.ONE,95            color=Color.BLUE,96        ),97    ]98    with soft_assertions():99        with pytest.raises(RuntimeError):100            game.handle_player_finds_set(cards, player="jeff")101        assert_that(game.players["jeff"]).is_length(0)102        assert_that(game.board).is_length(12)103        assert_that(game.cards).is_length(69)104def test_handle_player_finds_invalid_set(standard_deck):105    game = Game()106    game.cards = standard_deck107    game.add_player("jeff")108    game.deal()109    cards = [110        Card(111            shape=Shape.DIAMOND,112            shading=Shading.SOLID,113            number=Number.TWO,114            color=Color.GREEN,115        ),116        Card(117            shape=Shape.SQUIGGLE,118            shading=Shading.SOLID,119            number=Number.TWO,120            color=Color.GREEN,121        ),122        Card(123            shape=Shape.SQUIGGLE,124            shading=Shading.SOLID,125            number=Number.ONE,126            color=Color.RED,127        ),128    ]129    with soft_assertions():130        assert_that(game.handle_player_finds_set(cards, player="jeff")).is_false()131        assert_that(game.players["jeff"]).is_length(0)132        assert_that(game.board).is_length(12)...test_get_employee_data.py
Source:test_get_employee_data.py  
...13    context['get_response'] = get_response.json()14    context['get_status_code'] = get_response.status_code15@then('I expect response body to contain all user data')16def validate_all_user_data(context):17    with soft_assertions():18        assert_that(len(context['get_response']['data'])).is_greater_than(1)19        assert_that(len(context['get_response']['data'])).is_equal_to(6)20@then('I expect response body to contain a single user data')21def validate_single_user_data(context):22    with soft_assertions():23        assert_that(context['get_response']['data']['id']).is_equal_to(int(pytest.globalDict['user_id']))24        assert_that(context['get_response']['data']['email']).is_equal_to(Env.get_email())25        assert_that(context['get_response']['data']['first_name']).is_equal_to(Env.get_first_name())26        assert_that(context['get_response']['data']['last_name']).is_equal_to(Env.get_last_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!!
