Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach base64 images to the report #40

Open
mvgiacomello opened this issue Feb 13, 2019 · 6 comments
Open

Attach base64 images to the report #40

mvgiacomello opened this issue Feb 13, 2019 · 6 comments

Comments

@mvgiacomello
Copy link

  • HtmlTestRunner version: 1.1.2
  • Python version: 3.7
  • Operating System: Mac/Linux

Description

I'd like to have the ability to insert images to a certain test, specifically in base64 format.
We use this library for our selenium test reports and would be nice to attach screenshots when the tests fail.

What I Did

Nothing is specific. I am planning to contribute to this project if that's a feature you'd like to be added.

@oldani
Copy link
Owner

oldani commented Mar 15, 2019

This project allows you to pass your own template to be rendered. I think you can add logic to your tests where it takes the pictures on fail and then passes your own template to get this rendered.

@mvgiacomello
Copy link
Author

mvgiacomello commented Mar 18, 2019

Alright, I am "adding" the screenshot as follows:

class BaseTest(unittest.TestCase):
    def tearDown(self):
        for method, error in self._outcome.errors: 
            if error: # Verify if did not pass
                self.base_64_img = foo() # foo() returns the base64 image

So, I created my out template, a clone of the dault but I added the following:
{%- if test_case.err %}<p style="color:maroon;">{{ test_case.base_64_img }}</p>{% endif %}

However, I noticed the test_case is an actual object from HtmlTestRunner which I don't have access from my the unittest.TestCase.

Now, I did something stupid that worked but it's so stupid that I am sure it has a better way:

I replace self.base_64_img = foo()
With print(foo())
And added in the template:
<img src="data:image/png;base64, {{ test_case.stdout }}"/>

Thoughts on how I could make the first scenario do-able? I wouldn't mind contributing some code to your project.

@JamesMTSloan
Copy link

JamesMTSloan commented Mar 20, 2019

Hello,

I think you can actually do this as-is by using a custom TestResult class and overriding its stopTest() method. This class can be passed as a kwarg.

import unittest

from HtmlTestRunner import HTMLTestRunner
from HtmlTestRunner.result import HtmlTestResult


class CustomTestResult(HtmlTestResult):
    images = None

    def stopTest(self, test):
        self.images = "link to my image"
        super().stopTest(test)


class MyTests(unittest.TestCase):
    def test_one(self):
        self.assertTrue(True)


if __name__ == '__main__':
    unittest.main(
        testRunner=HTMLTestRunner(
            open_in_browser=True,
            resultclass=CustomTestResult
        )
    )

You will also need to edit the template accordingly, referencing this new custom field as follows for the non-subtests: test_case.test_result.images
(Remember to also add this to logic that controls whether or not the button element is shown.)

I hope this helps.

@Enigmaderockz
Copy link

Enigmaderockz commented Aug 3, 2019

HtmlTestRunner is not calling tearDown() in my test cases. is it getting called in your case?

@vishnuvk123
Copy link

Have you got solution anyone? Can you please guide me proper solution

@tmznwnel00
Copy link

@vishnuvk123 I solved this issue.
please check #100 and #101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants