-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
93 lines (78 loc) · 2.73 KB
/
conftest.py
File metadata and controls
93 lines (78 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import logging
import os
import allure
import pytest
import requests
@pytest.fixture(autouse=True)
def logger(request):
# Create logs directory & logfile, if missing
test_name = request.node.name
# log_file = os.path.join(os.getcwd(), "logs", f"{test_name}.log")
# log_file = os.path.join(os.getcwd(), "logs", "api.log")
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
log_file = os.path.join(log_dir, f"{test_name}.log")
# os.makedirs(os.path.dirname(log_file), exist_ok=True)
# Replace default logger at session start
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# File handler (new file each run)
fh = logging.FileHandler(log_file, mode="w", encoding="utf-8")
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"\n%(asctime)s - %(levelname)s - %(name)s : %(message)s",
datefmt="%m-%d-%Y %I:%M:%S %p"
)
fh.setFormatter(formatter)
# Stream handler (console)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
sh.setFormatter(formatter)
logger.handlers.clear()
logger.addHandler(fh)
logger.addHandler(sh)
logger.debug(f"*** [ Logger Initialized For Test - {test_name} ] ***")
request.node.logger = logger
yield logger
sh.flush()
logger.removeHandler(sh)
sh.close()
fh.flush()
with open(log_file, "r") as f:
allure.attach(f.read(), name=f"{test_name}.log", attachment_type=allure.attachment_type.TEXT)
logger.removeHandler(fh)
fh.close()
@pytest.fixture(autouse=True)
def api_cfg():
return {
"echos": {
"base_url": "https://postman-echo.com/",
"get_endpoint": "get",
},
"fkstore": {
"base_url": "https://api.escuelajs.co",
"user_endpoint": "/api/v1/users/",
"file_endpoint": "/api/v1/files/"
},
"reqres": {
"BASE_URL": "https://reqres.in/",
"COMMON_ENDPOINT": "api/users/",
"API_KEY": "reqres-free-v1"
}
}
@pytest.fixture(autouse=True)
def test_info_logging(request, logger):
logger.info(f" [ START OF TEST - {request.node.nodeid} ]")
yield
logger.info(f" [ END OF TEST - {request.node.nodeid} ]")
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
# if rep.when == "call" or rep.when == "teardown":#and rep.failed:
if rep.when == "teardown" and (rep.passed or rep.failed):
log_path = os.path.join("logs", f"{item.name}.log")
if os.path.exists(log_path):
with open(log_path, "r") as f:
allure.attach(f.read(), name=f"{item.name}.log", attachment_type=allure.attachment_type.TEXT)