Skip to content

Commit

Permalink
tests: create_users_test.py
Browse files Browse the repository at this point in the history
refactor: assertions.py, methods.py, constant.py
  • Loading branch information
Nat754 committed Feb 19, 2024
1 parent 9c414a3 commit f1ed891
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 49 deletions.
38 changes: 36 additions & 2 deletions base/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion base/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from base.logger import Logger


class BaseRequests:
class BaseRequests():

@staticmethod
def _send(link: str, data: dict, headers: {}, cookies: {}, method: str):
Expand Down
16 changes: 13 additions & 3 deletions data/constant.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import pytest
from faker import Faker

faker = Faker('En')
Faker.seed()


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'))]
73 changes: 69 additions & 4 deletions tests/create_users_test.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 0 additions & 19 deletions tests/list_users_test.py

This file was deleted.

19 changes: 0 additions & 19 deletions tests/single_user_test.py

This file was deleted.

0 comments on commit f1ed891

Please sign in to comment.