|
| 1 | +from _pytest.reports import CollectReport |
| 2 | +from _pytest.reports import TestReport |
| 3 | + |
| 4 | + |
| 5 | +class TestReportSerialization(object): |
| 6 | + """ |
| 7 | + All the tests in this class came originally from test_remote.py in xdist (ca03269). |
| 8 | + """ |
| 9 | + |
| 10 | + def test_xdist_longrepr_to_str_issue_241(self, testdir): |
| 11 | + testdir.makepyfile( |
| 12 | + """ |
| 13 | + import os |
| 14 | + def test_a(): assert False |
| 15 | + def test_b(): pass |
| 16 | + """ |
| 17 | + ) |
| 18 | + reprec = testdir.inline_run() |
| 19 | + reports = reprec.getreports("pytest_runtest_logreport") |
| 20 | + assert len(reports) == 6 |
| 21 | + test_a_call = reports[1] |
| 22 | + assert test_a_call.when == "call" |
| 23 | + assert test_a_call.outcome == "failed" |
| 24 | + assert test_a_call._to_json()["longrepr"]["reprtraceback"]["style"] == "long" |
| 25 | + test_b_call = reports[4] |
| 26 | + assert test_b_call.when == "call" |
| 27 | + assert test_b_call.outcome == "passed" |
| 28 | + assert test_b_call._to_json()["longrepr"] is None |
| 29 | + |
| 30 | + def test_xdist_report_longrepr_reprcrash_130(self, testdir): |
| 31 | + reprec = testdir.inline_runsource( |
| 32 | + """ |
| 33 | + import py |
| 34 | + def test_fail(): assert False, 'Expected Message' |
| 35 | + """ |
| 36 | + ) |
| 37 | + reports = reprec.getreports("pytest_runtest_logreport") |
| 38 | + assert len(reports) == 3 |
| 39 | + rep = reports[1] |
| 40 | + added_section = ("Failure Metadata", str("metadata metadata"), "*") |
| 41 | + rep.longrepr.sections.append(added_section) |
| 42 | + d = rep._to_json() |
| 43 | + a = TestReport._from_json(d) |
| 44 | + # Check assembled == rep |
| 45 | + assert a.__dict__.keys() == rep.__dict__.keys() |
| 46 | + for key in rep.__dict__.keys(): |
| 47 | + if key != "longrepr": |
| 48 | + assert getattr(a, key) == getattr(rep, key) |
| 49 | + assert rep.longrepr.reprcrash.lineno == a.longrepr.reprcrash.lineno |
| 50 | + assert rep.longrepr.reprcrash.message == a.longrepr.reprcrash.message |
| 51 | + assert rep.longrepr.reprcrash.path == a.longrepr.reprcrash.path |
| 52 | + assert rep.longrepr.reprtraceback.entrysep == a.longrepr.reprtraceback.entrysep |
| 53 | + assert ( |
| 54 | + rep.longrepr.reprtraceback.extraline == a.longrepr.reprtraceback.extraline |
| 55 | + ) |
| 56 | + assert rep.longrepr.reprtraceback.style == a.longrepr.reprtraceback.style |
| 57 | + assert rep.longrepr.sections == a.longrepr.sections |
| 58 | + # Missing section attribute PR171 |
| 59 | + assert added_section in a.longrepr.sections |
| 60 | + |
| 61 | + def test_reprentries_serialization_170(self, testdir): |
| 62 | + from _pytest._code.code import ReprEntry |
| 63 | + |
| 64 | + reprec = testdir.inline_runsource( |
| 65 | + """ |
| 66 | + def test_repr_entry(): |
| 67 | + x = 0 |
| 68 | + assert x |
| 69 | + """, |
| 70 | + "--showlocals", |
| 71 | + ) |
| 72 | + reports = reprec.getreports("pytest_runtest_logreport") |
| 73 | + assert len(reports) == 3 |
| 74 | + rep = reports[1] |
| 75 | + d = rep._to_json() |
| 76 | + a = TestReport._from_json(d) |
| 77 | + |
| 78 | + rep_entries = rep.longrepr.reprtraceback.reprentries |
| 79 | + a_entries = a.longrepr.reprtraceback.reprentries |
| 80 | + for i in range(len(a_entries)): |
| 81 | + assert isinstance(rep_entries[i], ReprEntry) |
| 82 | + assert rep_entries[i].lines == a_entries[i].lines |
| 83 | + assert rep_entries[i].reprfileloc.lineno == a_entries[i].reprfileloc.lineno |
| 84 | + assert ( |
| 85 | + rep_entries[i].reprfileloc.message == a_entries[i].reprfileloc.message |
| 86 | + ) |
| 87 | + assert rep_entries[i].reprfileloc.path == a_entries[i].reprfileloc.path |
| 88 | + assert rep_entries[i].reprfuncargs.args == a_entries[i].reprfuncargs.args |
| 89 | + assert rep_entries[i].reprlocals.lines == a_entries[i].reprlocals.lines |
| 90 | + assert rep_entries[i].style == a_entries[i].style |
| 91 | + |
| 92 | + def test_reprentries_serialization_196(self, testdir): |
| 93 | + from _pytest._code.code import ReprEntryNative |
| 94 | + |
| 95 | + reprec = testdir.inline_runsource( |
| 96 | + """ |
| 97 | + def test_repr_entry_native(): |
| 98 | + x = 0 |
| 99 | + assert x |
| 100 | + """, |
| 101 | + "--tb=native", |
| 102 | + ) |
| 103 | + reports = reprec.getreports("pytest_runtest_logreport") |
| 104 | + assert len(reports) == 3 |
| 105 | + rep = reports[1] |
| 106 | + d = rep._to_json() |
| 107 | + a = TestReport._from_json(d) |
| 108 | + |
| 109 | + rep_entries = rep.longrepr.reprtraceback.reprentries |
| 110 | + a_entries = a.longrepr.reprtraceback.reprentries |
| 111 | + for i in range(len(a_entries)): |
| 112 | + assert isinstance(rep_entries[i], ReprEntryNative) |
| 113 | + assert rep_entries[i].lines == a_entries[i].lines |
| 114 | + |
| 115 | + def test_itemreport_outcomes(self, testdir): |
| 116 | + reprec = testdir.inline_runsource( |
| 117 | + """ |
| 118 | + import py |
| 119 | + def test_pass(): pass |
| 120 | + def test_fail(): 0/0 |
| 121 | + @py.test.mark.skipif("True") |
| 122 | + def test_skip(): pass |
| 123 | + def test_skip_imperative(): |
| 124 | + py.test.skip("hello") |
| 125 | + @py.test.mark.xfail("True") |
| 126 | + def test_xfail(): 0/0 |
| 127 | + def test_xfail_imperative(): |
| 128 | + py.test.xfail("hello") |
| 129 | + """ |
| 130 | + ) |
| 131 | + reports = reprec.getreports("pytest_runtest_logreport") |
| 132 | + assert len(reports) == 17 # with setup/teardown "passed" reports |
| 133 | + for rep in reports: |
| 134 | + d = rep._to_json() |
| 135 | + newrep = TestReport._from_json(d) |
| 136 | + assert newrep.passed == rep.passed |
| 137 | + assert newrep.failed == rep.failed |
| 138 | + assert newrep.skipped == rep.skipped |
| 139 | + if newrep.skipped and not hasattr(newrep, "wasxfail"): |
| 140 | + assert len(newrep.longrepr) == 3 |
| 141 | + assert newrep.outcome == rep.outcome |
| 142 | + assert newrep.when == rep.when |
| 143 | + assert newrep.keywords == rep.keywords |
| 144 | + if rep.failed: |
| 145 | + assert newrep.longreprtext == rep.longreprtext |
| 146 | + |
| 147 | + def test_collectreport_passed(self, testdir): |
| 148 | + reprec = testdir.inline_runsource("def test_func(): pass") |
| 149 | + reports = reprec.getreports("pytest_collectreport") |
| 150 | + for rep in reports: |
| 151 | + d = rep._to_json() |
| 152 | + newrep = CollectReport._from_json(d) |
| 153 | + assert newrep.passed == rep.passed |
| 154 | + assert newrep.failed == rep.failed |
| 155 | + assert newrep.skipped == rep.skipped |
| 156 | + |
| 157 | + def test_collectreport_fail(self, testdir): |
| 158 | + reprec = testdir.inline_runsource("qwe abc") |
| 159 | + reports = reprec.getreports("pytest_collectreport") |
| 160 | + assert reports |
| 161 | + for rep in reports: |
| 162 | + d = rep._to_json() |
| 163 | + newrep = CollectReport._from_json(d) |
| 164 | + assert newrep.passed == rep.passed |
| 165 | + assert newrep.failed == rep.failed |
| 166 | + assert newrep.skipped == rep.skipped |
| 167 | + if rep.failed: |
| 168 | + assert newrep.longrepr == str(rep.longrepr) |
| 169 | + |
| 170 | + def test_extended_report_deserialization(self, testdir): |
| 171 | + reprec = testdir.inline_runsource("qwe abc") |
| 172 | + reports = reprec.getreports("pytest_collectreport") |
| 173 | + assert reports |
| 174 | + for rep in reports: |
| 175 | + rep.extra = True |
| 176 | + d = rep._to_json() |
| 177 | + newrep = CollectReport._from_json(d) |
| 178 | + assert newrep.extra |
| 179 | + assert newrep.passed == rep.passed |
| 180 | + assert newrep.failed == rep.failed |
| 181 | + assert newrep.skipped == rep.skipped |
| 182 | + if rep.failed: |
| 183 | + assert newrep.longrepr == str(rep.longrepr) |
0 commit comments