Skip to content

Add hook to change report main title #270

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

Merged
merged 1 commit into from
Feb 28, 2020
Merged

Conversation

werdeil
Copy link
Contributor

@werdeil werdeil commented Feb 26, 2020

I think it is a good thing to be able to have a customizable report title that is not always the same as the file name.

As it is my first merge request on this project I'm open to any comment.

@BeyondEvil
Copy link
Contributor

Thanks for this PR @werdeil !

Couple of comments:

  • Tests on Travis are failing. Specifically linting. Please see this on how to fix.
  • Please do add some documentation regarding the hook here, bonus points if you add an example.
  • Please also add some tests to cover the new functionality.

Feel free to reach out if you need any guidance.

Thanks again!

@@ -490,9 +490,12 @@ def generate_summary_item(self):
__name__, os.path.join("resources", "main.js")
).decode("utf-8")

report_title = os.path.basename(self.logfile)
session.config.hook.pytest_html_results_report_title(report_title=report_title)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the behavior for this a bit confusing? I thought strings were immutable.

>>> def swap(rt):
...     rt = 'hi'
...
>>> rt = 'report title'
>>> swap(rt)
>>> rt
'report title'
>>>            

How can the hook modify the report_title in place? Shouldn't the hook return a value instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, did think it about enough, sorry.

I see two solutions:

  • the hook return a value but it doesn't work as the other hooks.
  • the hook takes a mutable as input and we keep this behavior

I think the second option is better, and I'll push a commit to give the whole html title to the hook. It will allow more title customization.
Just need to document it, add test and examples before submitting again.

@werdeil
Copy link
Contributor Author

werdeil commented Feb 27, 2020

I think I've covered all review comments, Travis tests are failing but I think this is not related to my changes, I would like to rerun it but I don't have the right to do it.

@BeyondEvil @csm10495 Tell me if I need to do anything more.

@BeyondEvil
Copy link
Contributor

Is there a specific use-case for the hook @werdeil ?

With the way it is implemented, it's a lot of code (perhaps even the introduction of a conftest file) just to set the report title.

If there's not a specific use-case that warrants a hook, maybe a command line argument and/or an ini-file key will suffice? See this comment

@werdeil
Copy link
Contributor Author

werdeil commented Feb 27, 2020

My use case is to have a CI pipeline that generates a customized report.html. The CI generates automatically the title, and may add dynamically version or number of pass/fail e.g. "TEST REPORT v1.2.3 [2/3 Passed]"

In my use case I already have a conftest, and use other hooks so having another hook seems logic.

Having a command-line and/or an ini file key will indeed be sufficient except in cases where we want to add some test results in it.
Personally I think it is better to have it as a hook (even if it means having a conftest) as I see it similar as other report.html customizations (all customizations in the same file, but tell me if you want me to explore a solution with the --html-title option.

@BeyondEvil
Copy link
Contributor

Ok, fair enough! Thanks for the detailed explanation.

So, I guess with that, what I would like to see is to make it a little more straight forward to set the title than to have to mess around with html etc.

Here's my suggestion:

Add a title property to the HTMLReport class.

Then you can make the hook like so:

def pytest_html_report_title(report):
    """ Called before adding the title to the report """

And the plugin can call the hook:

session.config.hook.pytest_html_report_title(report=self)

And an implementation can then be made like:

def pytest_html_report_title(report):
    report.title = "My very own title!"

Also, note the name change of the hook. The title is for the entire report, not only the results part. 😊

Hope that makes sense. Feel free to question it. :)

@werdeil
Copy link
Contributor Author

werdeil commented Feb 28, 2020

Ok I'll follow your suggestion but I have one remark though: as we give the report to the hook, it allows to change all parts of the report, not only the title.

I see that we have already hooks that take the report as input so I guess it is acceptable. My concern is that someone willing to change anything in the report will use the "pytest_html_report_title" hook instead of finding a "better" way to do it, but as I said, it is already possible though other hooks.

Copy link
Contributor

@BeyondEvil BeyondEvil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once this is changed, can you also squash/fixup the commits? Thanks!

Copy link
Contributor

@BeyondEvil BeyondEvil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 💪

@BeyondEvil BeyondEvil merged commit ae0ca62 into pytest-dev:master Feb 28, 2020
@BeyondEvil
Copy link
Contributor

Released in v2.1.0

@ssbarnea ssbarnea added the feature This issue/PR relates to a feature request. label Aug 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This issue/PR relates to a feature request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants