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

pytest_runtest_makereport is not giving the report result with xdist #786

Closed
gurdeepsinghiet opened this issue Jun 7, 2022 · 17 comments
Closed

Comments

@gurdeepsinghiet
Copy link

gurdeepsinghiet commented Jun 7, 2022

platform window
Python 3.9.9
pytest-7.1.2
pluggy-1.0.0
plugins: xdist-2.5.0, forked-1.4.0

I have a scenarioes after executing all the test cases with xdist.i need to colloect the report with pytest_runtest_makereport
as follw:
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(self,item, call):
outcome = yield
result = outcome.get_result()
print('makereport:', result)
if result.when == 'call':
item.session.results[item] = result

but result = outcome.get_result() is not giving any result with xdist.without xdist it is working fine.

@RonnyPfannschmidt
Copy link
Member

With xdist print does not work, what's your output /expected output?

@gurdeepsinghiet
Copy link
Author

gurdeepsinghiet commented Jun 7, 2022

actually i am asking about result = outcome.get_result() method ,which is not giving report data retalted to Testcase name Status passed and failed.

i am using the below method to get the result data by using result=outcome.get_result() as follow:
def pytest_sessionfinish(session, exitstatus):
print('run status code:', exitstatus)
nm=[result for result in session.results.values()]
#testcasename=nm[0].nodeid.split("::")[1]
passed_amount = sum(1 for result in session.results.values() if result.passed)
print(f'there are {passed_amount}')
failed_amount = sum(1 for result in session.results.values() if result.failed)
totalCase=passed_amount+failed_amount
print(f'there are {passed_amount} passed and {failed_amount} failed tests in {totalCase} test cases')
summary=[]
for result in session.results.values():
summaryPram = {}
if result.passed:
summaryPram["testcasename"]=result.nodeid.split("::")[1]
summaryPram["status"] = "Passed"
summary.append(summaryPram)
elif result.failed:
summaryPram["testcasename"] = result.nodeid.split("::")[1]
summaryPram["status"] = "Failed"
summary.append(summaryPram)

@RonnyPfannschmidt
Copy link
Member

With a partial badly formatted code example and no expected vs actual output, the only thing i can vaguely guess is that print does not work on xdist

For anything else please provide enough detail to work with

@gurdeepsinghiet
Copy link
Author

gurdeepsinghiet commented Jun 7, 2022

No im not asking about print statement.i need complete report summary after excecuting the below code with xdist:

def pytest_sessionstart(session):
    session.results = dict()

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    **_result = outcome.get_result()_**
    if result.when == 'call':
        item.session.results[item] = result


def pytest_sessionfinish(session, exitstatus):
    nm=[result for result in session.results.values()]
    passed_amount = sum(1 for result in session.results.values() if result.passed)
    failed_amount = sum(1 for result in session.results.values() if result.failed)
    totalCase=passed_amount+failed_amount
    print(f'there are {passed_amount} passed and {failed_amount} failed tests in {totalCase} test cases')
    summary=[]
    for result in session.results.values():
        summaryPram = {}
        if result.passed:
            summaryPram["testcasename"]=result.nodeid.split("::")[1]
            summaryPram["status"] = "Passed"
            summary.append(summaryPram)
        elif result.failed:
            summaryPram["testcasename"] = result.nodeid.split("::")[1]
            summaryPram["status"] = "Failed"
            summary.append(summaryPram)

the above result = outcome.get_result() marked stament is not working fine with xdist.here is the formatted code

@RonnyPfannschmidt
Copy link
Member

It's unclear what you mean by not working

The posted code is unaware of xdist and likely broken as each worker makes a own summary with that

@gurdeepsinghiet
Copy link
Author

gurdeepsinghiet commented Jun 7, 2022

my question is that how to get the consolidated pass/fail report with tetscasename from each worker node .

@RonnyPfannschmidt
Copy link
Member

For that case i recommend that you use the logreport hook only on the coordination and skip doing any on the workers

@gurdeepsinghiet
Copy link
Author

gurdeepsinghiet commented Jun 7, 2022

logreport hook generate the Log file of test summary report?
can i not achive this by collection report data from all worker node?

@gurdeepsinghiet
Copy link
Author

gurdeepsinghiet commented Jun 7, 2022

u mean to say like this:

def pytest_runtest_logreport(report):
       if report and report.when and report.when == 'call':
        print("pytest_runtest_logreport - Test %s had outcome: %s - Did pass? %s"
              % (report.nodeid, report.outcome, report.passed))

@RonnyPfannschmidt
Copy link
Member

Kinda

@gurdeepsinghiet
Copy link
Author

so one more question can i achive this by worked node.and collect all the report data from all worked nodes.

@RonnyPfannschmidt
Copy link
Member

It's not clear what you are asking for, there are no built-in summary combination hooks and your presented use case is fine to run on the coordinator

@gurdeepsinghiet
Copy link
Author

gurdeepsinghiet commented Jun 7, 2022

actully i need to generate the html test summary report which conatin pass,fail,Total test cases.

Testcase Status Priority
test_keyclock Passed P1
test_keyclock1 Passed P1
test_keyclock2 Passed P1
test_keyclock3 Passed P1
test_keyclock4 Passed P1
test_keyclock5 Passed P1
test_keyclock6 Passed P1

so My problem is that when ran my test using xdist.i need to colect the consolidate result of all worker Node.that t can generate the su,mary report.with pass fail percentage.

@RonnyPfannschmidt
Copy link
Member

Using the logreport hook on the coordinator will automatically bring it together

@gurdeepsinghiet
Copy link
Author

logreport generate the Html report by default.or i need to implement the logic inside the logreport hook for html reporting.??

@gurdeepsinghiet
Copy link
Author

gurdeepsinghiet commented Jun 8, 2022

HI @RonnyPfannschmidt
Thanks for ur resolution .
I got all the reporting data by implementing logreport .

here is the code implementation:

class Reporting:
    def __init__(self):
	     self.reportSet=set()

    def pytest_sessionstart(self,session):
        session.results = dict()

    def pytest_runtest_logreport(self,report):
        if report and report.when and report.when == 'call':
            self.reportSet.add(report.nodeid.split("::")[1]+":" +report.outcome)
            print(self.reportSet)


    @pytest.hookimpl(hookwrapper=True, trylast=True)
    def pytest_sessionfinish(self, session, exitstatus):
        passed_amount = sum(1 for result in self.reportSet if result.split(":")[1] == "passed")
        failed_amount = sum(1 for result in self.reportSet if result.split(":")[1] == "failed")
        totalCase = passed_amount + failed_amount
        print(f'there are {passed_amount} passed and {failed_amount} failed tests in {totalCase} Total test cases')
        summary = []
        for result in self.reportSet:
            summaryPram = {}
            if result.split(":")[1] == "passed":
                summaryPram["testcasename"] = result.split(":")[0]
                summaryPram["status"] = "Passed"
                summary.append(summaryPram)
            elif result.split(":")[1] == "failed":
                summaryPram["testcasename"] = result.split(":")[0]
                summaryPram["status"] = "Failed"
                summary.append(summaryPram)

        return summary

@gurdeepsinghiet
Copy link
Author

i m closing my issue.

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

No branches or pull requests

2 participants