From cfd30782160529c7c49014abfeef375ef08c497c Mon Sep 17 00:00:00 2001 From: "MH.Dmitrii" Date: Wed, 19 Aug 2026 11:19:23 +0300 Subject: [PATCH 1/2] removing boilerplate target url --- tests/e2e/test_users_crud.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/tests/e2e/test_users_crud.py b/tests/e2e/test_users_crud.py index de95166..7b96ebe 100644 --- a/tests/e2e/test_users_crud.py +++ b/tests/e2e/test_users_crud.py @@ -10,6 +10,10 @@ from tests.e2e.conftest import MySession, e2e_settings @pytest.mark.integra class TestCrud: + async def _target_url(self): + return f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + + @pytest.mark.parametrize("test_user_fixture", [(["admin"], ["admin_group"])], indirect=True) async def test_get_user_by_email_positive(self,test_user_fixture)->None: @@ -19,7 +23,7 @@ class TestCrud: email = new_user_record.get("email") #get email from the fixture in yield sector - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() response = await session.get(f"{target_url}/user/get_by_email",params={"email":email}) response.raise_for_status() response=response.json() @@ -43,7 +47,7 @@ class TestCrud: async def test_get_user_by_email_negative(self, email:str, expected_status:int, auth_fixture:MySession)->None: with allure.step("Get user by email"): - target_url = f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() with pytest.raises(HTTPStatusError) as exc_info: response = await auth_fixture.get(f"{target_url}/user/get_by_email", params={"email": email}) @@ -73,7 +77,7 @@ class TestCrud: with allure.step("Setting target_url"): - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() with allure.step("Create new test user and check for the new user"): @@ -128,7 +132,7 @@ class TestCrud: with allure.step("Preparing data to create new user negative"): user_created=False - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() try: with allure.step("Create new test user and check for the new user"): @@ -165,7 +169,7 @@ class TestCrud: async def test_user_create_delete_soft_positive(self, new_user_record:dict, auth_fixture:MySession)->None: with allure.step("Setting target_url"): - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() with allure.step("Create new test user and check for the new user"): @@ -194,7 +198,7 @@ class TestCrud: with allure.step("Delete user soft"): - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() with pytest.raises(HTTPStatusError) as exc_info: @@ -211,7 +215,7 @@ class TestCrud: with allure.step("Delete user hard"): - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() with pytest.raises(HTTPStatusError) as exc_info: @@ -234,7 +238,7 @@ class TestCrud: with allure.step("Setting target_url"): - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() with allure.step("Update user"): response= await session.patch(f"{target_url}/user/patch_user", json=user_record_to_update, params={"email":new_user_record["email"]}) @@ -273,7 +277,7 @@ class TestCrud: with allure.step("Setting target_url"): - target_url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" + target_url=await self._target_url() with allure.step("Update user"), pytest.raises(expected_exception) as exc_info: -- 2.54.0 From 5dde797a1b717d2ad43317ec533fffe7cdf4099f Mon Sep 17 00:00:00 2001 From: "MH.Dmitrii" Date: Wed, 19 Aug 2026 11:28:29 +0300 Subject: [PATCH 2/2] remove boilerplate target url fix --- tests/e2e/conftest.py | 20 ++++++++------- tests/e2e/test_users_crud.py | 49 ++++++++---------------------------- 2 files changed, 22 insertions(+), 47 deletions(-) diff --git a/tests/e2e/conftest.py b/tests/e2e/conftest.py index 9d112eb..188d463 100644 --- a/tests/e2e/conftest.py +++ b/tests/e2e/conftest.py @@ -28,22 +28,20 @@ class MySession(requests_async.AsyncSession): return await super().request(method, url, **kwargs) @pytest_asyncio.fixture(scope="function") -async def auth_fixture(): +async def auth_fixture(target_url:str): payload = {"username": e2e_settings.TEST_USERNAME, "password": e2e_settings.TEST_PASSWORD} - url = f'http://{e2e_settings.HOST}:{e2e_settings.PORT}' async with MySession() as session: - response = await session.post(url + "/protected/token", data=payload) + response = await session.post(target_url + "/protected/token", data=payload) response.raise_for_status() session.token = response.json()["access_token"] yield session @pytest_asyncio.fixture(scope="function") -async def test_user_fixture(request, auth_fixture: MySession): +async def test_user_fixture(request, auth_fixture: MySession, target_url:str): - url=f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" test_id=uuid4() direct_permission_param, group_param = request.param @@ -57,7 +55,7 @@ async def test_user_fixture(request, auth_fixture: MySession): "direct_permissions":direct_permission_param, "group":group_param } - response=await auth_fixture.post(f"{url}/user/create_user", json=new_user_record) + response=await auth_fixture.post(f"{target_url}/user/create_user", json=new_user_record) response.raise_for_status() @@ -65,11 +63,15 @@ async def test_user_fixture(request, auth_fixture: MySession): payload={"username": new_user_record.get("email"), "password": new_user_record.get("plain_password")} - response = await session.post(url + "/protected/token", data=payload) + response = await session.post(target_url + "/protected/token", data=payload) response.raise_for_status() session.token = response.json()["access_token"] yield (session, new_user_record) - response=await auth_fixture.post(f"{url}/user/delete_user_hard", params={"email":new_user_record.get("email")}) - response.raise_for_status() \ No newline at end of file + response=await auth_fixture.post(f"{target_url}/user/delete_user_hard", params={"email":new_user_record.get("email")}) + response.raise_for_status() + +@pytest_asyncio.fixture() +async def target_url()->str: + return f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" \ No newline at end of file diff --git a/tests/e2e/test_users_crud.py b/tests/e2e/test_users_crud.py index 7b96ebe..84b436e 100644 --- a/tests/e2e/test_users_crud.py +++ b/tests/e2e/test_users_crud.py @@ -4,18 +4,14 @@ import allure import pytest from httpx import HTTPStatusError -from tests.e2e.conftest import MySession, e2e_settings +from tests.e2e.conftest import MySession @pytest.mark.integra class TestCrud: - - async def _target_url(self): - return f"http://{e2e_settings.HOST}:{e2e_settings.PORT}" - - + @pytest.mark.parametrize("test_user_fixture", [(["admin"], ["admin_group"])], indirect=True) - async def test_get_user_by_email_positive(self,test_user_fixture)->None: + async def test_get_user_by_email_positive(self,test_user_fixture, target_url:str)->None: session, new_user_record=test_user_fixture @@ -23,7 +19,6 @@ class TestCrud: email = new_user_record.get("email") #get email from the fixture in yield sector - target_url=await self._target_url() response = await session.get(f"{target_url}/user/get_by_email",params={"email":email}) response.raise_for_status() response=response.json() @@ -44,10 +39,9 @@ class TestCrud: pytest.param("test",404, id="wrong_email"), pytest.param("@d", 404, id="wrong_email") ]) - async def test_get_user_by_email_negative(self, email:str, expected_status:int, auth_fixture:MySession)->None: + async def test_get_user_by_email_negative(self, email:str, expected_status:int, auth_fixture:MySession,target_url:str)->None: with allure.step("Get user by email"): - target_url=await self._target_url() with pytest.raises(HTTPStatusError) as exc_info: response = await auth_fixture.get(f"{target_url}/user/get_by_email", params={"email": email}) @@ -73,12 +67,8 @@ class TestCrud: "direct_permissions":["WRONG_PERMISSIONS"], "group":["WRONG_GROUP"]},id="Positive_wrong_permissions"), ]) - async def test_create_delete_user_hard_positive(self, new_user_record:dict,auth_fixture:MySession)->None: + async def test_create_delete_user_hard_positive(self, new_user_record:dict,auth_fixture:MySession, target_url:str)->None: - with allure.step("Setting target_url"): - - target_url=await self._target_url() - with allure.step("Create new test user and check for the new user"): response=await auth_fixture.post(f"{target_url}/user/create_user", json=new_user_record) @@ -127,12 +117,11 @@ class TestCrud: "plain_password":"Test1234!", },422,id="Not_all_the_fields"), ]) - async def test_create_user_negative(self, new_user_record:dict, auth_fixture:MySession, expected_status:int): + async def test_create_user_negative(self, new_user_record:dict, auth_fixture:MySession, expected_status:int, target_url:str): with allure.step("Preparing data to create new user negative"): user_created=False - target_url=await self._target_url() try: with allure.step("Create new test user and check for the new user"): @@ -166,10 +155,7 @@ class TestCrud: "direct_permissions":[], "group":[]}, id="Positive_user_delete_soft"), ]) - async def test_user_create_delete_soft_positive(self, new_user_record:dict, auth_fixture:MySession)->None: - with allure.step("Setting target_url"): - - target_url=await self._target_url() + async def test_user_create_delete_soft_positive(self, new_user_record:dict, auth_fixture:MySession, target_url:str)->None: with allure.step("Create new test user and check for the new user"): @@ -194,12 +180,10 @@ class TestCrud: @pytest.mark.parametrize("email, expected_status, ",[ pytest.param("Test", 404,id="Wrong_email") ]) - async def test_user_delete_soft_negative(self, email:str,expected_status:int, auth_fixture:MySession)->None: + async def test_user_delete_soft_negative(self, email:str,expected_status:int, auth_fixture:MySession, target_url:str)->None: with allure.step("Delete user soft"): - target_url=await self._target_url() - with pytest.raises(HTTPStatusError) as exc_info: response=await auth_fixture.post(f"{target_url}/user/delete_user_soft", params={"email":email}) @@ -211,12 +195,10 @@ class TestCrud: @pytest.mark.parametrize("email, expected_status, ",[ pytest.param("Test", 404,id="Wrong_email") ]) - async def test_user_delete_hard_negative(self, email:str,expected_status:int, auth_fixture:MySession)->None: + async def test_user_delete_hard_negative(self, email:str,expected_status:int, auth_fixture:MySession, target_url:str)->None: with allure.step("Delete user hard"): - target_url=await self._target_url() - with pytest.raises(HTTPStatusError) as exc_info: response=await auth_fixture.post(f"{target_url}/user/delete_user_soft", params={"email":email}) @@ -225,21 +207,16 @@ class TestCrud: assert exc_info.value.response.status_code == expected_status - @pytest.mark.parametrize("test_user_fixture", [ (["admin"], ["admin_group"]) ], indirect=True) @pytest.mark.parametrize("user_record_to_update",[ pytest.param({"first_name": "Test_New"},id="Positive_user_update_partially") ]) - async def test_user_update_partially_positive(self, test_user_fixture, user_record_to_update:dict)->None: + async def test_user_update_partially_positive(self, test_user_fixture, user_record_to_update:dict, target_url:str)->None: session, new_user_record=test_user_fixture - with allure.step("Setting target_url"): - - target_url=await self._target_url() - with allure.step("Update user"): response= await session.patch(f"{target_url}/user/patch_user", json=user_record_to_update, params={"email":new_user_record["email"]}) response.raise_for_status() @@ -271,14 +248,10 @@ class TestCrud: pytest.param({"email": "Wrong_email"},HTTPStatusError,422,id="Wrong_email"), pytest.param({},HTTPStatusError, 400,id="Negative_user_update_nothing") ]) - async def test_user_update_partially_negative(self, test_user_fixture, user_record_to_update:dict, expected_exception, expected_status:int)->None: + async def test_user_update_partially_negative(self, test_user_fixture, user_record_to_update:dict, expected_exception, expected_status:int, target_url:str)->None: session, new_user_record=test_user_fixture - with allure.step("Setting target_url"): - - target_url=await self._target_url() - with allure.step("Update user"), pytest.raises(expected_exception) as exc_info: response= await session.patch(f"{target_url}/user/patch_user", json=user_record_to_update, params={"email":new_user_record["email"]}) -- 2.54.0