Skip to content

Commit

Permalink
Merge pull request #214 from prashanth-sams/archive-count
Browse files Browse the repository at this point in the history
#213 set maximum build count to display in the archives section
  • Loading branch information
kozakHolota committed Apr 11, 2022
2 parents 0c7f0c0 + b290fbe commit ad20ba9
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 619 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- name: Run Tests
run: |
pip install pytest pytest-cov coveralls Pillow
pip install pytest pytest-cov coveralls Pillow pytest-rerunfailures
py.test --cov ./pytest_html_reporter/ tests/unit/
coveralls --service=github
bash <(curl -s https://codecov.io/bash)
5 changes: 5 additions & 0 deletions README.rst
Expand Up @@ -72,6 +72,11 @@ Add ``--title`` tag followed by the report title::

$ pytest tests/ --html-report=./report --title='PYTEST REPORT'

Add ``--archive-count`` tag followed by an integer to limit showing the number of builds in the ``Archives`` section::

$ pytest tests/ --archive-count 7
$ pytest tests/ --html-report=./report --archive-count 7

..
pytest.ini
Expand Down
29 changes: 22 additions & 7 deletions pytest_html_reporter/html_reporter.py
Expand Up @@ -2,6 +2,7 @@
import json
import os
import time
import shutil
from datetime import date, datetime
from os.path import isfile, join

Expand All @@ -18,10 +19,12 @@
from pytest_html_reporter.time_converter import time_converter
from pytest_html_reporter.const_vars import ConfigVars


class HTMLReporter(object):
def __init__(self, path, config):
def __init__(self, path, archive_count, config):
self.json_data = {'content': {'suites': {0: {'status': {}, 'tests': {0: {}}, }, }}}
self.path = path
self.archive_count = archive_count
self.config = config
has_rerun = config.pluginmanager.hasplugin("rerunfailures")
self.rerun = 0 if has_rerun else None
Expand All @@ -37,7 +40,6 @@ def pytest_runtest_teardown(self, item, nextitem):
self.append_test_metrics_row()

def previous_test_name(self, _test_name):

if ConfigVars._previous_test_name == _test_name:
self.rerun += 1
else:
Expand All @@ -49,11 +51,9 @@ def pytest_runtest_setup(item):
ConfigVars._start_execution_time = time.time()

def pytest_sessionfinish(self, session):

if ConfigVars._suite_name is not None: self.append_suite_metrics_row(ConfigVars._suite_name)

def archive_data(self, base, filename):

path = os.path.join(base, filename)

if os.path.isfile(path) is True:
Expand All @@ -78,6 +78,22 @@ def report_path(self):
HTMLReporter.base_path = os.path.abspath(logfile)
return os.path.abspath(logfile), 'pytest_html_report.html'

def remove_old_archives(self):
archive_dir = os.path.abspath(os.path.expanduser(os.path.expandvars(self.path))) + '/archive'

if self.archive_count != '':
if int(self.archive_count) == 0:
if os.path.isdir(archive_dir):
shutil.rmtree(archive_dir)
return

archive_count = int(self.archive_count) - 1
if os.path.isdir(archive_dir):
archives = os.listdir(archive_dir)
archives.sort(key=lambda f: os.path.getmtime(os.path.join(archive_dir, f)))
for i in range(0, len(archives) - archive_count):
os.remove(os.path.join(archive_dir, archives[i]))

@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(self, terminalreporter, exitstatus, config):

Expand All @@ -104,7 +120,8 @@ def pytest_terminal_summary(self, terminalreporter, exitstatus, config):
self.update_trends(base)

# generate archive template
self.update_archives_template(base)
self.remove_old_archives()
self.update_archives_template(base) if self.archive_count != '0' else None

# generate suite highlights
generate_suite_highlights()
Expand Down Expand Up @@ -537,7 +554,6 @@ def generate_json_data(self, base):
json.dump(self.json_data, outfile)

def update_archives_template(self, base):

f = glob.glob(base + '/archive/*.json')
cf = glob.glob(base + '/output.json')
if len(f) > 0:
Expand All @@ -551,7 +567,6 @@ def update_archives_template(self, base):
self.load_archive(cf, value='current')

def load_archive(self, f, value):

def state(data):
if data == 'fail':
return 'times', '#fc6766'
Expand Down
12 changes: 11 additions & 1 deletion pytest_html_reporter/plugin.py
Expand Up @@ -21,15 +21,25 @@ def pytest_addoption(parser):
help="customize report title",
)

group.addoption(
"--archive-count",
action="store",
dest="archive_count",
default="",
help="set maximum build count to display in the archives section",
)


def pytest_configure(config):
path = config.getoption("path")
clean_screenshots(path)

title = config.getoption("title")
custom_title(title)

archive_count = config.getoption("archive_count")

config._html = HTMLReporter(path, config)
config._html = HTMLReporter(path, archive_count, config)
config.pluginmanager.register(config._html)


5 changes: 2 additions & 3 deletions pytest_html_reporter/util.py
Expand Up @@ -7,8 +7,8 @@

from pytest_html_reporter.const_vars import ConfigVars

def suite_highlights(data):

def suite_highlights(data):
for i in data['content']['suites']:
if data['content']['suites'][i]['status']['total_fail'] == 0:
l = data['content']['suites'][i]['suite_name']
Expand All @@ -26,7 +26,6 @@ def suite_highlights(data):


def generate_suite_highlights():

if ConfigVars.highlights == {}:
ConfigVars.max_failure_suite_name_final = 'No failures in History'
ConfigVars.max_failure_suite_count = 0
Expand Down Expand Up @@ -78,4 +77,4 @@ def clean_screenshots(path):


def custom_title(title):
ConfigVars._title = title[:26] + '...' if title.__len__() > 29 else title
ConfigVars._title = title[:26] + '...' if title.__len__() > 29 else title
3 changes: 2 additions & 1 deletion requirements.txt
Expand Up @@ -3,4 +3,5 @@ pytest-cov
coveralls
selenium
twine
pytest-xdist
pytest-xdist
pytest-rerunfailures
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -13,7 +13,7 @@ def read(fname):

setup(
name="pytest-html-reporter",
version="0.3",
version="0.3.0",
author="Prashanth Sams",
author_email="sams.prashanth@gmail.com",
maintainer="Prashanth Sams",
Expand Down

0 comments on commit ad20ba9

Please sign in to comment.