diff --git a/base/assertions.py b/base/assertions.py index 4023cb8..e9b22db 100644 --- a/base/assertions.py +++ b/base/assertions.py @@ -3,9 +3,43 @@ class Assertions: + @staticmethod def assert_code_status(response: Response, expected_status_code): + """Check status code is expected""" actual_status_code = response.status_code - with allure.step(f"Expected status {expected_status_code}"): + with allure.step(f'Expected status {expected_status_code}'): assert actual_status_code == expected_status_code, \ - f"Unexpected status code. Expected: {expected_status_code}. Actual: {actual_status_code}" + f'Unexpected status code. Expected: {expected_status_code}. Actual: {actual_status_code}' + + @staticmethod + def assert_response_is_json(response: Response): + """Check response has json format""" + with allure.step('Response is in JSON format'): + assert 'application/json' in response.headers.get('Content-Type', ''), \ + 'Error: Response is not in JSON format' + + @staticmethod + def assert_is_not_code_status(response: Response, expected_status_code): + """Check status code is wrong""" + actual_status_code = response.status_code + with allure.step(f'Expected status is not equal {expected_status_code}'): + assert actual_status_code != expected_status_code, \ + f'Unexpected status code. Expected not {expected_status_code}. Actual: {actual_status_code}' + + @staticmethod + def assert_key_name_is_in_response(response: Response, key_name): + """Check if the required key is in the response""" + json_value = response.json() + actual_key = json_value.get(key_name) + with allure.step(f'The key {actual_key} exist'): + assert actual_key is not None, f'The key {actual_key} does not exist' + + @staticmethod + def assert_key_value_is_in_response(response: Response, data, key_name): + """Check if the required value is in the response""" + json_value = response.json() + actual_key = json_value[key_name] + expected_key = data[key_name] + with allure.step(f'Actual key is {expected_key}'): + assert actual_key == expected_key, f'Actual key {expected_key} is not in response' diff --git a/base/logger.py b/base/logger.py index fb2e1a3..285d7a4 100644 --- a/base/logger.py +++ b/base/logger.py @@ -19,7 +19,7 @@ def _write_log_to_file(cls, data: str): @classmethod def add_request(cls, url: str, data: dict, headers: dict, cookies: dict, method: str): - data_to_add = f"{'#'*120}\n\n" + data_to_add = f'***\n\n' data_to_add += f'{str(datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S"))}\n' data_to_add += f"Test name: {os.environ.get('PYTEST_CURRENT_TEST')}\n" data_to_add += f"Request method: {method}\n" diff --git a/base/methods.py b/base/methods.py index 4cfd1b2..c7b23a2 100644 --- a/base/methods.py +++ b/base/methods.py @@ -3,7 +3,7 @@ from base.logger import Logger -class BaseRequests: +class BaseRequests(): @staticmethod def _send(link: str, data: dict, headers: {}, cookies: {}, method: str): diff --git a/data/constant.py b/data/constant.py index b278491..b4481cc 100644 --- a/data/constant.py +++ b/data/constant.py @@ -1,3 +1,4 @@ +import pytest from faker import Faker faker = Faker('En') @@ -5,12 +6,21 @@ class Constant: - BASE_URL = 'https://reqres.in/api/users' - - CREATE_USER = {"name": faker.name(), "job": faker.job()} + BASE_URL = 'https://reqres.in/' + WRONG_URL = 'http://reqres.in/' class StatusCode: STATUS_CODE_200 = 200 STATUS_CODE_201 = 201 STATUS_CODE_404 = 404 + + +class Data: + CREATE_USER = {"name": faker.name(), "job": faker.job()} + NAME_USER = {"name": faker.name()} + JOB_USER = {"job": faker.job()} + CREATE_USER_NAME_NONE ={"name": None, "job": faker.job()} + CREATE_USER_JOB_NONE = {"name": faker.name(), "job": None} + LIST_KEY = ['id', 'createdAt', pytest.param('name', marks=pytest.mark.xfail(reason='Bag')), + pytest.param('job', marks=pytest.mark.xfail(reason='Bag'))] diff --git a/tests/create_users_test.py b/tests/create_users_test.py index 2350b7e..cbdc053 100644 --- a/tests/create_users_test.py +++ b/tests/create_users_test.py @@ -1,15 +1,80 @@ import allure +import pytest + from base.assertions import Assertions -from data.constant import StatusCode, Constant +from data.constant import StatusCode, Data, Constant from base.methods import BaseRequests -@allure.epic("Create User") +@allure.epic('Create User') class TestCreateUser: status = StatusCode + data = Data constant = Constant - @allure.title("POST Создать пользователя") + @allure.title('POST Create user') def test_post_create_user(self): - response = BaseRequests.post(url='', headers=self.constant.CREATE_USER) + response = BaseRequests.post(url='api/users', headers=self.data.CREATE_USER) Assertions.assert_code_status(response, self.status.STATUS_CODE_201) + + @allure.title('Check format is json') + def test_post_create_user_response_is_json(self): + response = BaseRequests.post(url='api/users', headers=self.data.CREATE_USER) + Assertions.assert_response_is_json(response) + + @pytest.mark.parametrize('key_name', data.LIST_KEY) + @allure.title('Check response has key job') + def test_post_create_user_key_name_is_in_response(self, key_name): + headers = self.data.CREATE_USER + response = BaseRequests.post(url='api/users', headers=headers) + Assertions.assert_key_name_is_in_response(response, key_name) + + @pytest.mark.xfail(reason='Bag') + @pytest.mark.parametrize('key_name', data.CREATE_USER) + @allure.title('Check response has key job') + def test_post_create_user_key_value_is_in_response(self, key_name): + headers = self.data.CREATE_USER + response = BaseRequests.post(url='api/users', headers=headers) + Assertions.assert_key_value_is_in_response(response, headers, key_name) + + @allure.title('GET Send request get with body') + def test_get_create_user(self): + response = BaseRequests.get(url='api/users', headers=self.data.CREATE_USER) + Assertions.assert_code_status(response, self.status.STATUS_CODE_200) + + @pytest.mark.xfail(reason='Bag') + @allure.title('POST Создать пользователя без тела запроса') + def test_post_create_user_without_body(self): + response = BaseRequests.post(url='api/users') + Assertions.assert_is_not_code_status(response, self.status.STATUS_CODE_201) + + @pytest.mark.xfail(reason='Bag') + @allure.title('POST Create user without job') + def test_post_create_user_without_job(self): + response = BaseRequests.post(url='api/users', data=self.data.NAME_USER) + print('cod', response.status_code) + Assertions.assert_is_not_code_status(response, self.status.STATUS_CODE_201) + + @pytest.mark.xfail(reason='Bag') + @allure.title('POST Create user without name') + def test_post_create_user_without_name(self): + response = BaseRequests.post(url='api/users', headers=self.data.JOB_USER) + Assertions.assert_is_not_code_status(response, self.status.STATUS_CODE_201) + + @pytest.mark.xfail(reason='Bag') + @allure.title('POST Create user with None job') + def test_post_create_user_with_none_job(self): + response = BaseRequests.post(url='api/users', headers=self.data.CREATE_USER_JOB_NONE) + Assertions.assert_is_not_code_status(response, self.status.STATUS_CODE_201) + + @pytest.mark.xfail(reason='Bag') + @allure.title('POST Create user with None name') + def test_post_create_user_with_none_name(self): + response = BaseRequests.post(url='api/users', headers=self.data.CREATE_USER) + Assertions.assert_is_not_code_status(response, self.status.STATUS_CODE_201) + + @pytest.mark.xfail(reason='Bag') + @allure.title('POST Create user as {}') + def test_post_create_user_as_none(self): + response = BaseRequests.post(url='api/users', headers={}) + Assertions.assert_is_not_code_status(response, self.status.STATUS_CODE_201) diff --git a/tests/list_users_test.py b/tests/list_users_test.py deleted file mode 100644 index e128834..0000000 --- a/tests/list_users_test.py +++ /dev/null @@ -1,19 +0,0 @@ -import allure -from base.assertions import Assertions -from data.constant import StatusCode -from base.methods import BaseRequests - - -@allure.epic("List Users") -class TestListUsers: - status = StatusCode - - @allure.title("GET Получить список пользователей") - def test_get_list_users(self): - response = BaseRequests.get(url='?page=2') - Assertions.assert_code_status(response, self.status.STATUS_CODE_200) - - @allure.title("GET Получить список пользователей неверный эндпоинт") - def test_get_users(self): - response = BaseRequests.get(url='/str') - Assertions.assert_code_status(response, self.status.STATUS_CODE_404) diff --git a/tests/single_user_test.py b/tests/single_user_test.py deleted file mode 100644 index 5c47b21..0000000 --- a/tests/single_user_test.py +++ /dev/null @@ -1,19 +0,0 @@ -import allure -from base.assertions import Assertions -from data.constant import StatusCode -from base.methods import BaseRequests - - -@allure.epic("GET Single User") -class TestAPISingleUser: - status = StatusCode - - @allure.title("GET Получить одного пользователя с id 2") - def test_get_single_user(self): - response = BaseRequests.get(url='2') - Assertions.assert_code_status(response, self.status.STATUS_CODE_200) - - @allure.title("GET Попытка получить несуществующего пользователя с id 23") - def test_get_not_existing_single_user(self): - response = BaseRequests.get(url='/23') - Assertions.assert_code_status(response, self.status.STATUS_CODE_404)