testing-html-merge (#15)

* add tests for merge_html_reports function

Reviewed-on: local-it-infrastructure/e2e_tests#15
Co-authored-by: Daniel <d.brummerloh@gmail.com>
Co-committed-by: Daniel <d.brummerloh@gmail.com>
This commit is contained in:
Daniel 2023-12-09 15:28:48 +01:00 committed by dan
parent 873bf73ae8
commit 016b88a68d
11 changed files with 2762 additions and 19 deletions

View file

@ -7,7 +7,7 @@ from loguru import logger
from pytest_abra.dir_manager import DirManager
from pytest_abra.env_manager import EnvFile, EnvManager
from pytest_abra.html_helper import merge_html_files
from pytest_abra.html_helper import merge_html_reports
from pytest_abra.runner import Runner
from pytest_abra.utils import rmtree
@ -58,10 +58,10 @@ class Coordinator:
def combine_html(self) -> None:
"""combines all generated pytest html reports into one"""
in_path = str(self.DIR.RECORDS / "html")
out_path = str(self.DIR.RECORDS / "full-report.html")
in_dir_path = str(self.DIR.RECORDS / "html")
out_file_path = str(self.DIR.RECORDS / "full-report.html")
title = "combined.html"
merge_html_files(in_path, out_path, title)
merge_html_reports(in_dir_path, out_file_path, title)
def collect_traces(self):
"""moves all traces into SESSION/RECORDS dir

View file

@ -5,19 +5,36 @@ import json
import os
import pathlib
import re
import shutil
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup # type: ignore
from packaging import version
CHECKBOX_REGEX = r"^(?P<num>0|[1-9]\d*) (?P<txt1>.*)"
def merge_html_files(in_path: str, out_path: str, title: str):
paths = get_html_files(in_path, out_path)
if not paths:
raise RuntimeError(f"Unable to find html files in {in_path}")
def custom_copy_assets(assets_dir_path: str, out_file_path: str):
"""custom function added for pytest_abra
assets_dir_path = get_assets_path(in_path)
copies every asset to asset folder. Exclude style.css as this is already handled by pytest_html_merger"""
assets_source_dir = pathlib.Path(assets_dir_path)
assets_source_files = [p for p in assets_source_dir.glob("*") if p.is_file() and p.name != "style.css"]
out_dir_path = pathlib.Path(out_file_path).parent
assets_target_dir = out_dir_path / "assets"
assets_target_dir.mkdir(exist_ok=True)
for asset in assets_source_files:
shutil.copy(asset, assets_target_dir / asset.name)
def merge_html_reports(in_dir_path: str, out_file_path: str, report_title: str):
paths = get_html_files(in_dir_path, out_file_path)
if not paths:
raise RuntimeError(f"Unable to find html files in {in_dir_path}")
assets_dir_path = get_assets_path(in_dir_path)
custom_copy_assets(assets_dir_path, out_file_path)
first_file = BeautifulSoup("".join(open(paths[0])), features="html.parser")
paths.pop(0)
@ -30,7 +47,7 @@ def merge_html_files(in_path: str, out_path: str, title: str):
if assets_dir_path is None:
print(
f"Will assume css is embedded in the reports. If this is not the case, "
f"Please make sure that you have 'assets' directory inside {in_path} "
f"Please make sure that you have 'assets' directory inside {in_dir_path} "
f"which contains css files generated by pytest-html."
)
else:
@ -42,7 +59,7 @@ def merge_html_files(in_path: str, out_path: str, title: str):
head.style.append(content)
h = first_file.find("h1")
h.string = title or os.path.basename(out_path)
h.string = report_title or os.path.basename(out_file_path)
ps = first_file.find_all("p")
pytest_version = ps[0].text.split(" ")[-1]
@ -106,7 +123,7 @@ def merge_html_files(in_path: str, out_path: str, title: str):
for cb_type in cb_types:
set_checkbox_value(first_file, cb_type, cb_types[cb_type])
with open(out_path, "w") as f:
with open(out_file_path, "w") as f:
f.write(str(first_file))

View file

@ -57,7 +57,7 @@ class Runner:
"""runs the main test script and if available and sub test scripts if their running condition is met"""
# check if required dependencies have passed
if not self._dependencies_passed():
logger.warning(f"skipping run_tests() of {self.env_type}, because some dependencies have not passed")
logger.warning(f"skipping run_tests() of {self.env_type} (one or more dependencies have not passed)")
return
for test in test_list:
@ -79,16 +79,16 @@ class Runner:
# check if test aleady passed
if self._is_test_passed(identifier_string, remove_existing=True):
if test.prevent_skip:
logger.info(f"continuing , test {identifier_string} has passed but prevent_skip=True")
logger.info(f"continuing {identifier_string} (passed before but prevent_skip=True)")
else:
logger.info(f"skipping {identifier_string}, test has passed")
logger.info(f"skipping {identifier_string} (test has passed)")
return
if test.condition:
condition_result = self._run_condition(test.condition)
if not condition_result:
# test condition is defined but not met
logger.info(f"skipping {identifier_string}, test condition is not met")
logger.info(f"skipping {identifier_string} (test condition is not met)")
return
# test condition is undefined or not met
@ -126,7 +126,7 @@ class Runner:
return already_passed
def _call_pytest(self, full_test_path: Path) -> int:
"""runs pytest programmatically on a specific file
"""runs pytest programmatically with a specific file
all tests in the file [full_test_path] will be run along with command line arguments"""