diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py
index 744745a8..3903c1cf 100644
--- a/pytest_html/plugin.py
+++ b/pytest_html/plugin.py
@@ -150,8 +150,13 @@ def create_asset(self, content, extra_index,
str(test_index)])
hash_generator = hashlib.md5()
hash_generator.update(hash_key.encode('utf-8'))
- asset_file_name = '{0}_{1}.{2}'.format(hash_key,
- hash_generator.hexdigest(),
+ hex_digest = hash_generator.hexdigest()
+ # 255 is the common max filename length on various filesystems,
+ # we subtract hash length, file extension length and 2 more
+ # characters for the underscore and dot
+ max_length = 255 - len(hex_digest) - len(file_extension) - 2
+ asset_file_name = '{0}_{1}.{2}'.format(hash_key[:max_length],
+ hex_digest,
file_extension)
asset_path = os.path.join(os.path.dirname(self.logfile),
'assets', asset_file_name)
diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py
index eedd44d8..32fc8f6d 100644
--- a/testing/test_pytest_html.py
+++ b/testing/test_pytest_html.py
@@ -503,6 +503,35 @@ def pytest_runtest_makereport(item, call):
assert result.ret == 0
assert '
'.format(content) in html
+ def test_very_long_test_name(self, testdir):
+ testdir.makeconftest("""
+ import pytest
+ @pytest.mark.hookwrapper
+ def pytest_runtest_makereport(item, call):
+ outcome = yield
+ report = outcome.get_result()
+ if report.when == 'call':
+ from pytest_html import extras
+ report.extra = [extras.image('image.png')]
+ """)
+ # This will get truncated
+ test_name = 'test_{}'.format('a' * 300)
+ testdir.makepyfile("""
+ def {0}():
+ assert False
+ """.format(test_name))
+ result, html = run(testdir)
+
+ hash_key = 'test_very_long_test_name.py::{}00'.format(test_name)
+ hash_generator = hashlib.md5()
+ hash_generator.update(hash_key.encode('utf-8'))
+ src = 'assets/{0}_{1}.png'.format(hash_key[:218],
+ hash_generator.hexdigest())
+ link = (''.format(src))
+ assert result.ret
+ assert link in html
+ assert os.path.exists(src)
+
def test_no_environment(self, testdir):
testdir.makeconftest("""
def pytest_configure(config):