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

how to get trace and failed screenshot after yield in function scope #267

Open
vasilyevi opened this issue Jan 12, 2025 · 10 comments
Open

Comments

@vasilyevi
Copy link

I need to upload trace and screenshot made on failure at the end of each test to my personal external system, but in fixture, in function scope trace and screenshots does not exist. It will be created after the fixture itself. So, my question is how to deal with them at the end of each test?

@arieluchka-bse
Copy link

could you please share the relevant snippets of your code?

@vasilyevi
Copy link
Author

Running with --screenshot="only-on-failure" and --tracing=on

@pytest.fixture(scope="function", autouse=True)
def logged_in_user(page: Page, request, playwright: Playwright):
   //do login
    yield
   // do after test and here I need to get the trace.zip and screenshot if test is failed, but I'm not able as both does not exist yet. 
   //Which is weird as the test itself is completed and especially weird for screenshot on failure as I assume it have to be taken 
   //exactly after the test is failed

@aspenboy
Copy link

If you yield the test case is not considered as finished yet. I don't know your exact use case, so can't recommend precise solution - but I'd think about getting rid of yield.

@vasilyevi
Copy link
Author

vasilyevi commented Jan 13, 2025

The use case is pretty simple, once any test is completed I need to upload artifacts(trace and screenshot) of a test to a reporting tool. To upload them I somehow need to get their path in system.
One more time. Let's say I use testrail. Once a test is completed I need to send an API request to testrail with the test status and attach to testrail test result an artifacts.
If yield is not considered as finished yet, is there any other ways?

@aspenboy
Copy link

By "use case" I meant "why do you have to use yield inside your test" :) You can try to use return instead, but the best way would be probably to end it with assertion.

@vasilyevi
Copy link
Author

To be honest not sure we understand each other. What do you mean to use return instead?
Could you give a small example?

@aspenboy
Copy link

Ok, let's reverse the question - why you're using yield keyword in the middle of your testcase? If I understand why you're doing this then we can think about the solution here.

@vasilyevi
Copy link
Author

Because I need to get the trace and screenshot after the test execution, once the test itself is completed. As far as I understand fixture + yield is the only one way to do that(or recommended).

@aspenboy
Copy link

I think I messed up :( For some reason I thought I'm looking at the test function (your code snippet), but in fact it's a fixture. So my advice was for completely different problem... apologies for that.

For your issue - yeah, I think it should work the way you described. Some time ago I was trying to rename the video files (because videos recorded by Playwright are named with quite random chars, so if you run more than one test it's hard to tell which video is for which). Suprisingly the videos were not added "on the fly" after each test case, but after WHOLE test run. It might be the case with the screenshots - maybe they are in the temp folder? (unless the screenshots are not recorded even after whole testrun finishes - in such case I have no idea :) )

@ltsuda
Copy link

ltsuda commented Jan 13, 2025

Hello, you might need to look at Pytest Hooks and perform actions after each test finishes/failed.

https://pytest.org/en/stable/reference.html#hooks
https://pytest-with-eric.com/plugins/pytest-html/#Add-Images (examples)
https://pytest-with-eric.com/hooks/pytest-hooks/ (examples)

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

4 participants