add env vars and conftest

This commit is contained in:
Florin Barbu 2024-06-03 22:47:18 +03:00
parent 198c7083b3
commit 41c350b82d
No known key found for this signature in database
GPG Key ID: 593D6DBC6D9E5095
3 changed files with 76 additions and 3 deletions

19
src/env_vars.py Normal file
View File

@ -0,0 +1,19 @@
import os
from dotenv import load_dotenv
load_dotenv() # This will load environment variables from a .env file if it exists
def get_env_var(var_name, default=None):
env_var = os.getenv(var_name, default)
if env_var in [None, ""]:
print(f"{var_name} is not set; using default value: {default}")
env_var = default
print(f"{var_name}: {env_var}")
return env_var
# Configuration constants. Need to be upercase to appear in reports
NUM_MESSAGES = get_env_var("NUM_MESSAGES", 50)
DELAY_BETWEEN_MESSAGES = get_env_var("DELAY_BETWEEN_MESSAGES", 2)
RUNNING_IN_CI = get_env_var("CI")

53
tests/conftest.py Normal file
View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
import inspect
import glob
from src.libs.custom_logger import get_custom_logger
import os
import pytest
from datetime import datetime
from uuid import uuid4
from src.libs.common import attach_allure_file
import src.env_vars as env_vars
logger = get_custom_logger(__name__)
# See https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item):
outcome = yield
rep = outcome.get_result()
if rep.when == "call":
setattr(item, "rep_call", rep)
return rep
return None
@pytest.fixture(scope="session", autouse=True)
def set_allure_env_variables():
yield
if os.path.isdir("allure-results") and not os.path.isfile(os.path.join("allure-results", "environment.properties")):
logger.debug(f"Running fixture teardown: {inspect.currentframe().f_code.co_name}")
with open(os.path.join("allure-results", "environment.properties"), "w") as outfile:
for attribute_name in dir(env_vars):
if attribute_name.isupper():
attribute_value = getattr(env_vars, attribute_name)
outfile.write(f"{attribute_name}={attribute_value}\n")
@pytest.fixture(scope="function", autouse=True)
def test_id(request):
# setting up an unique test id to be used where needed
logger.debug(f"Running fixture setup: {inspect.currentframe().f_code.co_name}")
request.cls.test_id = f"{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}__{str(uuid4())}"
@pytest.fixture(scope="function", autouse=True)
def attach_logs_on_fail(request):
yield
if env_vars.RUNNING_IN_CI and hasattr(request.node, "rep_call") and request.node.rep_call.failed:
logger.debug(f"Running fixture teardown: {inspect.currentframe().f_code.co_name}")
logger.debug("Test failed, attempting to attach logs to the allure reports")
for file in glob.glob("*.log"):
attach_allure_file(file)

View File

@ -1,10 +1,11 @@
from time import sleep
from src.env_vars import DELAY_BETWEEN_MESSAGES, NUM_MESSAGES
from src.steps.common import StepsCommon
class TestNodes(StepsCommon):
def test_one_to_one_messages(self):
num_messages = 15 # Set the number of messages to send
num_messages = NUM_MESSAGES # Set the number of messages to send
# Send contact request from Charlie to Alice
self.node_charlie.send_contact_request(self.alice_pubkey, "test1")
@ -15,9 +16,9 @@ class TestNodes(StepsCommon):
# Send messages from Charlie to Alice and from Alice to Charlie
for i in range(num_messages):
timestamp_charlie, message_charlie = self.send_message_with_timestamp(self.node_charlie, self.alice_pubkey, f"message_from_charlie_{i}")
sleep(2)
sleep(DELAY_BETWEEN_MESSAGES)
timestamp_alice, message_alice = self.send_message_with_timestamp(self.node_alice, self.charlie_pubkey, f"message_from_alice_{i}")
sleep(2)
sleep(DELAY_BETWEEN_MESSAGES)
messages.append((timestamp_charlie, message_charlie, "charlie"))
messages.append((timestamp_alice, message_alice, "alice"))