Skip to content

Commit

Permalink
Merge branch 'master' into fix-ansi-in-report
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Aug 10, 2020
2 parents 0c7061c + 4dfec1c commit 955eb44
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Release Notes

**2.1.2 (unreleased)**

* Make the ``Results`` table ``Links`` column sortable (`#242 <https://github.com/pytest-dev/pytest-html/issues/242>`_)

* Thanks to `@vashirov <https://github.com/vashirov>`_ for reporting and `@gnikonorov <https://github.com/gnikonorov>`_ for the fix

* Fix issue with missing image or video in extras. (`#265 <https://github.com/pytest-dev/pytest-html/issues/265>`_ and `pytest-selenium#237 <https://github.com/pytest-dev/pytest-selenium/issues/237>`_)

* Thanks to `@p00j4 <https://github.com/p00j4>`_ and `@anothermattbrown <https://github.com/anothermattbrown>`_ for reporting and `@christiansandberg <https://github.com/christiansandberg>`_ and `@superdodd <https://github.com/superdodd>`_ and `@dhalperi <https://github.com/dhalperi>`_ for the fix
Expand Down
14 changes: 11 additions & 3 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def pytest_addoption(parser):
default=False,
help="Open the report with all rows collapsed. Useful for very large reports",
)
parser.addini(
"max_asset_filename_length",
default=255,
help="set the maximum filename length for assets "
"attached to the html report.",
)


def pytest_configure(config):
Expand Down Expand Up @@ -147,6 +153,9 @@ def __init__(self, outcome, report, logfile, config):
self.additional_html = []
self.links_html = []
self.self_contained = config.getoption("self_contained_html")
self.max_asset_filename_length = int(
config.getini("max_asset_filename_length")
)
self.logfile = logfile
self.config = config
self.row_table = self.row_extra = None
Expand Down Expand Up @@ -196,13 +205,12 @@ def __lt__(self, other):
def create_asset(
self, content, extra_index, test_index, file_extension, mode="w"
):
# 255 is the common max filename length on various filesystems
asset_file_name = "{}_{}_{}.{}".format(
re.sub(r"[^\w\.]", "_", self.test_id),
str(extra_index),
str(test_index),
file_extension,
)[-255:]
)[-self.max_asset_filename_length :]
asset_path = os.path.join(
os.path.dirname(self.logfile), "assets", asset_file_name
)
Expand Down Expand Up @@ -496,7 +504,7 @@ def generate_summary_item(self):
html.th("Result", class_="sortable result initial-sort", col="result"),
html.th("Test", class_="sortable", col="name"),
html.th("Duration", class_="sortable numeric", col="duration"),
html.th("Links"),
html.th("Links", class_="sortable links", col="links"),
]
session.config.hook.pytest_html_results_table_header(cells=cells)

Expand Down
9 changes: 9 additions & 0 deletions pytest_html/resources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function sort_column(elem) {
key = key_num;
} else if (elem.classList.contains('result')) {
key = key_result;
} else if (elem.classList.contains('links')) {
key = key_link;
} else {
key = key_alpha;
}
Expand Down Expand Up @@ -178,6 +180,13 @@ function key_num(col_index) {
};
}

function key_link(col_index) {
return function(elem) {
dataCell = elem.childNodes[1].childNodes[col_index].firstChild
return dataCell == null ? "" : dataCell.innerText.toLowerCase();
};
}

function key_result(col_index) {
return function(elem) {
var strings = ['Error', 'Failed', 'Rerun', 'XFailed', 'XPassed',
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
package_data={"pytest_html": ["resources/*"]},
entry_points={"pytest11": ["html = pytest_html.plugin"]},
setup_requires=["setuptools_scm"],
install_requires=["pytest>=5.0", "pytest-metadata"],
install_requires=["pytest>=5.0,!=6.0.0", "pytest-metadata"],
license="Mozilla Public License 2.0 (MPL 2.0)",
keywords="py.test pytest html report",
python_requires=">=3.6",
Expand Down
4 changes: 2 additions & 2 deletions testing/js_test_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<th class="sortable result initial-sort" col="result">Result</th>
<th class="sortable" col="name">Test</th>
<th class="sortable numeric" col="duration">Duration</th>
<th>Links</th></tr>
<th class="sortable links" col="links">Links</th></tr>
<tr hidden="true" id="not-found-message">
<th colspan="5">No results found. Try to check the filters</th>
</tr>
Expand All @@ -30,7 +30,7 @@
<td class="col-result">Rerun</td>
<td class="test-1 col-name">rerun.py::test_rexample_1</td>
<td class="col-duration">1.00</td>
<td class="col-links"></td></tr>
<td class="col-links"><a class="url" href="http://www.google.com/" target="_blank">URL</a> </td></tr>
<tr>
<td class="extra" colspan="5">
<div class="log">@pytest.mark.flaky(reruns=5)<br/> def test_example():<br/> import random<br/>&gt; assert random.choice([True, False])<br/><span class="error">E assert False</span><br/><span class="error">E + where False = &lt;bound method Random.choice of &lt;random.Random object at 0x7fe80b85f420&gt;&gt;([True, False])</span><br/><span class="error">E + where &lt;bound method Random.choice of &lt;random.Random object at 0x7fe80b85f420&gt;&gt; = &lt;module 'random' from '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyc'&gt;.choice</span><br/><br/>rerun.py:6: AssertionError<br/></div></td></tr></tbody>
Expand Down
8 changes: 7 additions & 1 deletion testing/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
'rerun results-table-row', 'passed results-table-row');
sort_column_test('[col=duration]',
'passed results-table-row', 'rerun results-table-row');

//links
sort_column_test('[col=links]',
'rerun results-table-row', 'passed results-table-row');
sort_column_test('[col=links]',
'passed results-table-row', 'rerun results-table-row');
});

QUnit.test('filter_table', function(assert){
Expand Down Expand Up @@ -123,6 +129,6 @@ QUnit.test('find', function (assert) {
});

QUnit.test('find_all', function(assert) {
assert.equal(find_all('.sortable').length, 3);
assert.equal(find_all('.sortable').length, 4);
assert.equal(find_all('.not-in-table').length, 0);
});
15 changes: 12 additions & 3 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,8 @@ def pytest_runtest_makereport(item, call):
assert result.ret == 0
assert '<a href="{0}"><img src="{0}"/>'.format(content) in html

def test_very_long_test_name(self, testdir):
@pytest.mark.parametrize("max_asset_filename_length", [10, 100])
def test_very_long_test_name(self, testdir, max_asset_filename_length):
testdir.makeconftest(
"""
import pytest
Expand All @@ -587,8 +588,16 @@ def {test_name}():
assert False
"""
)
result, html = run(testdir)
file_name = f"test_very_long_test_name.py__{test_name}_0_0.png"[-255:]
testdir.makeini(
f"""
[pytest]
max_asset_filename_length = {max_asset_filename_length}
"""
)
result, html = run(testdir, "report.html")
file_name = f"test_very_long_test_name.py__{test_name}_0_0.png"[
-max_asset_filename_length:
]
src = "assets/" + file_name
link = f'<a class="image" href="{src}" target="_blank">'
img = f'<img src="{src}"/>'
Expand Down

0 comments on commit 955eb44

Please sign in to comment.