import os
import platform
from behave.model import Scenario
from conditions import load_env, CURRENT_DIR
from steps.api.utils import CloudContextHandler, BehaveContext, Context
[docs]
CLOUD_ONLY_TAG = "cloud_only"
[docs]
PCVL_SERVER_ONLY_TAG = "pcvl_server"
[docs]
def generate_allure_environnement(context: Context):
my_dict = {
"os_platform": platform.system(),
"os_release": platform.freedesktop_os_release()['NAME'],
"os_version": platform.freedesktop_os_release()['VERSION'],
"python_version": platform.python_version()
}
apis = [context.apis["cloud"]]
if apis[0].swagger_file != context.apis["account"].swagger_file:
apis.append(context.apis["account"])
for api in apis:
name = api.schemas['info']['title'].replace(' ', '_').lower()
my_dict[f"{name}_version"] = api.schemas['info']['version']
my_dict[f"{name}_url"] = api.url
with open(os.path.join(context.output_dir, 'environment.properties'), 'w') as environment_file:
for key, value in my_dict.items():
environment_file.write(f"{key} = {value}\n")
[docs]
def before_all(context: BehaveContext):
cch = CloudContextHandler(context)
if '_config' in context.__dict__:
context.output_dir = os.path.join(os.path.dirname(CURRENT_DIR), context._config.outfiles[0])
config = load_env()
cch.initialize(config,
output_dir=context.output_dir,
api_file=os.path.join(CURRENT_DIR, "api.csv"),
users_file=os.path.join(CURRENT_DIR, "users.csv"))
context.pictures_dir = os.path.join(context.output_dir, 'pictures')
if not os.path.exists(context.pictures_dir):
os.mkdir(context.pictures_dir)
context.in_scenario = True
[docs]
def before_scenario(context: Context, scenario: Scenario):
if CLOUD_ONLY_TAG in scenario.effective_tags and context.light_version:
reason = f"Marked by @{CLOUD_ONLY_TAG}"
context.logger.info(f"Skipping scenario: {scenario.name}. {reason}")
scenario.skip(reason)
elif PCVL_SERVER_ONLY_TAG in scenario.effective_tags and not context.light_version:
reason = f"Marked by @{PCVL_SERVER_ONLY_TAG}"
context.logger.info(f"Skipping scenario: {scenario.name}. {reason}")
scenario.skip(reason)
context.logger.info(f"Starting scenario: {scenario.name}")
if "runner.continue_after_failed_step" in scenario.effective_tags:
Scenario.continue_after_failed_step = True
else:
Scenario.continue_after_failed_step = False
# erasing temp variables
context.cloud.temp = {}
context.request = None
context.in_scenario = True
[docs]
def after_scenario(context: Context, scenario: Scenario):
context.logger.info(f"Finish scenario: {scenario.name}")
[docs]
def after_all(context: Context):
context.in_scenario = False
# cleanup after tests run
try:
context.behave_driver.quit()
except:
pass
context.cloud.clear(context)
generate_allure_environnement(context)
if __name__ == "__main__":
from behave import runner
from unittest.mock import Mock
r.config = Mock()
r.config.verbose = False
r.config.outfiles = [""]
context = runner.Context(r)
before_all(context)
after_all(context)