1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
|
from io import StringIO
from traceback import format_exception
from flaky import defaults
from flaky.names import FlakyNames
class _FlakyPlugin:
_retry_failure_message = ' failed ({0} runs remaining out of {1}).'
_failure_message = ' failed; it passed {0} out of the required {1} times.'
_not_rerun_message = ' failed and was not selected for rerun.'
def __init__(self):
super().__init__()
self._stream = StringIO()
self._flaky_success_report = True
self._had_flaky_tests = False
@property
def stream(self):
"""
Returns the stream used for building the flaky report.
Anything written to this stream before the end of the test run
will be written to the flaky report.
:return:
The stream used for building the flaky report.
:rtype:
:class:`StringIO`
"""
return self._stream
def _log_test_failure(self, test_callable_name, err, message):
"""
Add messaging about a test failure to the stream, which will be
printed by the plugin's report method.
"""
formatted_exception_info = ''.join(format_exception(*err)).replace('\n', '\n\t').rstrip()
self._stream.writelines([
str(test_callable_name),
str(message),
str(formatted_exception_info),
'\n',
])
def _report_final_failure(self, err, flaky, name):
"""
Report that the test has failed too many times to pass at
least min_passes times.
By default, this means that the test has failed twice.
:param err:
Information about the test failure (from sys.exc_info())
:type err:
`tuple` of `class`, :class:`Exception`, `traceback`
:param flaky:
Dictionary of flaky attributes
:type flaky:
`dict` of `unicode` to varies
:param name:
The test name
:type name:
`unicode`
"""
min_passes = flaky[FlakyNames.MIN_PASSES]
current_passes = flaky[FlakyNames.CURRENT_PASSES]
message = self._failure_message.format(
current_passes,
min_passes,
)
self._log_test_failure(name, err, message)
def _log_intermediate_failure(self, err, flaky, name):
"""
Report that the test has failed, but still has reruns left.
Then rerun the test.
:param err:
Information about the test failure (from sys.exc_info())
:type err:
`tuple` of `class`, :class:`Exception`, `traceback`
:param flaky:
Dictionary of flaky attributes
:type flaky:
`dict` of `unicode` to varies
:param name:
The test name
:type name:
`unicode`
"""
max_runs = flaky[FlakyNames.MAX_RUNS]
runs_left = max_runs - flaky[FlakyNames.CURRENT_RUNS]
message = self._retry_failure_message.format(
runs_left,
max_runs,
)
self._log_test_failure(name, err, message)
def _should_handle_test_error_or_failure(self, test):
"""
Whether or not flaky should handle a test error or failure.
Only handle tests marked @flaky.
Count remaining retries and compare with number of required successes that have not yet been achieved.
This method may be called multiple times for the same test run, so it has no side effects.
:param test:
The test that has raised an error
:type test:
:class:`Function`
:return:
True, if the test needs to be rerun; False, otherwise.
:rtype:
`bool`
"""
if not self._has_flaky_attributes(test):
return False
flaky_attributes = self._get_flaky_attributes(test)
flaky_attributes[FlakyNames.CURRENT_RUNS] += 1
has_failed = self._has_flaky_test_failed(flaky_attributes)
return not has_failed
def _will_handle_test_error_or_failure(self, test, name, err):
"""
Whether or not flaky will handle a test error or failure.
Returns True if the plugin should handle the test result, and
the `rerun_filter` returns True.
:param test:
The test that has raised an error
:type test:
:class:`Function`
:param name:
The name of the test that has raised an error
:type name:
`unicode`
:param err:
Information about the test failure (from sys.exc_info())
:type err:
`tuple` of `type`, :class:`Exception`, `traceback`
:return:
True, if the test will be rerun by flaky; False, otherwise.
:rtype:
`bool`
"""
return self._should_handle_test_error_or_failure(test) and self._should_rerun_test(test, name, err)
def _handle_test_error_or_failure(self, test, err):
"""
Handle a flaky test error or failure.
Returning True from this method keeps the test runner from reporting
the test as a failure; this way we can retry and only report as a
failure if we are out of retries.
This method may only be called once per test run; it changes persisted flaky attributes.
:param test:
The test that has raised an error
:type test:
:class:`Function`
:param err:
Information about the test failure (from sys.exc_info())
:type err:
`tuple` of `type`, :class:`Exception`, `traceback`
:return:
True, if the test will be rerun;
False, if the test runner should handle it.
:rtype:
`bool`
"""
try:
name = self._get_test_callable_name(test)
except AttributeError:
return False
if self._has_flaky_attributes(test):
self._had_flaky_tests = True
self._add_flaky_test_failure(test, err)
should_handle = self._should_handle_test_error_or_failure(test)
self._increment_flaky_attribute(test, FlakyNames.CURRENT_RUNS)
if should_handle:
flaky_attributes = self._get_flaky_attributes(test)
if self._should_rerun_test(test, name, err):
self._log_intermediate_failure(err, flaky_attributes, name)
self._mark_test_for_rerun(test)
return True
self._log_test_failure(name, err, self._not_rerun_message)
return False
flaky_attributes = self._get_flaky_attributes(test)
self._report_final_failure(err, flaky_attributes, name)
return False
def _should_rerun_test(self, test, name, err):
"""
Whether or not a test should be rerun.
This is a pass-through to the test's rerun filter.
A flaky test will only be rerun if it hasn't failed too many
times to succeed at least min_passes times, and if
this method returns True.
:param test:
The test that has raised an error
:type test:
:class:`Function`
:param name:
The test name
:type name:
`unicode`
:param err:
Information about the test failure (from sys.exc_info())
:type err:
`tuple` of `class`, :class:`Exception`, `traceback`
:return:
Whether flaky should rerun this test.
:rtype:
`bool`
"""
rerun_filter = self._get_flaky_attribute(test, FlakyNames.RERUN_FILTER)
return rerun_filter(err, name, test, self)
def _mark_test_for_rerun(self, test):
"""
Mark a flaky test for rerun.
:param test:
The test that has raised an error or succeeded
:type test:
:class:`Function`
"""
raise NotImplementedError # pragma: no cover
def _should_handle_test_success(self, test):
if not self._has_flaky_attributes(test):
return False
flaky = self._get_flaky_attributes(test)
flaky[FlakyNames.CURRENT_PASSES] += 1
flaky[FlakyNames.CURRENT_RUNS] += 1
return not self._has_flaky_test_succeeded(flaky)
def _handle_test_success(self, test):
"""
Handle a flaky test success.
Count remaining retries and compare with number of required successes
that have not yet been achieved; retry if necessary.
Returning True from this method keeps the test runner from reporting
the test as a success; this way we can retry and only report as a
success if the test has passed the required number of times.
:param test:
The test that has raised an error
:type test:
:class:`Function`
:return:
True, if the test will be rerun; False, if the test runner should handle it.
:rtype:
`bool`
"""
try:
name = self._get_test_callable_name(test)
except AttributeError:
return False
need_reruns = self._should_handle_test_success(test)
if self._has_flaky_attributes(test):
self._had_flaky_tests = True
flaky = self._get_flaky_attributes(test)
min_passes = flaky[FlakyNames.MIN_PASSES]
passes = flaky[FlakyNames.CURRENT_PASSES] + 1
self._set_flaky_attribute(test, FlakyNames.CURRENT_PASSES, passes)
self._increment_flaky_attribute(test, FlakyNames.CURRENT_RUNS)
if self._flaky_success_report:
self._stream.writelines([
str(name),
' passed {} out of the required {} times. '.format(
passes,
min_passes,
),
])
if need_reruns:
self._stream.write(
'Running test again until it passes {} times.\n'.format(
min_passes,
)
)
else:
self._stream.write('Success!\n')
if need_reruns:
self._mark_test_for_rerun(test)
return need_reruns
@staticmethod
def add_report_option(add_option):
"""
Add an option to the test runner to suppress the flaky report.
:param add_option:
A function that can add an option to the test runner.
Its argspec should equal that of argparse.add_option.
:type add_option:
`callable`
"""
add_option(
'--no-flaky-report',
action='store_false',
dest='flaky_report',
default=True,
help="Suppress the report at the end of the "
"run detailing flaky test results.",
)
add_option(
'--no-success-flaky-report',
action='store_false',
dest='flaky_success_report',
default=True,
help="Suppress reporting flaky test successes"
"in the report at the end of the "
"run detailing flaky test results.",
)
@staticmethod
def add_force_flaky_options(add_option):
"""
Add options to the test runner that force all tests to be flaky.
:param add_option:
A function that can add an option to the test runner.
Its argspec should equal that of argparse.add_option.
:type add_option:
`callable`
"""
add_option(
'--force-flaky',
action="store_true",
dest="force_flaky",
default=False,
help="If this option is specified, we will treat all tests as "
"flaky."
)
add_option(
'--max-runs',
action="store",
dest="max_runs",
type=int,
default=2,
help="If --force-flaky is specified, we will run each test at "
"most this many times (unless the test has its own flaky "
"decorator)."
)
add_option(
'--min-passes',
action="store",
dest="min_passes",
type=int,
default=1,
help="If --force-flaky is specified, we will run each test at "
"least this many times (unless the test has its own flaky "
"decorator)."
)
def _add_flaky_report(self, stream):
"""
Baseclass override. Write details about flaky tests to the test report.
:param stream:
The test stream to which the report can be written.
:type stream:
`file`
"""
value = self._stream.getvalue()
# Do not print report if there were no tests marked 'flaky' at all.
if not self._had_flaky_tests and not value:
return
# If everything succeeded and --no-success-flaky-report is specified
# don't print anything.
if not self._flaky_success_report and not value:
return
stream.write('===Flaky Test Report===\n\n')
# Python 2 will write to the stderr stream as a byte string, whereas
# Python 3 will write to the stream as text. Only encode into a byte
# string if the write tries to encode it first and raises a
# UnicodeEncodeError.
try:
stream.write(value)
except UnicodeEncodeError:
stream.write(value.encode('utf-8', 'replace'))
stream.write('\n===End Flaky Test Report===\n')
@classmethod
def _copy_flaky_attributes(cls, test, test_class):
"""
Copy flaky attributes from the test callable or class to the test.
:param test:
The test that is being prepared to run
:type test:
:class:`Function`
"""
test_callable = cls._get_test_callable(test)
if test_callable is None:
return
for attr, value in cls._get_flaky_attributes(test_class).items():
already_set = hasattr(test, attr)
if already_set:
continue
attr_on_callable = getattr(test_callable, attr, None)
if attr_on_callable is not None:
cls._set_flaky_attribute(test, attr, attr_on_callable)
elif value is not None:
cls._set_flaky_attribute(test, attr, value)
@staticmethod
def _get_flaky_attribute(test_item, flaky_attribute):
"""
Gets an attribute describing the flaky test.
:param test_item:
The test method from which to get the attribute
:type test_item:
`callable` or :class:`Function`
:param flaky_attribute:
The name of the attribute to get
:type flaky_attribute:
`unicode`
:return:
The test callable's attribute, or None if the test
callable doesn't have that attribute.
:rtype:
varies
"""
return getattr(test_item, flaky_attribute, None)
@staticmethod
def _set_flaky_attribute(test_item, flaky_attribute, value):
"""
Sets an attribute on a flaky test. Uses magic __dict__ since setattr
doesn't work for bound methods.
:param test_item:
The test callable on which to set the attribute
:type test_item:
`callable` or :class:`Function`
:param flaky_attribute:
The name of the attribute to set
:type flaky_attribute:
`unicode`
:param value:
The value to set the test callable's attribute to.
:type value:
varies
"""
test_item.__dict__[flaky_attribute] = value
@classmethod
def _increment_flaky_attribute(cls, test_item, flaky_attribute):
"""
Increments the value of an attribute on a flaky test.
:param test_item:
The test callable on which to set the attribute
:type test_item:
`callable` or :class:`Function`
:param flaky_attribute:
The name of the attribute to set
:type flaky_attribute:
`unicode`
"""
cls._set_flaky_attribute(test_item, flaky_attribute, cls._get_flaky_attribute(test_item, flaky_attribute) + 1)
@classmethod
def _has_flaky_attributes(cls, test):
"""
Returns True if the test callable in question is marked as flaky.
:param test:
The test that is being prepared to run
:type test:
:class:`Function`
:return:
:rtype:
`bool`
"""
current_runs = cls._get_flaky_attribute(test, FlakyNames.CURRENT_RUNS)
return current_runs is not None
@classmethod
def _get_flaky_attributes(cls, test_item):
"""
Get all the flaky related attributes from the test.
:param test_item:
The test callable from which to get the flaky related attributes.
:type test_item:
`callable` or :class:`Function`
:return:
:rtype:
`dict` of `unicode` to varies
"""
return {
attr: cls._get_flaky_attribute(
test_item,
attr,
) for attr in FlakyNames()
}
@classmethod
def _add_flaky_test_failure(cls, test, err):
"""
Store test error information on the test callable.
:param test:
The flaky test on which to update the flaky attributes.
:type test:
:class:`Function`
:param err:
Information about the test failure (from sys.exc_info())
:type err:
`tuple` of `class`, :class:`Exception`, `traceback`
"""
errs = getattr(test, FlakyNames.CURRENT_ERRORS, None) or []
cls._set_flaky_attribute(test, FlakyNames.CURRENT_ERRORS, errs)
errs.append(err)
@classmethod
def _has_flaky_test_failed(cls, flaky):
"""
Whether or not the flaky test has failed
:param flaky:
Dictionary of flaky attributes
:type flaky:
`dict` of `unicode` to varies
:return:
True if the flaky test should be marked as failure; False if
it should be rerun.
:rtype:
`bool`
"""
max_runs, current_runs, min_passes, current_passes = (
flaky[FlakyNames.MAX_RUNS],
flaky[FlakyNames.CURRENT_RUNS],
flaky[FlakyNames.MIN_PASSES],
flaky[FlakyNames.CURRENT_PASSES],
)
runs_left = max_runs - current_runs
passes_needed = min_passes - current_passes
no_retry = passes_needed > runs_left
return no_retry and not cls._has_flaky_test_succeeded(flaky)
@staticmethod
def _has_flaky_test_succeeded(flaky):
"""
Whether or not the flaky test has succeeded
:param flaky:
Dictionary of flaky attributes
:type flaky:
`dict` of `unicode` to varies
:return:
True if the flaky test should be marked as success; False if
it should be rerun.
:rtype:
`bool`
"""
return flaky[FlakyNames.CURRENT_PASSES] >= flaky[FlakyNames.MIN_PASSES]
@classmethod
def _get_test_callable(cls, test):
"""
Get the test callable, from the test.
:param test:
The test that has raised an error or succeeded
:type test:
:class:`pytest.Item`
:return:
The test declaration, callable and name that is being run
:rtype:
`callable`
"""
raise NotImplementedError # pragma: no cover
@staticmethod
def _get_test_callable_name(test):
"""
Get the name of the test callable from the test.
:param test:
The test that has raised an error or succeeded
:type test:
:class:`pytest.Item`
:return:
The name of the test callable that is being run by the test
:rtype:
`unicode`
"""
raise NotImplementedError # pragma: no cover
@classmethod
def _make_test_flaky(cls, test, max_runs=None, min_passes=None, rerun_filter=None):
"""
Make a given test flaky.
:param test:
The test in question.
:type test:
:class:`Function`
:param max_runs:
The value of the FlakyNames.MAX_RUNS attribute to use.
:type max_runs:
`int`
:param min_passes:
The value of the FlakyNames.MIN_PASSES attribute to use.
:type min_passes:
`int`
:param rerun_filter:
Filter function to decide whether a test should be rerun if it fails.
Function signature is as follows:
(err, name, test, plugin) -> should_rerun
- err (`tuple` of `class`, :class:`Exception`, `traceback`):
Information about the test failure (from sys.exc_info())
- name (`unicode`):
The test name
- test (:class:`Function`):
The test that has raised an error
- plugin (:class:`FlakyPytestPlugin`):
The flaky plugin. Has a :prop:`stream` that can be written to in
order to add to the Flaky Report.
:type rerun_filter:
`callable`
"""
attrib_dict = defaults.default_flaky_attributes(max_runs, min_passes, rerun_filter)
for attr, value in attrib_dict.items():
cls._set_flaky_attribute(test, attr, value)
|